`

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();





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics