Overview

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

HUAWEI Scan Kit automatically detects, magnifies, and identifies barcodes from a distance, and is also able to scan a very small barcode in the same way. HUAWEI Scan Kit 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.

What You Will Create

In this codelab, you will create a barcode scanning app that uses the Bitmap mode.

What You Will Learn

Hardware Requirements

  1. A computer with Android Studio installed for app development
  2. Android 4.4 (SDK version 19) or later

Software Requirements

  1. Android Studio
  2. Java JDK (1.8 or later)

If you need to officially release an app that integrates HUAWEI Scan Kit, please refer to Preparations for Integrating HUAWEI HMS Core to complete preparations.
To verify this codelab demo, you can use settings in the Sample Code. Therefore, you can skip this step.

Sample Code Download

If you are using Android Studio, you can integrate the HMS Core SDK via the Maven repository. Before you start developing an app, integrate the HMS Core SDK into your Android Studio project.

Obtaining the Configuration File Adding the AppGallery Connect Configuration File of Your App

If you have enabled certain services in AppGallery Connect, add the agconnect-services.json file to your app.

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your app project and click the app that needs to integrate the HMS Core SDK.
  3. Go to Project Setting > General information. In the App information area, download the agconnect-services.json file.
  4. Copy the agconnect-services.json file to the app's root directory of your Android Studio project.

Configuring the Maven Repository Address for the HMS Core SDK

1. Open the build.gradle file in the root directory of your Android Studio project.

2. Add the AppGallery Connect plug-in and the Maven repository.

Adding Build Dependencies

  1. Open the build.gradle file in the app directory of your project.
  2. Configure build dependencies and replace {version} with the actual SDK version number. For details about the version number index, please refer to the Version Change History. For example, the current version is implementation 'com.huawei.hms:scan:1.2.5.300'.
    dependencies{ implementation 'com.huawei.hms:scan:{version}' }
  3. Add the AppGallery Connect plugin configuration.
  4. In versions earlier than Android Studio 4.0, add the following information under apply plugin:

    apply plugin: 'com.huawei.agconnect'

    In Android Studio 4.0 or later, add the plugin configuration in the plugins block:

    plugins { ... id 'com.huawei.agconnect' }
  5. Synchronize the project. After completing the preceding configuration, click the synchronization icon on the toolbar to synchronize the build.gradle file.

1. Before calling HUAWEI Scan Kit, declare required permissions in the manifest file.

In AndroidManifest.xml, add the following:

<!--File reading permission--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2. After declaring permissions in the manifest file, you need to dynamically request the permissions in the code. In MainActivity.java, add the following:

Java sample code:

public void newViewBtnClick(View view) { // BITMAP is customized for receiving the permission verification result. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { this.requestPermissions( new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, BITMAP); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (permissions == null || grantResults == null || grantResults[0] != PackageManager.PERMISSION_GRANTED) { return; } if (requestCode == BITMAP) { // Call the system album. Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); MainActivity.this.startActivityForResult(pickIntent, REQUEST_CODE_PHOTO); } }

Kotlin sample code:

fun newViewBtnClick(view:View?) { // BITMAP is customized for receiving the permission verification result. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { this.requestPermissions( arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), BITMAP) } } override public fun onRequestPermissionsResult(requestCode:Int, permissions:Array<out String>, grantResults:IntArray) { if (permissions == null || grantResults == null || grantResults[0] != PackageManager.PERMISSION_GRANTED) { return } if (requestCode == BITMAP) { // Call the system album. val pickIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*") this@MainActivity.startActivityForResult(pickIntent, REQUEST_CODE_PHOTO) } }

HUAWEI Scan Kit provides four call modes. For details about the differences between the four modes, please refer to the Development Guide. In this codelab, Bitmap Mode is used as the example to implement the scanning capability.
1. Obtain the bitmap returned by the system album. For details about the sample code, please refer to the Development Guide.
2. Call the decodeWithBitmap API.
To call the barcode scanning function in MainActivity, add the following to the MainActivity.java file in the sample code:
Java sample code:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //receive result after your activity finished scanning super.onActivityResult(requestCode, resultCode, data); if (resultCode != RESULT_OK || data == null) { return; } if (requestCode == REQUEST_CODE_PHOTO) { // Obtain the image path. final String path = getImagePath(MainActivity.this, data); if (TextUtils.isEmpty(path)) { return; } // Obtain the bitmap from the image path. Bitmap bitmap = ScanUtil.compressBitmap(MainActivity.this, path); // Call the decodeWithBitmap method to pass the bitmap. HmsScan[] result1 = ScanUtil.decodeWithBitmap(MainActivity.this, bitmap, new HmsScanAnalyzerOptions.Creator().setHmsScanTypes(0).setPhotoMode(false).create()); // Obtain the scanning result. if (result1 != null && result1.length > 0) { if (!TextUtils.isEmpty(result1[0].getOriginalValue())) { Toast.makeText(this, result1[0].getOriginalValue(), Toast.LENGTH_SHORT).show(); } } } }

Kotlin sample code:

override protected void onActivityResult(requestCode:Int, resultCode:Int, data:Intent?) { //receive result after your activity finished scanning super.onActivityResult(requestCode, resultCode, data) if (resultCode != RESULT_OK || data == null) { return } if (requestCode == REQUEST_CODE_PHOTO) { // Obtain the image path. val path = getImagePath(this@MainActivity, data) if (TextUtils.isEmpty(path)) { return } // Obtain the bitmap from the image path. val bitmap = ScanUtil.compressBitmap(this@MainActivity, path) // Call the decodeWithBitmap method to pass the bitmap. val result1 = ScanUtil.decodeWithBitmap(this@MainActivity, bitmap, HmsScanAnalyzerOptions.Creator().setHmsScanTypes(0).setPhotoMode(false).create()) // Obtain the scanning result. if (result1 != null && result1.length > 0) { if (!TextUtils.isEmpty(result1[0].getOriginalValue())) { Toast.makeText(this, result1[0].getOriginalValue(), Toast.LENGTH_SHORT).show() } } } }

Click the run button to run the project you have created in Android Studio to generate an APK. Then install the APK on the test phone.

Entry view:

Scanning view in Bitmap mode:

Scanning result view:

Well done. You have successfully completed this codelab and learned how to:

  1. Integrate HUAWEI Scan Kit.
  2. Use the Bitmap mode of HUAWEI Scan Kit to scan barcodes.

For more information about HUAWEI Scan Kit, please visit our official website. If you encounter any problem during the development, see the FAQs.

For more information, please click the following links:

  1. Development Guide
  2. API Reference

You can click the button below to download the source code.

Download full source code

Code copied