preface

A few days ago, xiaobian went out after shopping, delicified ready to drive home, the result was the garage payment qr code stopped, too far away, has been unable to identify, the owner of the car behind also urged hard, make me very irritable ah. After returning home and friends ridicule, did not get comfort, but also forced to join a two-dimensional code reflection, block large ridicule scene.

Hasn’t the scan function been used for a long time? Why are there so many questions?

Later, I came across an article about Huawei HMS Scan Kit on the Internet. Following the article, I found that APK experience can be downloaded on the website of Huawei Developer Alliance, so I quickly tried the effect. Huawei HMS Scan Kit provides powerful functions to easily solve complex code scanning scenarios such as long distance, reflection, damage, and occlusion.

Results show

Long range sweepReflective and codeDamage, block scan code

As xiaobian finds, Huawei HMS Scan Kit can also Scan at any Angle, Scan on a curved surface, and identify multiple codes at the same time. It supports 13 global mainstream codes, which can fully meet the requirements of various scenarios in life and work.

You can poke below link, under the experience: developer.huawei.com/consumer/cn…

The key comes, so good, powerful scan code service function, how to get it?

Don’t worry, xiaobian has helped you move the development process over, quickly to integrate it!

The development of actual combat

1 Configure AppGallery Connect

Before developing an application, you need to configure the relevant information in AppGallery Connect. Include:

  • Register as a developer.

  • Create an application.

Introduced here is not much, you can refer to the website operation: developer.huawei.com/consumer/cn…

2 Integrated HMS Core SDK

2.1 Adding the AppGallery Connect profile of the current application

If the related service is enabled in AppGallery Connect, you need to add the “agconnect-services.json” file to your App.

1. Log in AppGallery Connect and click “My Project”.

2. Find your project in the project list and click the application that needs to integrate the HMS Core SDK.

3. Choose Project Settings > General > Application, and click Agconnect-services. json to download the configuration file.

4. Copy the agconnect-services.json file to the application-level root directory.

2.2 Configuring the Maven repository address of the HMS Core SDK1. Open the Android Studio project-level “build.gradle” file.

2. Add the HUAWEI AGCP plug-in and Maven code base.

  • Configure the Maven repository for the HMS Core SDK in AllProjects > Repositories.
  • Configure the Maven repository address for the HMS Core SDK in “BuildScript > Repositories”.
  • If the “agconnect-services.json” file is added to the App, add the AGCP configuration in BuildScript > Dependencies.
buildscript {
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        ...
        classpath 'com. Huawei. Agconnect: agcp: 1.3.1.300'
    }
}
  
allprojects {
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}}}Copy the code

Maven store addresses cannot be opened and accessed directly in the browser. They can only be configured in the IDE. If you want to add multiple Maven code libraries, set the Maven repository address of Huawei at the end.2.3 Adding compile dependencies1. Open the Android Studio project-level “build.gradle” file.

2. Reference the SDK. Huawei Scan Kit provides two SDKS, common version and Plus version. The Plus version works better on non-Huawei phones but has a slightly larger SDK. You can select the appropriate SDK according to your requirements, and fill in the actual SDK version number.

For example, using the Scan Kit SDK, add the following in the “dependencies” compile dependencies, {version} need to be replaced for the current SDK version: implementation ‘com. Huawei. HMS: Scan: 1.2.2.300’.

dependencies{
  implementation 'com. Huawei. HMS: scan: 1.2.2.300'
 }
Copy the code

3. Add the following configuration to the apply plugin: ‘com.android.application’ header.2.4 Synchronous EngineeringAfter the above configuration is complete, click the Gradle synchronization icon in the toolbar to complete the synchronization of the “build.gradle” file and download the related dependencies to your local PC.

3 Adding Permission

1. When calling the Scan Kit, the developer needs to specify the corresponding permissions in the Manifest. To build the CAMERA scan function, you need to apply for “CAMERA” (CAMERA permission); Apply for READ_EXTERNAL_STORAGE (read file permission) to build the scan function for imported images.

<! -- Camera permission --> <uses-permission Android :name="android.permission.CAMERA"/ > <! --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/ > <! --> <uses-feature Android :name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Copy the code

2. After specifying permissions in the Manifest, you need to apply for permissions dynamically in your code.

//CAMERA_REQ_CODE is user-defined for receiving permission verification results
ActivityCompat.requestPermissions(this.new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_REQ_CODE);
Copy the code

3. Verify whether the corresponding permissions are enabled and decide whether to continue scanning codes.

/ / implementation "onRequestPermissionsResult" function to receive check permissions
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    // check whether "requestCode" is set to the requestCode CAMERA_REQ_CODE, and check whether the permission is enabled
    if (requestCode == CAMERA_REQ_CODE && grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
        // Call scan interface, build scan capability, developers need to achieve. }}Copy the code

4 Application Development

Huawei HMS Scan Kit provides two call modes: Default View mode, Customized View mode, Bitmap mode, and Multi Processor Mode. You can configure the code Scan function as required.

This section describes the customized View Mode development process. 1. Customize scan page elements. (1) Customize the title to define the text information as the “title” variable.

<TextView 
    android:layout_marginStart="10sp" 
    android:layout_toEndOf="@+id/back_img" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:text="@string/title" 
    android:textAllCaps="false" 
    android:textColor="#FFFFFF" 
    android:textSize="20sp" 
    android:textStyle="bold" />
Copy the code

(2) Customize the return button

Define a page view with id “back_img” and bind the click back action.

(3) Custom flash button

Define the page view for the flash button and bind the on/off action.

(4) Customize the code scanning interface

Draws the scan box.

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_centerInParent="true" 
        android:layout_centerHorizontal="true" 
        android:background="#FF000000" 
        android:alpha="0.1" /> 
    <TextView 
        android:layout_marginTop="225dp" 
        android:layout_centerHorizontal="true" 
        android:text="@string/scan_tip" 
        android:textAllCaps="false" 
        android:textColor="#FFFFFF" 
        android:textSize="15sp" 
        android:textStyle="bold" 
        android:layout_height="20dp" 
        android:layout_width="220dp"/ > <! <ImageView Android :layout_width="300dp" 
        android:layout_height="300dp" 
        android:layout_centerInParent="true" 
        android:layout_centerHorizontal="true" 
        android:background="@drawable/cloors" /> 
</RelativeLayout>
Copy the code

2. Use Customized View to implement the camera code sweep function. (1) Create Remote View and load it into the Activity layout. (2) Set the result callback monitor on Remote View to obtain the Scan result object HMS Scan.

@Override
protected void onCreate(Bundle savedInstanceState) {...// Identify the result callback event subscription
    remoteView.setOnResultCallback(new OnResultCallback() { 
        @Override 
        public void onResult(HmsScan[] result) { 
            // obtain the scan result HmsScanshowResult(result); }}); }Copy the code

conclusion

Relying on Huawei’s powerful capabilities in computer vision, Huawei Scan Kit supports remote code detection and automatic amplification of small codes, and effectively deals with complex code scanning scenarios such as reflection, occlusion, stain, and blur. It supports Android and iOS integration, helping developers quickly build associated services, and improving users’ code scanning experience and success rate.

Scan Kit sample code is open source on Github: github.com/HMS-Core/hm…

For more details about the development guide, see the official website of Huawei Developer Alliance

Developer.huawei.com/consumer/cn… For more details, please refer to:

Huawei developer alliance website: developer.huawei.com/consumer/cn…

To obtain development guidance document: developer.huawei.com/consumer/cn…

Participate in developer discussion please to Reddit community: www.reddit.com/r/HMSCore/

Download the demo and sample code at Github: github.com/HMS-Core

Solve the integration problem please to Stack Overflow:stackoverflow.com/questions/t…


The original link: developer.huawei.com/consumer/cn… Author: Say Hi