本项目依赖于ZXing 3.2.1
对6.0以上和第三方ROM自带的权限增加了权限判断
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<activity android:name="io.github.xudaojie.qrcodelib.CaptureActivity"/>
CaptureActivity
对二维码扫描信息默认是将扫描结果传回上一个界面,如果你要在CaptureActivity
中直接处理,可通过重写handleResult(String resultString)
实现
Intent i = new Intent(mContent, CaptureActivity.class);
startActivityForResult(i, REQUEST_QR_CODE);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK
&& requestCode == REQUEST_QR_CODE
&& data != null) {
String result = data.getStringExtra("result");
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
}
public class SimpleCaptureActivity extends CaptureActivity {
@Override
protected void handleResult(String resultString) {
if (resultString.equals("")) {
Toast.makeText(mActivity, R.string.scan_failed, Toast.LENGTH_SHORT).show();
restartPreview();
} else {
// TODO: 16/9/17 ...
// restartPreview();
}
}
}
Tips
如果你的操作不会触发SimpleCaptureActivity
的onPause
、onResume
生命周期,则需要在完成操作后,调用restartPreview()
以重新启动扫描
想引入你的项目,需要修改你的build.gradle
repositories {
maven { url "http://repo1.maven.org/maven2" }
maven { url 'https://jitpack.io' }
}
compile 'com.github.XuDaojie:QRCode-Android:v0.4.2'
ZXing
zxing扫描二维码和识别图片二维码及其优化策略
生成二维码