We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesML KitAndroidApp DevelopmentText-related ServicesGeneral Card Recognition

General Card Recognition

Service Introduction

The general card recognition service provides a universal development framework based on the text recognition technology. It allows you to customize the post-processing logic to extract required information from any fixed-format cards, such as Exit-Entry Permit for Traveling to and from Hong Kong and Macao, Hong Kong identity card, and Mainland Travel Permit for Hong Kong and Macao Residents. Currently, it provides three types of APIs for general card recognition from camera streams, picture taking, and local images, and also supports recognition UI customization to meet your requirements in diversified scenarios.

Use Cases

Post-processing for detection results can be customized to recognize desired objects, such as Exit-Entry Permit for Traveling to and from Hong Kong and Macao, Taiwan Entry and Exit Permit, driving license, and vehicle license, and extract their information in the original structure. If your app needs to collect such information, it is an ideal choice to integrate the general card recognition service, making information input automatic and delivering a better user experience. For example, when a user books a hotel or flight ticket outside the Chinese mainland, this service can quickly recognize the relevant card of the user, realizing efficient information input.

Precautions

After integrating the general card recognition service, implement post-processing logic based on the to-be-recognized card types. For your reference, the sample code shows the post-processing logic for the Exit-Entry Permit for Traveling to and from Hong Kong and Macao, Hong Kong identity card, and Mainland Travel Permit for Hong Kong and Macao Residents.

Development Process

Before app development, you need to make necessary development preparations, configure the Maven repository address for the HMS Core SDK, and integrate the general card recognition plugin SDK.

You can call the general card recognition plugin 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 the 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 when, for example, the camera is unavailable.
    Java
    Kotlin
    Collapse
    Word wrap
    Dark theme
    Copy code
    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 result,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 (result != null) {// Check whether a result is returned.
    9. // Recognition result processing logic.
    10. if (!isMatch(result)) {// 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: result code.
    23. // bitmap: bank 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. };
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. private val callback: MLGcrCapture.Callback = object : 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 fun onResult(result: MLGcrCaptureResult, o: Any): Int {
    6. // Process the recognition result. Implement post-processing logic based on your use case to extract valid information and return the status code.
    7. if (result != null) { // Check whether a result is returned.
    8. // Recognition result processing logic.
    9. if (!isMatch(result)) { // Check whether the processing result meets the requirements. Implement the isMatch method based on your use case.
    10. return MLGcrCaptureResult.CAPTURE_CONTINUE // The processing result does not meet the requirements, and the recognition continues.
    11. }
    12. // Process the results that meet the requirements.
    13. }
    14. return MLGcrCaptureResult.CAPTURE_STOP // The processing ends, and the recognition exits.
    15. }
    16. override fun onCanceled() {
    17. // Processing for recognition request cancelation.
    18. }
    19. // Callback method used when no text is recognized or a system exception occurs during recognition.
    20. // retCode: result code.
    21. // bitmap: bank card image that fails to be recognized.
    22. override fun onFailure(retCode: Int, bitmap: Bitmap) {
    23. // Exception handling.
    24. }
    25. override fun onDenied() {
    26. // Processing for recognition request deny scenarios, for example, the camera is unavailable.
    27. }
    28. }
  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.
    • Camera stream-based
      Java
      Kotlin
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. private void startCaptureActivity(Object object, MLGcrCapture.Callback callback) {
      2. // Create a general card recognition configurator that can be used to configure languages recognized.
      3. MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().setLanguage("zh").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 scanning box. It is recommended that the text contain less than 30 characters.
      9. .setTipText("Recognizing, align edges")
      10. .create();
      11. // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
      12. MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig);
      13. // Method 2: Use the default UI to create a general card recognition processor.
      14. MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig);
      15. // Bind the general card recognition processor to the processing result callback function.
      16. ocrManager.capturePreview(this, object, callback);
      17. }
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. private fun startCaptureActivity(`object`: Any, callback: MLGcrCapture.Callback) {
      2. // Create a general card recognition configurator that can be used to configure languages recognized.
      3. val cardConfig = MLGcrCaptureConfig.Factory().setLanguage("zh").create()
      4. // Create a general card recognition UI configurator.
      5. val uiConfig = MLGcrCaptureUIConfig.Factory() // Set the color of the scanning box.
      6. .setScanBoxCornerColor(Color.GREEN) // Set the tip text in the scanning box. It is recommended that the tip text contain less than 30 characters.
      7. .setTipText("Recognizing, align edges")
      8. .create()
      9. // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
      10. val ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig)
      11. // Method 2: Use the default UI to create a general card recognition processor.
      12. val ocrManager2 = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig)
      13. // Bind the general card recognition processor to the processing result callback function.
      14. ocrManager.capturePreview(this, `object`, callback)
      15. }
    • Picture taking-based
      Java
      Kotlin
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. private void startTakePhotoActivity(Object object, MLGcrCapture.Callback callback) {
      2. // Create a general card recognition configurator that can be used to configure languages recognized.
      3. MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().setLanguage("zh").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 scanning box. It is recommended that the text contain less than 30 characters.
      9. .setTipText("Taking picture, align edges")
      10. .create();
      11. // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
      12. MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig);
      13. // Method 2: Use the default UI to create a general card recognition processor.
      14. MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig);
      15. // Bind the general card recognition processor to the processing result callback function.
      16. ocrManager.capturePhoto(this, object, callback);
      17. }
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. private fun startTakePhotoActivity(`object`: Any, callback: MLGcrCapture.Callback) {
      2. // Create a general card recognition configurator that can be used to configure languages recognized.
      3. val cardConfig = MLGcrCaptureConfig.Factory().setLanguage("zh").create()
      4. // Create a general card recognition UI configurator.
      5. val uiConfig = MLGcrCaptureUIConfig.Factory() // Set the color of the scanning box.
      6. .setScanBoxCornerColor(Color.BLUE) // Set the tip text in the scanning box. It is recommended that the tip text contain less than 30 characters.
      7. .setTipText("Taking picture, align edges")
      8. .create()
      9. // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
      10. val ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig)
      11. // Method 2: Use the default UI to create a general card recognition processor.
      12. val ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig)
      13. // Bind the general card recognition processor to the processing result callback function.
      14. ocrManager.capturePhoto(this, `object`, callback)
      15. }
    • Image-based
      Java
      Kotlin
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. private void startLocalImageActivity(Bitmap bitmap, Object object, MLGcrCapture.Callback callback) {
      2. // Create a general card recognition configurator that can be used to configure languages recognized.
      3. MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().setLanguage("zh").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. }
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. private fun startLocalImageActivity(bitmap: Bitmap, `object`: Any, callback: MLGcrCapture.Callback) {
      2. // Create a general card recognition configurator that can be used to configure languages recognized.
      3. val cardConfig = MLGcrCaptureConfig.Factory().setLanguage("zh").create()
      4. val config: MLGcrCaptureConfig? = null
      5. val ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(config)
      6. // The bitmap indicates image data in bitmap format. JPG, JPEG, PNG, and BMP images are supported.
      7. ocrManager.captureImage(bitmap, `object`, callback)
      8. }
  3. In the callback of the recognition button, call the method defined in step 2 to implement general card recognition.
    Java
    Kotlin
    Collapse
    Word wrap
    Dark theme
    Copy code
    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. }
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. override fun onClick(v : View) {
    2. when (v.getId()) {
    3. // Button for recognizing general cards in images.
    4. R.id.detect_picture -> this.startLocalImageActivity(cardImage, null, callback)
    5. // Button for recognizing general cards in camera streams.
    6. R.id.detect_video -> this.startCaptureActivity(null, callback)
    7. // Button for recognizing general cards in picture taking.
    8. R.id.detect_take_photo -> this.startTakePhotoActivity(null, callback)
    9. }
    10. }
Search in Guides
Enter a keyword.