Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
10 kB
2
Indexable
Never
package com.innovatrics.abis.training;

import java.io.IOException;
import java.util.*;

import com.innovatrics.abis.rest.*;
import com.innovatrics.abis.rest.api.*;
import com.innovatrics.abis.rest.client.*;
import com.innovatrics.abis.rest.model.*;
import com.innovatrics.abis.rest.model.EnrollAction.EnrollActionTypeEnum;
import com.innovatrics.abis.rest.model.Fingerprint.PositionEnum;

/**
 * Masterclass Sao Paulo, Brazil 2022
 * Simple demo integration / hands on 
 * 
 * alejandro.aleman@innovatrics.com
 */
public class App {

    public static String externalId = "Escreva aqui seu RG ou ID"; // (Normalmente você usaria um identificador de banco de dados)

        public static void main(String[] args) throws IOException, ApiException {
            ApiClient defaultClient = Configuration.getDefaultApiClient();

            defaultClient.setBasePath("http://escreva aqui o URL da instância do seu ABIS");

            Utils utils = new Utils();

            //Geramos uma instância do endpoint com estado
            ApplicantApi apiInstance = new ApplicantApi(defaultClient);

            //E geramos uma instância do endpoint SEM estado
            RpcApi rpcInstance = new RpcApi(defaultClient);

            int choice = -1;

            do {
                choice = Utils.DisplayMenu();

                EnrollAction ea = new EnrollAction();

                switch(choice){
                    // Enrolamento sem deduplicacao: O Aplicante tendra status Enrolled (no adjudicable)
                    case 1:
                        ea.setEnrollActionType(EnrollActionTypeEnum.NONE);
                        Enroll(apiInstance, ea);
                        break;
                    case 2:
                    // Enrolamento sim deduplicacao: O Aplicante tendra status Master (sim adjudicable)
                        ea.setEnrollActionType(EnrollActionTypeEnum.MASTERIZE);
                        Enroll(apiInstance, ea);
                        break;
                    case 3:
                    // Enrolamento sim deduplicacao: O Aplicante tendra status Master ou Duplicado (sim adjudicable)
                        ea.setEnrollActionType(EnrollActionTypeEnum.DEDUPLICATE);
                        Enroll(apiInstance, ea);
                        break;
                    case 4:
                    // Fazemos um get
                        SimpleGet(apiInstance);
                        break;
                    case 5:
                    // Fazemos uma identificacao com estado
                        Identify(apiInstance, rpcInstance, true);
                        break;
                    case 6:
                    // Fazemos uma identificacao sem estado
                        Identify(apiInstance, rpcInstance, false);
                        break;
                    case 7:
                    // Fazemos uma verificacao sem estado
                        Verify(rpcInstance);
                        break;
                    case 8:
                        System.out.println("Até logo!");
                }

                utils.readLine("Pressione enter para continuar...");
            } while (choice != 8);
    }

    public static void Enroll(ApplicantApi apiInstance, EnrollAction ea) throws IOException {
        try {

            EnrollApplicantRequest ear = new EnrollApplicantRequest();

            FullApplicantFingerprintModality fafm = new FullApplicantFingerprintModality();

            {
                Fingerprint f = new Fingerprint();
                f.setPosition(PositionEnum.RIGHTTHUMB);
                //f.setPosition(PositionEnum..RIGHTTHUMB);

                Image fpImage = new Image();

                if(ea.getEnrollActionType() == EnrollActionTypeEnum.NONE || ea.getEnrollActionType() == EnrollActionTypeEnum.DEDUPLICATE){
                    fpImage.dataBytes(Utils.getFingerprintBytes(true));
                }
                else{
                    fpImage.dataBytes(Utils.getFingerprintBytes(false));
                }
                
                fpImage.setFormat(com.innovatrics.abis.rest.model.Image.FormatEnum.JPG);

                f.setImage(fpImage);

                fafm.addFingerprintsItem(f);
            }

            /* // Quando não é possível capturar um dedo 
             * {
             *      MissingFingerprint mfp = new MissingFingerprint();
             *      
             *      mfp.setPosition(MissingFingerprint.PositionEnum.LEFTINDEX);
             *      mfp.missingReasonCode("001"); // O código é seu, esta é apenas a minha demonstração
             *      mfp.missingReasonText("Finger is bandaged");
             *      
             *      fafm.addMissingFingerprintsItem(mfp);
             *      
             *      ArrayList<MissingFingerprint> mfps = new ArrayList<>();
             *      mfps.add(mfp);
             *      
             *      fafm.setMissingFingerprints(mfps);
             * }
             */

            ear.setFingerprintModality(fafm);

            /*
                FullApplicantFaceModality fafam = new FullApplicantFaceModality();

                FaceImage fi = new FaceImage();
                fi.dataBytes(Utils.getFaceBytes(true));

                ArrayList<FaceImage> faces = new ArrayList<>();
                faces.add(fi);

                ear.setFaceModality(fafam);
            */

            // Careful with this one down
     

            ear.setExternalId(externalId);

            //ear.setGallery("nome da galeria aqui (opcional)");

            ApplicantIdentifierResponse resp = apiInstance.enroll(externalId, null, ear);

            System.out.println(resp.toString());
            
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    public static void SimpleGet(ApplicantApi apiInstance) throws IOException {
        try {
            ApplicantResponse resp = apiInstance.get(externalId, true, null);

            System.out.println(resp.toString());

            // Download the fingerprints
            if(resp.getFingerprintModality() != null){
                for(Fingerprint fp : resp.getFingerprintModality().getFingerprints()){
                    // Utiliza la classe Utils.getResource 
                    // fp.getImage().getDataUrl()
                }
            }
        } catch (ApiException ae) {
            // If 404 here, it means it's with the applicant
            System.out.println(ae.getResponseBody());
        } catch (Exception e) {
            // If 404 here, it means it's with the web server
            System.out.println(e.getStackTrace());
        }
    }
    
    private static void Identify(ApplicantApi apiInstance, RpcApi rpcInstance, boolean stateful)
            throws ApiException, IOException {

        List<IdentificationCandidate> ir = new ArrayList<IdentificationCandidate>();

        try {
            if (stateful) {

                IdentifyApplicantRequest iar = new IdentifyApplicantRequest();

                IdentificationParameters identificationParameters = new IdentificationParameters();
                identificationParameters.setThreshold(100);
                identificationParameters.setMaxRotation(180); // 0 - 180
                identificationParameters.setSpeed(4); // 6 or 7

                // identificationParameters.setGalleries(Arrays.singletonList("default"));
                // identificationParameters.setGalleries(Arrays.asList("default", "criminal"));
                // identificationParameters.setGalleries(Arrays.asList("criminal"));

                iar.setIdentificationParameters(identificationParameters);

                // e stateful, por tanto usamos apiInstance
                ir = apiInstance.identify(externalId, null, true, null);

            } else {
                IdentifyProbeByImagesRequest ipir = new IdentifyProbeByImagesRequest();

                BiometricDataImages probe = new BiometricDataImages();
                FingerprintImage fingerprintsItem = new FingerprintImage();

                fingerprintsItem.dataBytes(Utils.getFingerprintBytes(true));
                fingerprintsItem.setPosition(FingerprintImage.PositionEnum.RIGHTTHUMB);

                probe.addFingerprintsItem(fingerprintsItem);

                ipir.setProbe(probe);

                // e stateless, por tanto usamos apiInstance
                ir = rpcInstance.identifyProbeByImages(ipir, true, null);
            }

            for (IdentificationCandidate ic : ir) {
                System.out.println("Candidate:");
                System.out.println(ic.toString());
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    // Realiza uma operação de verificação entre IMAGENS e uma referência no ABIS, via externalID.
    private static void Verify(RpcApi rpcInstance) {
        try {
            VerifyProbeImagesReferencesRequest vpirr = new VerifyProbeImagesReferencesRequest();
            vpirr.addReferencesItem(externalId);

            BiometricDataImages probe = new BiometricDataImages();

            FingerprintImage fingerprintsItem = new FingerprintImage();
            
            fingerprintsItem.dataBytes(Utils.getFingerprintBytes(true));
            fingerprintsItem.setPosition(FingerprintImage.PositionEnum.RIGHTTHUMB);

            probe.addFingerprintsItem(fingerprintsItem);

            vpirr.setProbe(probe);

            // Tarefa: Testar tambem com fullResponse = false
            List<VerificationCandidate> vc = rpcInstance.verifyImagesReferences(vpirr, true, UUID.randomUUID().toString());

            for (VerificationCandidate verificationCandidate : vc) {
                System.out.println("Candidate:");
                System.out.println(verificationCandidate.toString());
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}