如何在android sdutio配置Zxing进行扫码功能

github开源项目(Zxing)demo

最快的调用Zxing方法

1.关联第三方库

2.调用基础的扫码

3.获取返回值

具体代码如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "wei.shm.zxingscancode"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    testCompile 'junit:junit:4.12'

    //新建项目只增加这个
    compile 'com.journeyapps:zxing-android-embedded:3.5.0'
    //需要核对的有:
    //1.备置仓库:repositories里的jcenter()
    //2.com.android.support:appcompat-v7:版本号必须大于23以上
    //3.buildToolsVersion:版本需要大于等于23.0.2,旧版本可能会导致编译错误
    //以上条件都满足则只需要添加 compile 'com.journeyapps:zxing-android-embedded:3.5.0'
}//1.默认选项启动意图
new IntentIntegrator(MainActivity.this).initiateScan(); // `this` is the current Activity
//2.获取得到的结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(this, "取消扫码", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "扫码结果:" + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

IntentIntegrator相关方法注解翻译
setCaptureActivity:设置活动类使用。它可以是任何活动,但应处理的意图额外使用这里。
setPrompt:设置一个提示显示在捕捉屏幕上,而不是使用默认。
setOrientationLocked:默认情况下,方向锁定。设置为false不锁定。
setCameraId:使用指定的相机ID。
setBeepEnabled:设置为false禁用扫描的哔哔声。
setBarcodeImageEnabled:设置为true,以便在结果意图中保存条形码图像并发送其路径。
setDesiredBarcodeFormats:设置所需的条码格式扫描。
initiateScan:启动扫描所有已知的条形码类型与默认相机。
setTimeout:启动扫描所有已知的条形码类型与默认相机。并启动计时器超时完成
createScanIntent:使用指定选项创建扫描意图。








  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值