Zxing扫描二维码

文/orgwcl90(简书作者)
原文链接:http://www.jianshu.com/p/28006c6232c0

------------------------------------------------------------------------

 

想要在项目中实现扫描二维码功能就肯定得添加Zxing的第三方库,但是在导入中,需要修改很多内容。现在将网上找到的简单实现的方法作以记录,以便日后使用。

一、

只需在build.gradle文件中添加如下内容:

repositories{
    jcenter()
}

dependencies{
    compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
    compile 'com.google.zxing:core:3.2.0'
}

然后Gradle会自动编译代码并且导入到app中。

导入完成后就是使用了:

new IntentIntegrator(this).initiateScan();//'this' is the current Activity

在Fragment中使用:

IntentIntegrator.forFragment(this).initiateScan();//'this' is the current Fragment
//If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead

自定义设置:

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan a barcode");
integrator.setCameraId(0); // Use a specific camera of the device
integrator.setBeepEnabel(false);
integrator.setBarcodeImageEnable(true);
integrator.initiateScan();

更多的设置

设置方向:

由于程序默认设置的横屏,对于大多数应用来说使用竖屏居多,因此在使用的时候需要做修改。

为了改变方向,需要创建一个新的Activity继承自CaptureActivity并且在AndroidManifest.xml文件中进行方向设置。

public class CaptureActivityAnyOrientation extends CaptureActivity{
}

<activity android:name=".CaptureActivityAnyOrientation"
    android:screenOrientation="fullSensor"
    android:stateNotNeeded="true"
    android:theme="@style/zxing_CaptureTheme"
    android:windowSoftInputMode="stateAlwaysHidden">
</activity>

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(CaptrueActivityAnyOrientation.class);
integrator.setOrientationLocked(false);
integrator.initiateScan();





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值