Android扫描二维码信息
调用华为的scanplus库做二维码扫描
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
def releaseTime() {
return new Date().format("yyyy-MM-dd",java.util.TimeZone.getTimeZone("UTC"))
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
maven { url 'http://developer.huawei.com/repo/'}
}
ext {
compileSdkVersion = 30
buildToolsVersion = "30.0.3"
minSdkVersion = 19
targetSdkVersion = 30
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2.app的build.gradle文件中添加库的依赖,文件内容如下,文件中的其他部分省略了。
implementation 'com.huawei.hms:scanplus:1.1.1.301'
apply plugin: 'com.android.application'
android {
...
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
//implementation project(path: ':Venus')
api 'com.gitee.premeditate:VenusAndroid:V1.0.22'
//二维码扫描库
implementation 'com.huawei.hms:scanplus:1.1.1.301'
}
3.在AndroidManifest.xml清单文件中声明扫描二维码的Activity和相关权限,文件内容如下。无关的内容省略。
<activity android:name="com.huawei.hms.hmsscankit.ScanKitActivity" />
<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" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.venus.mpeqi">
<!--相机权限-->
<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" />
<application
android:name="org.venus.library.app.LibraryApplication"
android:allowBackup="true"
android:icon="@drawable/icon_app"
android:label="@string/app_name"
android:roundIcon="@drawable/icon_app"
android:supportsRtl="true"
android:theme="@style/NoTitleBar">
<activity
android:name=".ui.activity.LoginActivity"
android:screenOrientation="userLandscape"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--对扫码页面进行声明-->
<activity android:name="com.huawei.hms.hmsscankit.ScanKitActivity" />
</application>
</manifest>
4.在调用扫码功能处按照如下函数执行
调用事件
扫码功能的按钮点击事件,在这里去申请扫码权限
/**
* 扫描二维码
*/
private void scan() {
requestPermission(CAMERA_REQ_CODE, 1);
}
请求权限
笔者在这里不调用动态申请权限。在设备中手动允许应用的相机权限但是仍不能扫码,所以这里必须动态申请一次相机权限。
private void requestPermission(int requestCode, int mode) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode);
}
请求权限结果
权限请求结果,如果拒绝权限就不能继续执行,允许权限就可以启动扫码界面进行扫码。
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
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());
}
}
扫码结果
扫码结果拿到的是HmsScan对象,通过打印可以看到二维码的基本数据大概是如下结构的。可以通过HmsScan对象的originalValue属性拿到二维码的主要信息进行业务处理,如果扫描的二维码与你的业务无关,可以做个简易的提示并不处理这次扫描,如果是你系统的信息就执行你的业务。
@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);
// System.out.println(new Gson().toJson(obj));
System.out.println(obj.originalValue);
try {
QRCode qrCode = new Gson().fromJson(obj.originalValue, QRCode.class);
if (!QRCode.TITLE_DEFAULT.equals(qrCode.title)) {
ToastUtil.show(this, "二维码标识信息不正确");
return;
}
queryEquipmentInfoByNumber(qrCode.number);
} catch (Exception e) {
ToastUtil.show(this, "请扫描移动便携设备二维码");
}
}
}
二维码数据结构
{
"HMS_SCAN_VERSION": 3,
"cornerPoints": [
{
"x": 691,
"y": 1080
},
{
"x": 328,
"y": 855
},
{
"x": 544,
"y": 492
},
{
"x": 915,
"y": 715
}
],
"linkUrl": {
"linkvalue": "https://u.wechat.com/EDTLAmRNzshSMzp21uQ4kBM",
"theme": ""
},
"originValueByte": [
66,
-58,
-121,
71,
71,
7,
51,
-94,
-14,
-9,
82,
-25,
118,
86,
54,
-122,
23,
66,
-26,
54,
-10,
-46,
-12,
84,
69,
68,
-60,
22,
-43,
36,
-25,
-89,
54,
-123,
52,
-41,
-89,
3,
35,
23,
85,
19,
70,
-76,
36,
-48
],
"originalValue": "https://u.wechat.com/EDTLAmRNzshSMzp21uQ4kBM",
"scanType": 1,
"scanTypeForm": 1006,
"showResult": "https://u.wechat.com/EDTLAmRNzshSMzp21uQ4kBM",
"zoomValue": 0.0
}
业务
拿到扫描结果后执行你自己的业务
private void queryEquipmentInfoByNumber(String number) {
Common.queryEquipmentByNumber(this,number);
}
5.截图
这是程序界面,点击右上角的扫码按钮,执行扫码函数
扫码界面,如果是第一次扫码会提示申请权限,选择允许就好。扫码界面出现二维码后会很快识别到二维码信息并退出。
扫描到二维码数据解析后进行业务处理,笔者这里就是扫描到系统内的设备信息并显示。