Huawei Developers
Published in

Huawei Developers

HMS Scan Kit

Nowadays, many mobile services are used in the world. One of the most popular of these mobile services is Huawei Mobile Services. It was developed in HMS as well as very useful kits such as Push, Analytics, Ads, Map, Location, as well as Scan Kit. “What is this Scan Kit?” If you say, let’s begin to examine this kit with the titles below.

1.What is HMS Scan Kit? What does it do? Which Fields can be used?

Barcodes and barcode scanners are used in stores, markets, notaries, education, ticket purchases, health institutions, and even street vendors in short, almost all institutions and organizations.

Instead of writing down the website link, information letter, contact information or keeping in mind about these areas, you can access, save and use this information at any time, thanks to an application that you can install on your phone. If we approach from this perspective, QR code is one of the most effective ways to enable your application to connect with the real world and to speed up information sharing in the digital world.

If we want to develop this type of application, we need a smartphone with a camera, a mobile service that can be used in its software and provide scanning. It fully meets this need in the HMS Scan Kit. So what is this HMS Scan Kit?

HMS Scan Kit is a mobile service that can scan 1D and 2D barcodes, parse them, generate QR codes as a result of inputs, and help you quickly create barcode scanning functions for your applications.

2. What are the HMS Scan Kit Features?

13 Different Formats Supported
1D barcodes: EAN-8, EAN-13, UPC-A, UPC-E, Codabar, Code 39, Code 93, Code 128, and ITF
2D barcodes: QR code, Data Matrix, PDF417, and Aztec

Advanced Recognition Feature

The process is performed even if the scanned area is cylindrical, cloudy, bright or has low visibility. At the same time, even if the scanned plane is damaged or damaged, the scan is largely accomplished.

Auto Zoom

Scanning performance is increased by zooming in small or remote barcodes with automatic zoom feature.

Multiple Barcode Detection
It has 5 barcode processing feature at the same time.

QR code generation
The best version of input also provides QR code generation.

3. How is the HMS Scan Kit Integrated into the Project?

We need some software requirements when integrating the HMS Scan kit. These;

  • Android Studio
  • Android 4.4 (SDK versiyon 19) or later
  • Java JDK (1.8 veya sonrası)

1- In order to formally publish an application that integrates the HUAWEI Screening Kit, we first need to integrate HUAWEI HMS Core.

HUAWEI HMS Core Integration

a) Let’s log in to AppGallery Connect and choose my apps option.

Let’s find our application from the drop-down list and click. If we do not have a pre-created application, let’s create an application record with the “New App” button.

b) Let’s click the Develop-> Overview -> App Information tabs respectively. Let’s download it by clicking “agconnect-services.json” from the window that opens.

c) Let’s copy the Agconnect-services.json file we downloaded to the root of the app via Android Studio.

d) Let’s open the build.gradle file in the root directory. Let’s go to allprojects -> repositories and configure the Maven repositories address for the HMS SDK, respectively.

allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

e) Let’s add buildscript -> dependencies configurations respectively.

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
}
}

f) Let’s configure the build.gradle file in the app directory, add implementation dependencies. Let’s enter the current SDK version number instead of {version}. For example ‘com.huawei.hms: scan: 1.1.3.301’.

dependencies{
implementation 'com.huawei.hms:scan:{version}'
}

g) Let’s add AppGallery Connect plug-in dependency to App root directory.

apply plugin: 'com.huawei.agconnect'

Let’s sync the project. Let’s wait until synchronization is complete. If an error occurs, let’s check your network connection and configurations in the build.gradle file. Thus, we will have completed our HMS Core integration. In the next steps, we can move on to the HMS Scan Kit integration steps.

HMS Scan Kit integration

2- Adding Required Permissions for HMS Scan Kit

First of all, let’s add the camera permission and file read permission to our manifest file for mobile devices with old SDK version.

<!--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" />

Next, let’s add the camera and file reading permissions and the functions that receive the user to our code in mobile devices whose SDK version is up to date, which supports the “runtime permission” process.

public void newViewBtnClick(View view) {
// DEFINED_CODE 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;
}if (requestCode == DEFINED_CODE) {
// Display the barcode scanning view.
this.startActivityForResult(new Intent(this, DefinedActivity.class), REQUEST_CODE_SCAN);
}
}

3- Adding HMS Scan Kit Scanning Methods

We can integrate the HMS Scan Kit features in two different ways. The first is the default path, while the second is the custom path.

a) Let’s take a look at the first way. Scanning command is given with the StartScan command. Here we can write the 3rd parameter of the startScan method as null if you want the scanning Kit to scan all 13 supported barcodes.

ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, null);

If we want to create a specific scanning format option, we can replace the option that we have created instead of the 3rd parameter. Different format types can be selected instead of “FORMAT_ALL_FORMATS” format type. An example of this is given below.

HmsMLVisionScanDetectorOptions options = new HmsMLVisionScanDetectorOptions.Builder().setBarcodeFormats(HmsMLVisionScan.FORMAT_ALL_FORMATS).build();ScanUtil.startScan(BottomNavigationBarActivity.this, REQUEST_CODE_SCAN, options);

The user can use the camera to scan a barcode or select an image from his album. Scan result is obtained with the onActivityResult method shown in the example below.

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(resultCode!=RESULT_OK||data==null){
return;
}

if(requestCode==REQUEST_CODE_SCAN_ONE){
HmsScan obj=data.getParcelableExtra(ScanUtil.RESULT);
if(obj!=null){
// Display the decoding result.
showResult(obj);
}
}
}

b) The development manual can be consulted for details of creating a customized view to implement the HMS Scan Kit capability.

In addition to these, we can use the codes that should be in the following class in our own application in order to scan the barcode using the customized camera.

// Import all dependencies suggested by Android Studio.
private RemoteView remoteView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_defined);//1.get screen density to caculate viewfinder's rect
DisplayMetrics dm = getResources().getDisplayMetrics();
float density = dm.density;
//2.get screen size
mScreenWidth = getResources().getDisplayMetrics().widthPixels;
mScreenHeight = getResources().getDisplayMetrics().heightPixels;int scanFrameSize = (int) (SCAN_FRAME_SIZE * density);//3.caculate viewfinder's rect,it's in the middle of the layout
//set scanning area(Optional, rect can be null,If not configure,default is in the center of layout)
Rect rect = new Rect();
rect.left = mScreenWidth / 2 - scanFrameSize / 2;
rect.right = mScreenWidth / 2 + scanFrameSize / 2;
rect.top = mScreenHeight / 2 - scanFrameSize / 2;
rect.bottom = mScreenHeight / 2 + scanFrameSize / 2;//initialize RemoteView instance, and set calling back for scanning result
remoteView = new RemoteView.Builder().setContext(this).setBoundingBox(rect).setFormat(HmsScan.ALL_SCAN_TYPE).build();
remoteView.onCreate(savedInstanceState);
remoteView.setOnResultCallback(new OnResultCallback() {
@Override
public void onResult(HmsScan[] result) {if (result != null && result.length > 0 && result[0] != null && !TextUtils.isEmpty(result[0].getOriginalValue())) {
Intent intent = new Intent();
intent.putExtra(SCAN_RESULT, result[0]);
setResult(RESULT_OK, intent);
DefinedActivity.this.finish();
}
}});// Add the defined RemoteView to the page layout.
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
FrameLayout frameLayout = findViewById(R.id.rim);
frameLayout.addView(remoteView, params);
}

In addition, we can manage the life cycle.

@Override
protected void onStart() {
super.onStart();
remoteView.onStart();
}
@Override
protected void onResume() {
super.onResume();
remoteView.onResume();
}
@Override
protected void onPause() {
super.onPause();
remoteView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
remoteView.onDestroy();
}
@Override
protected void onStop() {
super.onStop();
remoteView.onStop();
}

We can add the following codes to display the Barcode Scan result.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Receive result
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK || data == null) {
return;
}
if (requestCode == REQUEST_CODE_SCAN) {
HmsScan hmsScan = data.getParcelableExtra(DefinedActivity.SCAN_RESULT);
if (hmsScan != null && !TextUtils.isEmpty(hmsScan.getOriginalValue())) {
Toast.makeText(MainActivity.this, hmsScan.getOriginalValue(), Toast.LENGTH_SHORT).show();
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store