Create and Scan QR Codes With HMS Scan Kit

Bekir Yavuz Koc
Huawei Developers
Published in
5 min readSep 24, 2020

HUAWEI Scan Kit scans and parses all major 1D and 2D barcodes and generates QR codes, helping quickly build barcode scanning functions into apps.

Scan Kit automatically detects, magnifies, and recognizes barcodes from a distance, and is also able to scan a very small barcode in the same way. It works even in suboptimal situations, such as under dim lighting or when the barcode is reflective, dirty, blurry, or printed on a cylindrical surface. This leads to a higher scanning success rate, and an improved user experience. Scan Kit can be integrated into both Android and iOS systems. The Android system supports barcode scanning in landscape mode.

Requirements to use Scan Kit:

To able to use Hms Scan Kit, you need to follow some instructions.

1- Register as a Huawei Developer

To use these kits, register to Huawei website. You can find information in this link → https://developer.huawei.com/consumer/en/doc/10104

2- Generate a Signing Certificate Fingerprint

In AppGallery Connect, press General Information button.

In below this page, there is a SHA-256 certificate fingerprint area box.

To fill this area, you need to open a new project in Android Studio and press Gradle button in the top right corner. In tasks, android and double clicking to signingReport will generate this SHA-256 key.

This key can be seen in console. After you generated this key, you need to copy and paste to the SHA-256 certificate fingerprint.

Also, copy the signature file generated in Generating a Signing Certificate Fingerprint to the app directory of your project and configure the signature in the build.gradle file.

4- Downloading agconnect-services.json file

In General Information tab, below the page you can see the agconnect-services.json file. In order to use these kits, you need to download this file.

After downloading agconnect-services.json file, paste the to the app root directory.

5- Adding Dependencies

Configure of the Maven repository address for the HMS SDK is required for use Huawei Scan Kit.

AppGallery Connect plug-in dependency has to be added to the file.

Add Scan Kit dependency in build.gradle file in app directory.

After all these steps we are ready to use HMS Scan Kit in our apps.

Development Process:

In this article, we are going to create and read a QR Code with Huawei Scan Kit. We are going to use bitmaps in order to save the QR Code image to the gallery and read QR code value from that image. We need only one activity to do the entire development process. Firstly, we need to add permissions request to the Manifest file.

Add permissions to the Manifest File:

XML Structure of Main Activity:

We have only two buttons in this app. First button will create QR Code, the second button will scan the QR Code. onClick attributes are set in the XML.

Main Activity Layout

Kotlin File of the MainActivity:

In this activity, when the buttons are clicked, read(newViewBtnClick) or write(saveImageBitmapClick) permissions are requested. If the permissions are granted, Scan Kit can use its ability.

In onRequestPermissionsResult method, reading or writing requests are handled. If the request code is equal to BITMAP, reading will be operated and Scan Kit will scan the image that is selected. If the request code is equal to BitmapSave, Scan Kit will generate a QR Code inside of an album(will create and album if it is not created).

A Toast message will appear when QR Code is created

In Bitmap Save request, a QR code is created with “Test QR” content and this QR code has 400 width and 400 height. Inside of try-catch, qrBitmap is created by using ScanUtil.buildBitmap method. saveToGallery method is used for saving the QR to the Gallery. In this example, “HuaweiScanKitAlbum” will be generated in the device gallery.

saveToGallery Method:

saveTheGalleryMethod requires context, bitmap and a string that determines and creates the album name in the user device gallery. After this step, you can check and see that QR code is generated in your album.

After clicking the Scan QR Code button and selecting the QR Code image, onActivityResult will be returned.

Generated QR Code

onActivityResult Method:

In onActivityResult to get the path, getImagePath method is used in line 9. To Compress bitmap with ScanUtil.compressBitmap method, path is required. If the result is not null or empty, QR code value will be read. In this example “TEST QR” will be read due to its content.

getImagePath Methods:

This methods will be used for getting album image path to be used in ScanUtil.compressBitmap.

Full Code of MainActivity.kt:

As it can be seen clearly, custom QR codes can be generated and can be scanned very easily. There is a link to the github of this project in reference area, if you are interested.

With HMS Scan Kit you can also use your camera to scan barcodes. Detailed information can be find in reference.

Reference:

Scan Kit

Scan Kit Documentation

Github

Scan Kit Default View Mode

Scan Kit Customized View Mode

Scan Kit Bitmap Mode

--

--