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 Default View 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.3.1.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.

Before calling HUAWEI Scan Kit, declare required permissions in the manifest file. Android provides two scanning permissions: CAMERA (camera permission) and READ_EXTERNAL_STORAGE (file reading permission).

In AndroidManifest.xml, add the following:

<!--Camera permission--> <uses-permission android:name="android.permission.CAMERA" /> <!--File reading permission--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />

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) { // DEFAULT_VIEW is customized for receiving the permission verification result. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { this.requestPermissions( new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, DEFAULT_VIEW); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (permissions == null || grantResults == null || grantResults.length < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) { return; } }

Kotlin sample code:

public fun newViewBtnClick(view:View) { // DEFAULT_VIEW is customized for receiving the permission verification result. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { this.requestPermissions( arrayOf(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE), DEFAULT_VIEW) } } override public fun onRequestPermissionsResult(requestCode:Int, permissions:Array<out String>, grantResults:IntArray) { if (permissions == null || grantResults == null || grantResults.length < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) { return } if (requestCode == DEFAULT_VIEW) { // Call the barcode scanning view in Default View mode. ScanUtil.startScan(this@MainActivity, REQUEST_CODE_SCAN, HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.ALL_SCAN_TYPE).create()) } }

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, Default View is used as the example to implement the scanning capability.

1. Call the barcode scanning view in Default View mode.

Java sample code:

// After the permissions are applied for, call the barcode scanning view in Default View mode. int result = ScanUtil.startScan(MainActivity.this, REQUEST_CODE_SCAN, new HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.ALL_SCAN_TYPE).create());

Kotlin sample code:

// After the permissions are applied for, call the barcode scanning view in Default View mode. ScanUtil.startScan(this@MainActivity, REQUEST_CODE_SCAN, HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.ALL_SCAN_TYPE).create())

2. Process the scanning result.

To process the barcode scanning result 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; } // Obtain the return value of HmsScan from the value returned by the onActivityResult method by using ScanUtil.RESULT as the key value. if (requestCode == REQUEST_CODE_SCAN) { Object obj = data.getParcelableExtra(ScanUtil.RESULT); if (obj instanceof HmsScan) { if (!TextUtils.isEmpty(((HmsScan) obj).getOriginalValue())) { Toast.makeText(this, ((HmsScan) obj).getOriginalValue(), Toast.LENGTH_SHORT).show(); } return; } } }

Kotlin sample code:

override protected fun 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 } // Obtain the return value of HmsScan from the value returned by the onActivityResult method by using ScanUtil.RESULT as the key value. if (requestCode == REQUEST_CODE_SCAN) { var obj:Any? = data?.getParcelableExtra(ScanUtil.RESULT) if (obj is HmsScan?) { if (!TextUtils.isEmpty(obj?.getOriginalValue())) { Toast.makeText(this, obj?.getOriginalValue(), Toast.LENGTH_SHORT).show() } return } } }

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 Default View mode:

Scanning result view:

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

  1. Integrate HUAWEI Scan Kit.
  2. Use the Default View 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