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

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core GuidesScan KitClient DevelopmentBuilding Scanning CapabilitiesBitmap

Bitmap

In Bitmap mode, barcodes can be scanned using the camera or from images, which you can specify when calling the scanning API. If you choose to scan barcodes using the camera, camera control capabilities required for scanning should be developed by yourself. For the two barcode scanning ways, Scan Kit provides optimized scanning algorithms. Choosing the one that suits your needs best will provide you with the best experience.

1. Scanning Barcodes Using the Camera

Service Process

The service process of scanning barcodes using the camera in Bitmap 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 permission.

3. If the app has the permission, call the scanning API decodeWithBitmap of the HMS SDK.

4. Return the scanning result.

5. Check whether the scanning result is valid. If it is valid, start processing the result. If zoomValue is greater than 1.0, the barcode detected is too small. Adjust the focal length accordingly to scan the barcode again.

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

Development Process

1. Obtain an image frame of the camera.

Collapse
Word wrap
Dark theme
Copy code
  1. YuvImage yuv = new YuvImage(data, ImageFormat.NV21, camera.getParameters().getPreviewSize().width,
  2. camera.getParameters().getPreviewSize().height, null);
  3. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  4. yuv.compressToJpeg(new Rect(0, 0, camera.getParameters().getPreviewSize().width,
  5. camera.getParameters().getPreviewSize().height), 100, stream);

2. Convert the camera data into bitmap data.

Collapse
Word wrap
Dark theme
Copy code
  1. Bitmap bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0,stream.toByteArray().length);

3. Initialize HmsScanAnalyzerOptions, set your desired barcode formats, and set the bitmap mode to camera-based barcode scanning.

   The following two parameters are optional. Set them as needed.

  • setHmsScanTypes(): Set your desired barcode formats. Scan Kit with only a few barcode formats supported will scan faster. Barcode Formats Supported by HUAWEI Scan Kit will be scanned by default.

  • setPhotoMode(false): Set the bitmap mode. The default mode is camera-based scanning. Here, use the default setting.

Collapse
Word wrap
Dark theme
Copy code
  1. // QRCODE_SCAN_TYPE and DATAMATRIX_SCAN_TYPE are set for the barcode format, indicating that Scan Kit will support only QR Code and Data Matrix.
  2. HmsScanAnalyzerOptions options = new HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE, HmsScan.DATAMATRIX_SCAN_TYPE).setPhotoMode(false).create();

4. Call the static method decodeWithBitmap of ScanUtil to initiate a barcode scanning request and obtain the scanning result object HmsScan. For the information contained in this object, please refer to Parsing Barcodes.

  • If you do not have specific barcode formats to set, set options to null here.

  • If the barcode detected is too small, Scan Kit will return an instruction to the app for adjusting the camera's focal length to obtain a clearer barcode image.

Collapse
Word wrap
Dark theme
Copy code
  1. HmsScan[] hmsScans = ScanUtil.decodeWithBitmap(BitmapActivity.this, bitmap, options);  
  2. // Process the decoding result when the scanning is successful.
  3. if (hmsScans != null && hmsScans.length > 0 && !TextUtils.isEmpty(hmsScans[0].getOriginalValue())) {  
  4. // Display the scanning result.
  5.    showResult(hmsScans);
  6. }
  7. // If zoomValue is greater than 1.0, adjust the focal length using getZoomValue() and scan the barcode again.
  8. if (hmsScans != null && hmsScans.length > 0 && TextUtils.isEmpty(hmsScans[0].getOriginalValue()) && hmsScans[0].getZoomValue() != 1.0) {
  9. // Set the focal length of the camera. The camera generates new bitmap data and scans again. (The convertZoomInt() function is used to convert the magnification level to the value that can be received and recognized by the camera.)
  10.    params = camera.getParameters();
  11.    params.setZoom(convertZoomInt(hmsScans[0].getZoomValue()));
  12.    camera.setParameters(params);
  13. }

Sample code for implementing the convertZoomInt() function:

Collapse
Word wrap
Dark theme
Copy code
  1. public int convertZoomInt(double zoomValue) {    
  2.    List<Integer> allZoomRatios = parameters.getZoomRatios();    
  3.    float maxZoom = Math.round(allZoomRatios.get(allZoomRatios.size() - 1) / 100f);    
  4.    if (zoomValue >= maxZoom) {
  5.        return allZoomRatios.size() - 1;    
  6.    }
  7.    for (int i = 1; i < allZoomRatios.size(); i++) {
  8.        if (allZoomRatios.get(i) >= (zoomValue * 100) && allZoomRatios.get(i - 1) <= (zoomValue * 100)) {
  9.            return i;        
  10.        }    
  11.    }
  12.    return -1;
  13. }

2. Scanning QR Codes from Images

Service Process

The service process of scanning barcodes from images in Bitmap 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 file reading permission.

3. If the app has the permission, call the scanning API decodeWithBitmap of the HMS SDK.

4. Return the scanning result.

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

Development Process

1. Obtain an image and convert it into bitmap data.

Collapse
Word wrap
Dark theme
Copy code
  1. Bitmap bitmap = MediaStore.Images.Media.getBitmap(BitmapActivity.this.getContentResolver(), data.getData());

2. Initialize HmsScanAnalyzerOptions, set your desired barcode formats, and set the bitmap mode to image-based barcode scanning.

   The following two parameters are optional. Set them as needed.

  • setHmsScanTypes(): Set your desired barcode formats. Scan Kit with only a few barcode formats supported will scan faster. Barcode FormatsSupported by HUAWEI Scan Kit will be scanned by default.

  • setPhotoMode(true): It specifies the bitmap mode. The default mode is camera-based scanning. Here, image-based barcode scanning is required, so the value is set to true.

Collapse
Word wrap
Dark theme
Copy code
  1. // QRCODE_SCAN_TYPE and DATAMATRIX_SCAN_TYPE are set for the barcode format, indicating that Scan Kit will support only QR Code and Data Matrix.
  2. HmsScanAnalyzerOptions options = new HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE, HmsScan.DATAMATRIX_SCAN_TYPE).setPhotoMode(true).create();

3. Call the static method decodeWithBitmap of ScanUtil to initiate a barcode scanning request and obtain the scanning result object HmsScan. For the information contained in this object, please refer to Parsing Barcodes. 

    If you do not have specific barcode formats to set, set options to null here.

Collapse
Word wrap
Dark theme
Copy code
  1. HmsScan[] hmsScans = ScanUtil.decodeWithBitmap(BitmapActivity.this, bitmap, options);  
  2. // Process the scanning result.
  3. if (hmsScans != null && hmsScans.length > 0) {  
  4. // Display the scanning result.
  5.    showResult(hmsScans);
  6. }


This page may contain third-party content. For details, click here.