目录

In Customized View mode, you do not need to worry about developing the scanning process or camera control. Scan Kit will do all these tasks for you. However, you will need to design the scanning UI. This can also be easily complete based on sample code.

Service Process

The process of using Scan Kit in Customized View mode is as follows:

1. A user starts an app and sends a request of scanning a barcode.

2. Check whether the app has the camera access and file reading permissions.

3. Initialize the customized view of the app to build the scanning UI and start scanning.

4. If the scanning is successful, the HMS SDK calls the onResult API of the app to pass the scanning result.

5. The app encapsulates the scanning result and sends it to the user.

Development Process

Customizing the Elements on the Scanning UI

For details, please refer to Custom Elements for the Scanning UI in Customized View Mode.

Developing the Function of Scanning Barcodes Using the Camera

1. Create a RemoteView and load it to the activity layout.

   a. Bind the camera preview screen to the RemoteView and set the scanning area. In the preview stream, Scan Kit identifies only the image in the scanning area. To better magnify the captured image, it is recommended that the scanning box be placed in the middle of the screen and be slightly smaller than the scanning area.

   b. Set the barcode formats you want Scan Kit to support, initialize the remote view, and set UI elements.

      Scan Kit that supports only certain barcode formats will scan faster. For example, if only QR Code and Data Matrix need to be detected, use the setFormat(HmsScan.QRCODE_SCAN_TYPE, HmsScan.DATAMATRIX_SCAN_TYPE) method to set Scan Kit to support only the two types of barcodes. If scanning barcodes in all Barcode Formats Supported by HUAWEI Scan Kit supported by Scan Kit is acceptable for you, you do not need to call the setFormat method to specify the barcode format.

   c. Load the RemoteView to the activity. The sample code is as follows:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3.    super.onCreate(savedInstanceState);
  4.    setContentView(R.layout.activity_defined);
  5.    // Bind the camera preview layout.
  6.    frameLayout = findViewById(R.id.rim);
  7.    // Set the scanning area. Set the parameters as required.
  8.    DisplayMetrics dm = getResources().getDisplayMetrics();
  9.    float density = dm.density;
  10.    mScreenWidth = getResources().getDisplayMetrics().widthPixels;
  11.    mScreenHeight = getResources().getDisplayMetrics().heightPixels;
  12.    // Set the width and height of the barcode scanning box to 300 dp.
  13.    final int SCAN_FRAME_SIZE = 300;
  14.    int scanFrameSize = (int) (SCAN_FRAME_SIZE * density);
  15.    Rect rect = new Rect();
  16.    rect.left = mScreenWidth / 2 - scanFrameSize / 2;
  17.    rect.right = mScreenWidth / 2 + scanFrameSize / 2;
  18.    rect.top = mScreenHeight / 2 - scanFrameSize / 2;
  19.    rect.bottom = mScreenHeight / 2 + scanFrameSize / 2;
  20.    // Initialize the remote view. Use setContext() to pass the context (mandatory), use setBoundingBox() to set the scanning area, and use setFormat() to set the barcode format. Then call the build() method to create the remote view.
  21.    remoteView = new RemoteView.Builder().setContext(this).setBoundingBox(rect).setFormat(HmsScan.QRCODE_SCAN_TYPE, HmsScan.DATAMATRIX_SCAN_TYPE).build();
  22.    // Load the customized view to the activity.
  23.    remoteView.onCreate(savedInstanceState);
  24.    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
  25.    frameLayout.addView(remoteView, params);
  26. }

2. Set a result callback listener for the RemoteView to obtain the HmsScan object. For the information contained in this object, please refer to Parsing Barcodes.

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3.    ...
  4.    // Subscribe to the identification result callback event.
  5.    remoteView.setOnResultCallback(new OnResultCallback() {  
  6.        @Override  
  7.        public void onResult(HmsScan[] result) {  
  8.            // Obtain the scanning result object HmsScan.
  9.            showResult(result);  
  10.        }  
  11.    });
  12. }

3. Set a lifecycle listener for the remote view.

  1. @Override
  2. protected void onStart() {
  3.    super.onStart();
  4.    remoteView.onStart();
  5. }
  6. @Override
  7. protected void onResume() {
  8.    super.onResume();
  9.    remoteView.onResume();
  10. }
  11. @Override
  12. protected void onPause() {
  13.    super.onPause();
  14.    remoteView.onPause();
  15. }
  16. @Override
  17. protected void onStop() {
  18.    super.onStop();
  19.    remoteView.onStop();
  20. }
  21. @Override
  22. protected void onDestroy() {
  23.    super.onDestroy();
  24.    remoteView.onDestroy();
  25. }

4. Set other optional listeners for the RemoteView.

  1. protected void onCreate(Bundle savedInstanceState) {
  2.    ...
  3. // Call back this API under dim lighting. The sample code is used to control the flashlight button.
  4.    flushBtn = findViewById(R.id.flush_btn);
  5.    remoteView.setOnLightVisibleCallback(new OnLightVisibleCallBack() {
  6.        @Override
  7.        public void onVisibleChanged(boolean visible) {
  8.            if(visible){
  9.                flushBtn.setVisibility(View.VISIBLE);
  10.            }  
  11.        }  
  12.    });
  13.    ...
  14. }

5. (Optional) Pause or resume barcode scanning. 

    a. After successfully scanning a barcode, if you want to perform other operations without ending the current activity, call the API for pausing barcode scanning.

  1. remoteView.pauseContinuouslyScan();

    b. If you want to scan again, call the API for resuming barcode scanning. The activity will start receiving the scanning result again.

  1. remoteView.resumeContinuouslyScan();
Note:

Pausing barcode scanning will reset the camera's focal length. For example, if the focal length is adjusted to zoom in, it will be restored to the initial state after the barcode scanning is paused and then resumed.

6. If you want to add the function of scanning barcodes from images, use the Bitmap.

   a. Set the button for going to the album.

   b. Develop the process of scanning barcodes from images in Bitmap mode. For details, please refer to Scanning QR Codes from Images.