# Accura Scan -  Face Match / Face Biometrics

{% embed url="<https://drive.google.com/file/d/1MxXqc_FtS8wTI6Qa1aGiIBzAkgJD9uL6/view?usp=sharing>" %}

## Step 1: Before you begin

1. If you haven't done already then follow [Project Setup](https://docs.accurascan.com/language/android/project-setup) steps.
2. Please download the Accura Scan license and then add it to your app.

   1. To generate your Accura Scan license contact <sales@accurascan.com>

   2. Move your **accuraface.license** file into the **module (app-level)** **assets folder** (usually `<project>/<app-module>/src/main/assets`)

   > <mark style="color:blue;">**Note:**</mark> Make sure license file name should be **accuraface.license**
3. Permissions required:
   1. Camera Permission `android.permission.CAMERA`
   2. Storage Permission required only for print out debug logs.

{% hint style="info" %} <mark style="color:blue;">**Note:**</mark> Enable logging for debugging purposes using the methods provided below. Please remember to disable logging before releasing the app. **(Storage permission is required for logging).**\
AccuraFaceMatchLog.setPrintLogs(true);\
AccuraFaceMatchLog.refreshLogfile(this);\
\
Log file will be stored in **InternalStorage/Downloads/AccuraLog.txt**
{% endhint %}

## Step 2 : Open Auto Capture Camera

* Customize camera screen **(Optional)**<br>

  ```
  FMCameraScreenCustomization cameraScreenCustomization = new FMCameraScreenCustomization();

  cameraScreenCustomization.backGroundColor = getResources().getColor(R.color.fm_camera_Background);
  cameraScreenCustomization.closeIconColor = getResources().getColor(R.color.fm_camera_CloseIcon);
  cameraScreenCustomization.feedbackBackGroundColor = getResources().getColor(R.color.fm_camera_feedbackBg);
  cameraScreenCustomization.feedbackTextColor = getResources().getColor(R.color.fm_camera_feedbackText);
  cameraScreenCustomization.feedbackTextSize = 18;
  cameraScreenCustomization.feedBackframeMessage = "Frame Your Face";
  cameraScreenCustomization.feedBackAwayMessage = "Move Phone Away";
  cameraScreenCustomization.feedBackOpenEyesMessage = "Keep Your Eyes Open";
  cameraScreenCustomization.feedBackCloserMessage = "Move Phone Closer";
  cameraScreenCustomization.feedBackCenterMessage = "Move Phone Center";
  cameraScreenCustomization.feedBackMultipleFaceMessage = "Multiple Face Detected";
  cameraScreenCustomization.feedBackHeadStraightMessage = "Keep Your Head Straight";
  cameraScreenCustomization.feedBackBlurFaceMessage = "Blur Detected Over Face";
  cameraScreenCustomization.feedBackGlareFaceMessage = "Glare Detected";
  cameraScreenCustomization.feedBackLowLightMessage = "Low light detected";
  cameraScreenCustomization.feedbackDialogMessage = "Loading...";
  cameraScreenCustomization.feedBackProcessingMessage = "Processing...";
  cameraScreenCustomization.showlogo = 0; // Set 0 to hide logo from selfie camera screen
  cameraScreenCustomization.logoIcon = R.drawable.your_logo; // To set your custom logo
      
  // FMCameraScreenCustomization.CAMERA_FACING_FRONT to set selfie camera       
  // FMCameraScreenCustomization.CAMERA_FACING_BACK to set rear camera
  cameraScreenCustomization.facing = FMCameraScreenCustomization.CAMERA_FACING_FRONT;

      
  // 0 for full dark face and 100 for full bright face or set it -1 to remove low light filter
  cameraScreenCustomization.setLowLightTolerence(-1/*lowLightTolerence*/);

  // 0 for clean face and 100 for Blurry face or set it -1 to remove blur filter
  cameraScreenCustomization.setBlurPercentage(80/*blurPercentage*/); // To allow blur on face
                                                  
  // Set min and max percentage for glare or set it -1 to remove glare filter
  cameraScreenCustomization.setGlarePercentage(6/*glareMinPercentage*/, 99/*glareMaxPercentage*/);
  ```

* Open Camera screen using android **Intent.**<br>

  ```
  Intent intent = SelfieFMCameraActivity.getCustomIntent(this, cameraScreenCustomization);
  startActivityForResult(intent, ACCURA_FACEMATCH_CAMERA);
  ```

{% hint style="info" %} <mark style="color:blue;">**Note:**</mark> If you want to use default camera screen then create intent with null object.\
Intent intent = SelfieFMCameraActivity.getCustomIntent(this,  null);&#x20;
{% endhint %}

* Receive Capture Image<br>

  ```
  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {
          if (requestCode == ACCURA_LIVENESS_CAMERA && data != null) {
              AccuraFMCameraModel result = data.getParcelableExtra("Accura.fm");
              if (result == null) {
                  return;
              }
              if (result.getStatus().equals("1")) {
                  // result bitmap
                  Bitmap bitmap = result.getFaceBiometrics();
                  Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
              } else {
                  Toast.makeText(this, "Failed" + result.getStatus(), Toast.LENGTH_SHORT).show();
              }
          }
      }
  }
  ```

## **Step 3 : Implement face match**

1. **(Optional)** Required storage permission to read image file from internal storage.

2. Initialize **`com.inet.facelock.callback.FaceHelper`** in onCreate method <br>

   ```
   FaceHelper faceHelper = new FaceHelper(/*your activity context*/);
   faceHelper.setFaceMatchCallBack(this);
   faceHelper.initEngine();
   ```

3. Set src and target image to faceHelper<br>

   > <mark style="color:blue;">**Note:**</mark> Make sure to call **"faceHelper.setInputImage"** first, followed by **"faceHelper.setMatchImage"**.

   \
   Using a **File**

   ```
   faceHelper.setInputImage(srcFile);
   faceHelper.setMatchImage(targetFile);
   ```

   Using a **File URI**

   ```
   faceHelper.setInputImage(srcUri);
   faceHelper.setMatchImage(targetUri);
   ```

   Using **Path**

   ```
   faceHelper.setInputImage(srcPath);
   faceHelper.setMatchImage(targetPath);
   ```

   Using a **Bitmap**

   ```
   faceHelper.setInputImage(srcBitmap);
   faceHelper.setMatchImage(targetBitmap);
   ```

   \
   Using a file **URI** by using one function

   ```
   faceHelper.getFaceMatchScore(srcUri, targetUri);
   ```

   \
   Using a **file** by using one function

   ```
   faceHelper.getFaceMatchScore(srcFile, targetFile);
   ```

4. Implement **`com.inet.facelock.callback.FaceCallback`**&#x74;o your Activity and Override following methods to receive data.

   * Received original image in Bitmap format to display on ui as per your requirement <br>

     ```
     @Override
     public void onSetInputImage(Bitmap src1) {
         // set src image to your view
         image1.setImageBitmap(src1);
     }

     @Override
     public void onSetMatchImage(Bitmap src2) {
         // set target image to your view
         image2.setImageBitmap(src2);
     }
     ```

   * Receive Match score of src and target Image on below override method<br>

     ```
     @Override
     public void onFaceMatch(float score) {
         // get face match score
         System.out.println("Match Score : " + ss + " %");
     }
     ```

   * **(Optional)** Recomanded override methods for SDK<br>

     ```
     @Override
     public void onInitEngine(int ret) {
     }

     @Override
     public void onExtractInit(int ret) {
     }
     ```

   * Receive Detected Face position on your src and Target Images<br>

     ```
     // Src Image result
     @Override
     public void onLeftDetect(FaceDetectionResult faceResult) {
         if (faceResult != null) {
             // do some code
             Bitmap bitmap = faceResult.getFaceImage(bitmap); // get face Image
         }
     }

     // Target Image Result
     @Override
     public void onRightDetect(FaceDetectionResult faceResult) {
         if (faceResult != null) {
             // do some code
             Bitmap bitmap = faceResult.getFaceImage(bitmap); // get face Image
         }
     }
     ```

{% hint style="info" %}
Take a look of [ActivityFaceMatch.java](https://github.com/accurascan/Android-KYC) for full working example.
{% endhint %}
