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.
In this codelab, you will create a barcode scanning app that uses the Default View mode.
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.
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.
If you have enabled certain services in AppGallery Connect, add the agconnect-services.json file to your app.
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.
buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
dependencies{
implementation 'com.huawei.hms:scan:{version}'
}
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'
}
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.
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())
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:
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:
You can click the button below to download the source code.