1.首先建一个android 项目(工具android studio)
大家可以直接去华为官网看这个demo–>华为官网的Demo和示例代码,示例代码!
第一步 这是项目的build.gradle文件
这是项目的build.gradle文件
buildscript { //脚本执行所需依赖,分别是对应的maven库和插件
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'}//maven 地址
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.huawei.agconnect:agcp:1.2.0.300'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects { //allprojects里是项目本身需要的依赖
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'} //maven 地址
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
第二步 在app的build.gradle文件中添加配置
在app的build.gradle文件中添加配置
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
android {
compileSdkVersion 28
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.jksd.app" //一般是项目的不用管
minSdkVersion 20
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.huawei.hms:scan:1.1.3.301' //这里是要加这个华为的sdk 一般华为手机就用scan 其他手机的用scanplus兼容好点
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
第三步 在项目的AndroidManifest.xml中添加权限以及扫码页面的说明!
在项目的AndroidManifest.xml中添加权限以及扫码页面的说明!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jksd.app">
<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
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<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>
第四步去MainActivity
public class MainActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback{
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 String RESULT = "SCAN_RESULT";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
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());
}
}
/**
*扫码结果的回调
*/
@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("https://"+obj.showResult);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/saoma_button"
android:layout_width="279dp"
android:layout_height="87dp"
tools:layout_editor_absoluteX="53dp"
tools:layout_editor_absoluteY="225dp"
android:text="点击扫码"
android:onClick="loadScanKitBtnClick"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
最后一步 运行项目-.-!!!!
亲测没问题。。。。。
Demo(内含官方的和本文章demo)