preface

Just recently, Huawei HMS ML Kit released version 1.0.3.30. ML Kit added bank card Identification (BCR) and second-generation ID card Identification (ICR) on the basis of the original universal OCR function. Today xiaobian will introduce the ICR capability to you. ML kit provides not only the language related AI ability, also provides visual direction ability of AI, detailed information you can walk developer.huawei.com/consumer/cn…


scenario

As a person with identity, identity authentication is inevitable. Usually, we need identification to buy train tickets, plane tickets, hotels, hotels need identification, now even play games need to do real name authentication. As much as we’d love to prove our identity, typing in those 18 incredibly long ID numbers every time is a bit of a muddle and a bit of a handwringing. Therefore, using the second generation id card identification (ICR) scanning id card, do the entry of identity information is quite convenient.

Travel app for identity authentication

Game app for identity authentication

Any app that requires authentication, use your imagination


Are you now itching to develop and implement your own? That I don’t talk any more, to introduce you to the simple integration of ML kit to achieve ICR steps ~~

Pre-development preparation

Android studio installation

Very simple, download and install. Specific download link:

Android studio website download links: developer.android.com/studio

Android studio installation process reference links: www.cnblogs.com/xiadewang/p…

Add Huawei Maven storehouse to project gradle

Open the project-level build.gradle file in AndroidStudio

buildscript {
   repositories {        
       maven {url 'http://developer.huawei.com/repo/'}
   }    
}
allprojects {
   repositories {      
       maven { url 'http://developer.huawei.com/repo/'}}}Copy the code

Add SDK dependencies to build.gradle at the application level

dependencies{  
 // Introduce the base SDK
 implementation 'com. Huawei. HMS: ml - computer vision - icr: 1.0.3.300'
 // Introduce the id plugin package
 implementation 'com. Huawei. HMS: ml - computer - card - icr - cn - plugin: 1.0.3.315'
 // Introduce id model package
 implementation 'com. Huawei. HMS: ml - computer - card - icr - cn - model: 1.0.3.315'
}
Copy the code

Incrementally add the model to the androidmanifest.xml file for automatic download

To enable the application to automatically update the latest machine learning model to the user’s device after the user installs your application from Huawei App Market, add the following statement to the application’s Androidmanifest.xml file:

<manifest ... <meta-data android:name="com.huawei.hms.ml.DEPENDENCY" android:value= "icr"/> <! --If multiple models are required,set the parameter as follows: android:value="object,ocr,face,label,icr,bcr,imgseg"--> ... </manifest>Copy the code

Apply for camera, network, and storage permissions in the androidmanifest.xml file

<! -- Camera permission --> <uses-permission Android :name="android.permission.CAMERA"/ > <! --> <uses-permission android:name="android.permission.INTERNET"/ > <! -- write permission --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/ > <! --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Copy the code

Code development key steps

During development, integrated development can be carried out in three ways: identification plug-in for video stream ID card identification, device id card identification and cloud ID card identification. Here we mainly introduced to identify the video stream id used to identify the plugin development steps, if you are interested in two other integrated way, can consult developer alliance development steps: huawei developer.huawei.com/consumer/en…

Create the recognition result callback function

Recognize the result callback and override onSuccess, onCanceled, onFailure, and onDenied. MLCnIcrCaptureResult indicates the recognition result, onCanceled indicates that the user cancels, onFailure indicates that the camera is unavailable, and so on.

private MLCnIcrCapture.Callback idCallback = new MLCnIcrCapture.Callback() {
   @Override
   public void onSuccess(MLCnIcrCaptureResult idCardResult){
       // Recognition is successful.
   }  
   @Override
   public void onCanceled(a){
       // The user cancels the processing.
   }  
   // A callback method that does not recognize any text message or recognize that a system exception occurred during the process.
   // retCode: error code.
   // bitmap: failed id card image.
   @Override
   public void onFailure(int retCode, Bitmap bitmap){
       // Identify exception handling.
   }
   @Override
   public void onDenied(a){
       // The camera does not support scene processing.}}Copy the code

Setting identification Parameters

Set the identification parameters and call the capture interface. The identification result is returned through the callback function of Step 1.

private void startCaptureActivity(MLCnIcrCapture.Callback callback, boolean isFront, boolean isRemote) {
   MLCnIcrCaptureConfig config = new MLCnIcrCaptureConfig.Factory()
       // Set the front and back sides of the id card.
       // true: positive.
       // false: reverse.
       .setFront(true)
       // Set whether to use cloud-side capabilities for identification.
       // true: cloud side.
       // false: end side.
       .setRemote(false)
       .create();
   MLCnIcrCapture icrCapture = MLCnIcrCaptureFactory.getInstance().getIcrCapture(config);
   icrCapture.capture(callback, this);
}
Copy the code

Implement identity recognition

In the callback of the detection button, the method defined in Step 2 is called to realize id identification.

@Override
public void onClick(View v) {
   switch (v.getId()) {
       // Identify the front button.
       case R.id.IDCard_image_front:
           startCaptureActivity(idCallback, true.false);
           break;
       // Identify the back button.
       case R.id.IDCard_image_back:
           startCaptureActivity(idCallBack, false.false);
           break;
       default:
           break; }}Copy the code

Effect of the Demo

This demo is the key to identifying the front and back sides of an ID card. The result is as follows:


Forward link: fast application, fast service, direct service… What the hell are these

Content source: developer.huawei.com/consumer/cn…

Original author: LittleWhite