androidstudio基于华为hms scankit实现二维码扫码功能
首先在根目录bulid.gradle添加:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript{
repositories{
google()
jcenter()
maven{url'https://developer.huawei.com/repo/'}
}
dependencies{
classpath"com.android.tools.build:gradle:4.1.1"
classpath'com.huawei.agconnect:agcp:1.2.1.301'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects{
repositories{
google()
jcenter()
maven{url'https://developer.huawei.com/repo/'}
}
}
task clean(type: Delete){
deleterootProject.buildDir
}
在当前工程的bulid.gradle添加:
applyplugin:'com.huawei.agconnect'
dependencies{
implementation'com.huawei.hms:scan:1.2.5.300'
}
在manifest文件添加:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission><!--改变无线网状态-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission><!--访问无线网状态-->
<uses-permission android:name="android.permission.CAMERA" /><!--相机权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--从图库获取照片或保存-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" /><!-- 震动-->
再在application里面添加:
<activity android:name="com.huawei.hms.hmsscankit.ScanKitActivity" />
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".scan">
android:text="二维码"
android:layout_width="279dp"
android:layout_height="87dp"
tools:layout_editor_absoluteX="53dp"
tools:layout_editor_absoluteY="225dp"
android:onClick="loadScanKitBtnClick"
tools:ignore="MissingConstraints,OnClick"
android:id="@+id/saoma_button"/>
</RelativeLayout>
java文件:
package com.example.smartcommunityv2;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions;
public class scanextends AppCompatActivity {
public static final int CAMERA_REQ_CODE =111;
public static final int DECODE =1;
public static final int GENERATE =2;
private static final int REQUEST_CODE_SCAN_ONE =0X01;
public static final StringRESULT ="SCAN_RESULT";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
}
public void loadScanKitBtnClick(View view) {
requestPermission(CAMERA_REQ_CODE, DECODE);
}
/**
* 申请权限
*/
private void requestPermission(int requestCode, int mode) {
if (mode ==DECODE) {
decodePermission(requestCode);
}
else if (mode ==GENERATE) {
generatePermission(requestCode);
}
}
/**
* 申请权限
*/
private void decodePermission(int requestCode) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE},requestCode);
}
private void generatePermission(int requestCode) {
System.out.println("3");
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
requestCode);
}
/**
* Call back the permission application result. If the permission application is successful, the barcode scanning view will be displayed.
回调权限应用程序结果。如果权限申请成功,将显示条形码扫描视图。
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//默认扫码格式
// if (requestCode == CAMERA_REQ_CODE) {
// ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, new HmsScanAnalyzerOptions.Creator().create());
// }
if (permissions ==null || grantResults ==null) {
return;
}
if (grantResults.length <2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
return;
}
if (requestCode ==CAMERA_REQ_CODE) {
ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, new HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE).create());
}
}
/**
*扫码结果的回调
*/
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode ==REQUEST_CODE_SCAN_ONE) {
HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
Uri uri = Uri.parse(obj.showResult);
Intent intent =new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Toast.makeText(this,"小天才成功了",Toast.LENGTH_SHORT).show();
}
}
}