[Huawei Developers] Mike-mei:
The latest version (6.11.0) of HMS Core Scan Kit is now available, and this article aims to share some of its exciting new features with you:

The kit adds the decode API that is available to both camera-based and image-based barcode scanning scenarios.

This API supports image data in NV21 format (which is output by your custom camera API) and multi-barcode recognition. Compared with decodeWithBitmap, an API released in an earlier version that supports only the bitmap format, the decode API saves time converting the image format and delivers a faster barcode scanning process in the camera-based mode.

The kit supports the multi-functional code.

This is a printed physical tag. To pair a phone with a device that has such a code, a user can simply use the phone to scan the code, tap the phone against the code, or get the phone close to the code.

Below is an example of such a code.

View attachment 5943189

Now let's check how to use the new decode API in different barcode scanning scenarios.

Scanning a Barcode in Camera-based Mode​

1. Obtain an image frame of the camera.

"Java"
// Convert the data array of the camera into a ByteArrayOutputStream stream. data is an instance of the byte array, and camera is an instance of android.hardware.Camera.
YuvImage yuv = new YuvImage(data, ImageFormat.NV21, camera.getParameters().getPreviewSize().width,
camera.getParameters().getPreviewSize().height, null);

"Kotlin"
// Convert the data array of the camera into a ByteArrayOutputStream stream. data is an instance of the byte array, and camera is an instance of android.hardware.Camera.
val yuv = YuvImage(data, ImageFormat.NV21, camera.getParameters().getPreviewSize().width,
camera.getParameters().getPreviewSize().height, null)

2. Convert the obtained YUV data streams to HmsScanFrame.

"Java"
HmsScanFrame frame= new HmsScanFrame(yuv);

"Kotlin"
val frame = HmsScanFrame(yuv)

3. Initialize HmsScanFrameOptions to set the supported barcode formats and set whether to use the camera-based mode or image-based mode for barcode scanning, whether to recognize a single barcode or multiple barcodes, and whether to enable barcode parsing:

setHmsScanTypes(int type): sets the supported barcode formats. Fewer formats mean faster scanning. All formats supported by Scan Kit will be scanned by default.

setPhotoMode(boolean photoMode): sets whether to use the camera-based mode or image-based mode for barcode scanning. The default value is false (camera-based mode).

setMultiMode(boolean multiMode): sets whether to recognize a single barcode or multiple barcodes. The default value is false (single barcode).

setParseResult(boolean parseResul): sets whether to enable barcode parsing. Set the parameter to false if you only need the original scanning result. In this case, the format of the recognized barcode will be returned in text via getScanType(). The default value is true (barcode parsing is enabled).

"Java"
// QRCODE_SCAN_TYPE and PDF417_SCAN_TYPE indicate that only barcodes in the QR code format and PDF-417 format are supported.
HmsScanFrameOptions option = new HmsScanFrameOptions.Creator().setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE| HmsScan.PDF417_SCAN_TYPE).setMultiMode(false).setParseResult(true).setPhotoMode(true).create();

"Kotlin"
// QRCODE_SCAN_TYPE and PDF417_SCAN_TYPE indicate that only barcodes in the QR code format and PDF-417 format are supported.
HmsScanFrameOptions option = new HmsScanFrameOptions.Creator().setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE| HmsScan.PDF417_SCAN_TYPE).setMultiMode(false).setParseResult(true).setPhotoMode(true).create()

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

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

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.

"Java"
HmsScanResult result = ScanUtil.decode(BitmapActivity.this, frame, option);
HmsScan[] hmsScans = result.getHmsScans();
// Process the parsing result when the scanning is successful.
if (hmsScans != null && hmsScans.length > 0 && !TextUtils.isEmpty(hmsScans[0].getOriginalValue())) {
// Display the scanning result.
...
}
// If the value of zoomValue is greater than 1.0, adjust the focal length by using getZoomValue() and scan the barcode again.
if (hmsScans != null && hmsScans.length > 0 && TextUtils.isEmpty(hmsScans[0].getOriginalValue()) && hmsScans[0].getZoomValue() != 1.0) {
// Set the focal length of the camera. The camera generates new bitmap data and scans again. (The convertZoomInt() function converts the magnification level to the focal length parameter that can be received and recognized by the camera.)
Camera.Parameters parameters= camera.getParameters();
parameters.setZoom(convertZoomInt(hmsScans[0].getZoomValue()));
camera.setParameters(parameters);
}

"Kotlin"
val hmsScansResult= ScanUtil.decode(this@BitmapActivity, frame, options)
val hmsScans = hmsScansResult.hmsScans
// Process the parsing result when the scanning is successful.
if (hmsScans != null && hmsScans.size > 0 && !TextUtils.isEmpty(hmsScans[0].getOriginalValue())) {
// Display the scanning result.
...
}
// If the value of zoomValue is greater than 1.0, adjust the focal length by using getZoomValue() and scan the barcode again.
if (hmsScans != null && hmsScans.size > 0 && TextUtils.isEmpty(hmsScans[0].getOriginalValue()) && hmsScans[0].getZoomValue() != 1.0) {
// Set the focal length of the camera. The camera generates new bitmap data and scans again. (The convertZoomInt() function converts the magnification level to the focal length parameter that can be received and recognized by the camera.)
var parameters= camera.getParameters()
parameters.setZoom(convertZoomInt(hmsScans[0].getZoomValue()))
camera.setParameters(parameters)
}

Scanning a Barcode in Image-based Mode​

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

"Java"
// data is of the Intent type, and data.getData is the URI of the barcode image to be scanned.
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());

"Kotlin"
// data is of the Intent type, and data.getData is the URI of the barcode image to be scanned.
val bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData())

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

Call the following methods to set optional parameters:

setHmsScanTypes(int type): sets the supported barcode formats. Fewer formats speed up scanning. All formats supported by Scan Kit will be scanned by default.

setPhotoMode(boolean photoMode): sets whether to use the camera-based mode or image-based mode for barcode scanning. In this example, set the parameter to true (image-based mode). The default value is false (camera-based mode).

setMultiMode(boolean multiMode): sets whether to recognize a single barcode or multiple barcodes. The default value is false (single barcode).

setParseResult(boolean parseResul): sets whether to enable barcode parsing. Set the parameter to false if you only need the original scanning result. In this case, the format of the detected barcode will be returned in text via getScanType(). The default value is true (barcode parsing is enabled).

"Java"
// QRCODE_SCAN_TYPE and PDF417_SCAN_TYPE indicate that only barcodes in the QR code format and PDF-417 format are supported.
HmsScanFrameOptions option = new HmsScanFrameOptions.Creator().setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE| HmsScan.PDF417_SCAN_TYPE).setMultiMode(false).setParseResult(true).setPhotoMode(true).create();

"Kotlin"
// QRCODE_SCAN_TYPE and PDF417_SCAN_TYPE indicate that only barcodes in the QR code format and PDF-417 format are supported.
HmsScanFrameOptions option = new HmsScanFrameOptions.Creator().setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE| HmsScan.PDF417_SCAN_TYPE).setMultiMode(false).setParseResult(true).setPhotoMode(true).create()

3. Call decode (a static method) of ScanUtil to initiate a barcode scanning request and obtain the scanning result object HmsScanResult. For information contained in this object, please refer to Parsing Barcodes. If you do not have specific requirements on barcode formats, set options to null.

"Java"
HmsScanResult result = ScanUtil.decode(BitmapActivity.this, frame, options);
HmsScan[] hmsScans = result.getHmsScans();
// Process the scanning result.
if (hmsScans != null && hmsScans.length > 0) {
// Display the scanning result.
...
}

"Kotlin"
val hmsScansResult= ScanUtil.decode(this@BitmapActivity, frame, option)
val hmsScans = hmsScansResult.hmsScans
// Process the scanning result.
if (hmsScans != null && hmsScans.size > 0) {
// Display the scanning result.
...
}

References​

Home page of HMS Core Scan Kit

Dev guide for HMS Core Scan Kit