Android扫一扫Zxing之基础依赖

Android扫一扫Zxing

这个是最基础的Zxing的依赖扫一扫,Zxing设置的扫描是横屏,之后的文章会写篇自定义的Zxing扫一扫

一、在根目录的build.gradle中添加依赖

allprojects {
    repositories {
        jcenter()
        maven{ url "http://dl.bintray.com/journeyapps/maven" }
    }
}

二、在主项目的build.gradle中添加依赖,并同步gradle

compile 'com.google.zxing:core:3.0.1'
compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'
compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'
compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'

三、在清单文件中添加权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

注:"android.permission.CAMERA"Android6.0以上系统中要手动获取参考:Android6.0以上系统获取权限

四、跳转到扫描界面

//跳转到扫描界面
IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this);
intentIntegrator.initiateScan();

五、重写回调,解码扫描结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (scanResult != null) {
        String result = scanResult.getContents();
        Log.e("LKing","扫描结果:"+result);
    }
}

 

你可能感兴趣的:(Android)