Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
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.
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.
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.
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:
- private MLGcrCapture.Callback callback = new MLGcrCapture.Callback() {
- // This method requires the following status codes:
- // 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.
- // MLGcrCaptureResult.CAPTURE_STOP: The returned result meets the requirements and the recognition stops.
- @Override
- public int onResult(MLGcrCaptureResult result,Object o){
- // Process the recognition result. Implement post-processing logic based on your use case to extract valid information and return the status code.
- if (result != null) {// Check whether a result is returned.
- // Recognition result processing logic.
- if (!isMatch(result)) {// Check whether the processing result meets the requirements. Implement the isMatch method based on your use case.
- return MLGcrCaptureResult.CAPTURE_CONTINUE;// The processing result does not meet the requirements, and the recognition continues.
- }
- // Process the results that meet the requirements.
- }
- return MLGcrCaptureResult.CAPTURE_STOP;// The processing ends, and the recognition exits.
- }
- @Override
- public void onCanceled(){
- // Processing for recognition request cancelation.
- }
- // Callback method used when no text is recognized or a system exception occurs during recognition.
- // retCode: result code.
- // bitmap: bank card image that fails to be recognized.
- @Override
- public void onFailure(int retCode, Bitmap bitmap){
- // Exception handling.
- }
- @Override
- public void onDenied(){
- // Processing for recognition request deny scenarios, for example, the camera is unavailable.
- }
- };
- private val callback: MLGcrCapture.Callback = object : MLGcrCapture.Callback {
- // This method requires the following status codes:
- // 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.
- // MLGcrCaptureResult.CAPTURE_STOP: The returned result meets the requirements and the recognition stops.
- override fun onResult(result: MLGcrCaptureResult, o: Any): Int {
- // Process the recognition result. Implement post-processing logic based on your use case to extract valid information and return the status code.
- if (result != null) { // Check whether a result is returned.
- // Recognition result processing logic.
- if (!isMatch(result)) { // Check whether the processing result meets the requirements. Implement the isMatch method based on your use case.
- return MLGcrCaptureResult.CAPTURE_CONTINUE // The processing result does not meet the requirements, and the recognition continues.
- }
- // Process the results that meet the requirements.
- }
- return MLGcrCaptureResult.CAPTURE_STOP // The processing ends, and the recognition exits.
- }
-
- override fun onCanceled() {
- // Processing for recognition request cancelation.
- }
-
- // Callback method used when no text is recognized or a system exception occurs during recognition.
- // retCode: result code.
- // bitmap: bank card image that fails to be recognized.
- override fun onFailure(retCode: Int, bitmap: Bitmap) {
- // Exception handling.
- }
-
- override fun onDenied() {
- // Processing for recognition request deny scenarios, for example, the camera is unavailable.
- }
- }
- private void startCaptureActivity(Object object, MLGcrCapture.Callback callback) {
- // Create a general card recognition configurator that can be used to configure languages recognized.
- MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().setLanguage("zh").create();
-
- // Create a general card recognition UI configurator.
- MLGcrCaptureUIConfig uiConfig = new MLGcrCaptureUIConfig.Factory()
- // Set the color of the scanning box.
- .setScanBoxCornerColor(Color.GREEN)
- // Set the prompt text in the scanning box. It is recommended that the text contain less than 30 characters.
- .setTipText("Recognizing, align edges")
- .create();
- // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
- MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig);
- // Method 2: Use the default UI to create a general card recognition processor.
- MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig);
- // Bind the general card recognition processor to the processing result callback function.
- ocrManager.capturePreview(this, object, callback);
- }
- private fun startCaptureActivity(`object`: Any, callback: MLGcrCapture.Callback) {
- // Create a general card recognition configurator that can be used to configure languages recognized.
- val cardConfig = MLGcrCaptureConfig.Factory().setLanguage("zh").create()
- // Create a general card recognition UI configurator.
- val uiConfig = MLGcrCaptureUIConfig.Factory() // Set the color of the scanning box.
- .setScanBoxCornerColor(Color.GREEN) // Set the tip text in the scanning box. It is recommended that the tip text contain less than 30 characters.
- .setTipText("Recognizing, align edges")
- .create()
- // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
- val ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig)
- // Method 2: Use the default UI to create a general card recognition processor.
- val ocrManager2 = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig)
- // Bind the general card recognition processor to the processing result callback function.
- ocrManager.capturePreview(this, `object`, callback)
- }
- private void startTakePhotoActivity(Object object, MLGcrCapture.Callback callback) {
- // Create a general card recognition configurator that can be used to configure languages recognized.
- MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().setLanguage("zh").create();
- // Create a general card recognition UI configurator.
- MLGcrCaptureUIConfig uiConfig = new MLGcrCaptureUIConfig.Factory()
- // Set the color of the scanning box.
- .setScanBoxCornerColor(Color.BLUE)
- // Set the prompt text in the scanning box. It is recommended that the text contain less than 30 characters.
- .setTipText("Taking picture, align edges")
- .create();
- // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
- MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig);
- // Method 2: Use the default UI to create a general card recognition processor.
- MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig);
- // Bind the general card recognition processor to the processing result callback function.
- ocrManager.capturePhoto(this, object, callback);
- }
- private fun startTakePhotoActivity(`object`: Any, callback: MLGcrCapture.Callback) {
- // Create a general card recognition configurator that can be used to configure languages recognized.
- val cardConfig = MLGcrCaptureConfig.Factory().setLanguage("zh").create()
- // Create a general card recognition UI configurator.
- val uiConfig = MLGcrCaptureUIConfig.Factory() // Set the color of the scanning box.
- .setScanBoxCornerColor(Color.BLUE) // Set the tip text in the scanning box. It is recommended that the tip text contain less than 30 characters.
- .setTipText("Taking picture, align edges")
- .create()
- // Method 1: Create a general card recognition processor based on the customized card recognition UI configurator.
- val ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig, uiConfig)
- // Method 2: Use the default UI to create a general card recognition processor.
- val ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(cardConfig)
- // Bind the general card recognition processor to the processing result callback function.
- ocrManager.capturePhoto(this, `object`, callback)
- }
- private void startLocalImageActivity(Bitmap bitmap, Object object, MLGcrCapture.Callback callback) {
- // Create a general card recognition configurator that can be used to configure languages recognized.
- MLGcrCaptureConfig cardConfig = new MLGcrCaptureConfig.Factory().setLanguage("zh").create();
- MLGcrCapture ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(config);
- // The bitmap indicates image data in bitmap format. JPG, JPEG, PNG, and BMP images are supported.
- ocrManager.captureImage(bitmap, object, callback);
- }
- private fun startLocalImageActivity(bitmap: Bitmap, `object`: Any, callback: MLGcrCapture.Callback) {
- // Create a general card recognition configurator that can be used to configure languages recognized.
- val cardConfig = MLGcrCaptureConfig.Factory().setLanguage("zh").create()
- val config: MLGcrCaptureConfig? = null
- val ocrManager = MLGcrCaptureFactory.getInstance().getGcrCapture(config)
- // The bitmap indicates image data in bitmap format. JPG, JPEG, PNG, and BMP images are supported.
- ocrManager.captureImage(bitmap, `object`, callback)
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- // Button for recognizing general cards in images.
- case R.id.detect_picture:
- this.startLocalImageActivity(cardImage, null, callback);
- break;
- // Button for recognizing general cards in camera streams.
- case R.id.detect_video:
- this.startCaptureActivity(null, callback);
- break;
- // Button for recognizing general cards in picture taking.
- case R.id.detect_take_photo:
- this.startTakePhotoActivity(null, callback);
- break;
- default:
- break;
- }
- }
- override fun onClick(v : View) {
- when (v.getId()) {
- // Button for recognizing general cards in images.
- R.id.detect_picture -> this.startLocalImageActivity(cardImage, null, callback)
- // Button for recognizing general cards in camera streams.
- R.id.detect_video -> this.startCaptureActivity(null, callback)
- // Button for recognizing general cards in picture taking.
- R.id.detect_take_photo -> this.startTakePhotoActivity(null, callback)
- }
- }