目录

Before API development, you need to make necessary development preparations, ensure that the Maven repository address of the HMS Core SDK has been configured in your project, and the SDK of this service has been integrated.

You can call the general card recognition plug-in API to obtain the recognition result through the callback function, regardless of recognition from camera streams, picture taking, or images. For details, please refer to Sample Code.

1. Create the recognition result callback function and reload the onResult, onCanceled, onFailure, and onDenied methods. onResult indicates that the result is returned. MLGcrCaptureResult indicates the recognition result. onCanceled indicates that the user cancels the recognition. onFailure indicates that the recognition fails. onDenied indicates that the recognition request is denied, for example, the camera is unavailable.

  1. private MLGcrCapture.Callback callback = new MLGcrCapture.Callback() {
  2.    // This method requires the following status codes:
  3.    // MLGcrCaptureResult.CAPTURE_CONTINUE: The returned result does not meet the requirements (for example, no result is returned or the returned result is incorrect). In camera stream or picture taking mode, the recognition continues.
  4.    // MLGcrCaptureResult.CAPTURE_STOP: The returned result meets the requirements and the recognition stops.
  5.    @Override
  6.    public int onResult(MLGcrCaptureResult mlGcrCaptureResult, Object o){
  7.        // Process the recognition result. Implement post-processing logic based on your use case to extract valid information and return the status code.
  8.        if (cardResult != null) {// Check whether a result is returned.
  9.            // Recognition result processing logic.
  10.            if (!isMatch(cardResult)) {// Check whether the processing result meets the requirements. Implement the isMatch method based on your use case.
  11.                return MLGcrCaptureResult.CAPTURE_CONTINUE;// The processing result does not meet the requirements, and the recognition continues.
  12.            }
  13.            // Process the results that meet the requirements.
  14.        }
  15.        return MLGcrCaptureResult.CAPTURE_STOP;// The processing ends, and the recognition exits.
  16.    }  
  17.    @Override
  18.    public void onCanceled(){
  19.        // Processing for recognition request cancelation.
  20.    }  
  21.    // Callback method used when no text is recognized or a system exception occurs during recognition.
  22.    // retCode: error code.
  23.    // bitmap: general card image that fails to be recognized.
  24.    @Override
  25.    public void onFailure(int retCode, Bitmap bitmap){
  26.        // Exception handling.
  27.    }
  28.    @Override
  29.    public void onDenied(){
  30.        // Processing for recognition request deny scenarios, for example, the camera is unavailable.
  31.    }  
  32. };

2. Set the recognition parameters for calling the capture API of the recognizer. The recognition result is returned through the callback function created in step 1. The recognition works based on camera streams, picture taking, or images.

  1. private void startCaptureActivity(Object object, MLGcrCapture.Callback callback) {
  2.    // Create a general card recognition service configurator.
  3.    MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().create();
  4.    // Create a general card recognition UI configurator.
  5.    MLGcrCaptureUIConfig uiConfig = new MLGcrCaptureUIConfig.Factory()        
  6.        // Set the color of the scanning box.
  7.        .setScanBoxCornerColor(Color.GREEN)
  8.        // Set the prompt text in the scan box. It is recommended that the text contain less than 30 characters.
  9.        .setTipText("Recognizing, align edges")
  10.        // Set the recognition screen display orientation.
  11.        // MLGcrCaptureUIConfig.ORIENTATION_AUTO: adaptive mode. The display orientation is determined by the physical sensor.
  12.        // MLGcrCaptureUIConfig.ORIENTATION_LANDSCAPE: landscape mode.
  13.        // MLGcrCaptureUIConfig.ORIENTATION_PORTRAIT: portrait mode.
  14.        .setOrientation(MLGcrCaptureUIConfig.ORIENTATION_AUTO)
  15.        .create();
  16.    // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
  17.    MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig);
  18.    // Method 2: Use the default UI to create a general card recognition processor.
  19.    MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig);
  20.    // Bind the general card recognition processor to the processing result callback function.
  21.    ocrManager.capturePreview(this, object, callback);
  22. }
  1. private void startTakePhotoActivity(Object object, MLGcrCapture.Callback callback) {
  2.    // Create a general card recognition service configurator.
  3.    MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().create();
  4.    // Create a general card recognition UI configurator.
  5.    MLGcrCaptureUIConfig uiConfig = new MLGcrCaptureUIConfig.Factory()
  6.        // Set the color of the scanning box.
  7.        .setScanBoxCornerColor(Color.BLUE)
  8.        // Set the prompt text in the scan box. It is recommended that the text contain less than 30 characters.
  9.        .setTipText("Taking picture, align edges")
  10.        // Set the recognition screen display orientation.
  11.        // MLGcrCaptureUIConfig.ORIENTATION_AUTO: adaptive mode. The display orientation is determined by the physical sensor.
  12.        // MLGcrCaptureUIConfig.ORIENTATION_LANDSCAPE: landscape mode.
  13.        // MLGcrCaptureUIConfig.ORIENTATION_PORTRAIT: portrait mode.
  14.        .setOrientation(MLGcrCaptureUIConfig.ORIENTATION_AUTO)
  15.        .create();
  16.    // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
  17.    MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig);
  18.    // Method 2: Use the default UI to create a general card recognition processor.
  19.    MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig);
  20.    // Bind the general card recognition processor to the processing result callback function.
  21.    ocrManager.capturePhoto(this, object, callback);
  22. }
  1. private void startLocalImageActivity(Bitmap bitmap, Object object, MLGcrCapture.Callback callback) {
  2.    // Create a general card recognition service configurator.
  3.    MLGcrCaptureConfig config = new MLGcrCaptureConfig.Factory().create();
  4.    MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(config);
  5.    // The bitmap indicates image data in bitmap format. JPG, JPEG, PNG, and BMP images are supported.
  6.    ocrManager.captureImage(bitmap, object, callback);
  7. }

3. In the callback of the recognition button, call the method defined in step 2 to implement general card recognition.

  1. @Override
  2. public void onClick(View v) {
  3.    switch (v.getId()) {
  4.        // Button for recognizing general cards in images.
  5.        case R.id.detect_picture:
  6.            this.startLocalImageActivity(cardImage, null, callback);
  7.            break;
  8.        // Button for recognizing general cards in camera streams.
  9.        case R.id.detect_video:
  10.            this.startCaptureActivity(null, callback);
  11.            break;
  12.        // Button for recognizing general cards in picture taking.
  13.        case R.id.detect_take_photo:
  14.            this.startTakePhotoActivity(null, callback);
  15.            break;
  16.        default:
  17.            break;
  18.        }
  19. }