All Products
Search
Document Center

Advanced guide

Last Updated: May 21, 2021

This topic describes some advanced operations of using the Scan function.

Use the Scan function in the standard UI

When using the Scan function in the standard UI, you can set the start-up parameters according to the following code snippet.

  1. ScanRequest scanRequest = new ScanRequest();
  2. // Set the UI style of the scan page.
  3. scanRequest.setScanType(ScanRequest.ScanType.QRCODE); // The style of the QR code.
  4. scanRequest.setScanType(ScanRequest.ScanType.BARCODE); // The default style of the barcode.
  5. // Set the scan page title
  6. scanRequest.setTitleText("Standard Scan")
  7. // Set the prompt text under the scan window.
  8. scanRequest.setViewText("Prompt Text");
  9. // Set the prompt text for turning on the torch, for baseline 10.1.60 and later versions only.
  10. scanRequest.setOpenTorchText("Turn on the torch");
  11. // Set the prompt text for turning off the torch, for baseline 10.1.60 and later versions only.
  12. scanRequest.setCloseTorchText("Turn off the torch");
  13. // Set the code recognition type, for the baseline 10.1.60.6 and later veresions and 10.1.68.2 and later veresions.
  14. // This setting is only valid when a code is scanned directly. It is invalid when an album picture needs to be recognized.
  15. scanRequest.setRecognizeType(
  16. ScanRequest.RecognizeType.QR_CODE, // QR code
  17. ScanRequest.RecognizeType.BAR_CODE, // Barcode
  18. ScanRequest.RecognizeType.DM_CODE, // DM code
  19. ScanRequest.RecognizeType.PDF417_Code // PDF417 code
  20. ); // If not set, the first three types are scanned by default.
  21. // Set the transparent status bar (valid on Android 4.4 and later versions), for the baseline 10.1.68.15 and later versions only.
  22. scanRequest.setTranslucentStatusBar(true);
  23. // Set the Hide Album button, for the baseline 10.1.68.22 and later versions only.
  24. scanRequest.setNotSupportAlbum(true);

Use the Scan component in the custom UI


See Sample code.

Upgrade adaptation in the custom UI

Since baseline 10.1.68.5 and 10.1.60.11, the Scan SDK adds class MPScanner and related APIs to replace the original APIs such as BQCScanCallback and MaScanCallback. These two APIs were previously used for customizing the scan feature. Compared with the original APIs, MPScanner provides complete encapsulation, easy-to-use APIs, and support for more new features such as callbacks for insufficient environmental brightness. We recommend that you use MPScanner to develop custom scan pages.

If you are still using original APIs such as BQCScanCallback and MaScanCallback, you may need to adapt the following changes when you upgrade from an earlier version:

  • Version 10.1.68.22: Some APIs are added for the MaScanCallback class, BQCScanCallback class, and IOnMaSDKDecodeInfo class. You only need to handle these APIs with empty implementation, among which “false” is returned for the MaScanCallback.onMaCodeInterceptor method.
  • Version 10.1.60.6: Some APIs are added for the BQCScanCallback class. You only need to handle these APIs with empty implementation.
  • Version 10.1.60: Some APIs are added for the BQCScanCallback class. You only need to handle these APIs with empty implementation.
  • Version 10.1.20: For MaScanCallback class, the interface void onResultMa(MaScanResult maScanResult) changed to void onResultMa(MultiMaScanResult multiMaScanResult).

You can obtain MaScanResult in the following code snippet:

  1. MaScanResult maScanResult = multiMaScanResult.maScanResults[0];

API description for the custom UI

MPScanner

The settings related to the custom UI are as follows:

  1. /**
  2. * Set View to display the camera content.
  3. * It is recommended to call in the onConfiguration method of {@link MPScanListener}.
  4. *
  5. * @param textureView Customize TextureView in the scan page.
  6. */
  7. public void setDisplayView(TextureView textureView);
  8. /**
  9. * Set the area to be scanned.
  10. *
  11. * @param rect The recognized area.
  12. */
  13. public void setScanRegion(Rect rect);
  14. /**
  15. * Set the scan listener.
  16. */
  17. public void setMPScanListener(MPScanListener mpScanListener);
  18. /**
  19. * Set the listener for identifying the gray value of the image.
  20. */
  21. public void setMPImageGrayListener(MPImageGrayListener mpImageGrayListener);
  22. /**
  23. * Obtain the Camera object.
  24. *
  25. * @return Camera object.
  26. */
  27. public Camera getCamera();
  28. /**
  29. * Set the code type to be scanned.
  30. * Only valid for direct scan. Invalid for scanning a code from the bitmap.
  31. *
  32. *
  33. * @param recognizeTypes BAR_CODE Barcode;
  34. * QR_CODE QR code;
  35. * DM_CODE DM code;
  36. * PDF417_CODE PDF417 code;
  37. * If not set, the first three types are scanned by default.
  38. */
  39. public void setRecognizeType(MPRecognizeType... recognizeTypes);

The scan content related to the custom UI is as follows:

  1. /**
  2. * Open the camera and start scanning.
  3. *
  4. * Call the API when entering the page for the first time or the camera exits.
  5. */
  6. public void openCameraAndStartScan();
  7. /**
  8. * Open the camera and stop scanning.
  9. */
  10. public void closeCameraAndStopScan();
  11. /**
  12. * Start scanning.
  13. *
  14. * The camera state will not be changed. The invocation of this method takes effect only when the camera is turned on.
  15. */
  16. public void startScan();
  17. /**
  18. * Stop scanning.
  19. *
  20. * The camera state will not be changed.
  21. */
  22. public void stopScan();
  23. /**
  24. * Scan the code from the bitmap.
  25. *
  26. * @param bitmap The bitmap to be scanned.
  27. * @return: The scan result.
  28. */
  29. public MPScanResult scanFromBitmap(Bitmap bitmap);

Others:

  1. /**
  2. * Turn on or off the torch.
  3. *
  4. * @return Whether the torch is turned on after the method is called.
  5. */
  6. public boolean switchTorch();
  7. /**
  8. * Turn on the torch.
  9. */
  10. public void openTorch();
  11. /**
  12. * Turn off the torch.
  13. */
  14. public void closeTorch();
  15. /**
  16. * Play the default "beep" sound.
  17. */
  18. public void beep();
  19. /**
  20. * Release the resource.
  21. *
  22. * Call in onDestroy.
  23. */
  24. public void release();

MPScanListener

  1. /**
  2. * Scanning parameter configuration completed.
  3. */
  4. void onConfiguration();
  5. /**
  6. * Scanning starts.
  7. */
  8. void onStart();
  9. /**
  10. * Scanned.
  11. *
  12. * @param result The scan result.
  13. */
  14. void onSuccess(MPScanResult result);
  15. /**
  16. * Scan error.
  17. *
  18. * @param error The error.
  19. */
  20. void onError(MPScanError error);

MPImageGrayListener

  1. /**
  2. * Obtain the average gray value of the recognized image.
  3. *
  4. * The normal range is from 50 to 140,
  5. * When the gray value is lower or higher than the normal range, it usually means that the ambient brightness is too low or too high. The user can be prompted to turn on or off the torch.
  6. * Note: This method will be called continuously during the scan process.
  7. *
  8. * @param gray The average gray value of the image.
  9. */
  10. void onGetImageGray(int gray);

MPScanResult

  1. /**
  2. * The scan result string.
  3. */
  4. private String text;
  5. /**
  6. * The scanned code type.
  7. */
  8. private MPRecognizeType mpRecognizeType;