Zxing扫描二维码的简单实现
- 先导入依赖
- 添加动态权限
- application注册
生成二维码
//导入依赖
implementation 'cn.yipianfengye.android:zxing-library:2.2' //Zxing依赖
//权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
//application注册
//在manifests也要注册
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ZXingLibrary.initDisplayOpinion(this);
}
}
//添加动态权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
Manifest.permission.VIBRATE,
Manifest.permission.CAMERA}, 102);
}
//代码实现
Intent intent = new Intent(Main2Activity.this, CaptureActivity.class);
startActivityForResult(intent, 102);
//onActivityResult回调
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
if (data != null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
int result_type = bundle.getInt(CodeUtils.RESULT_TYPE);
if (result_type== CodeUtils.RESULT_SUCCESS) {
String result_Str = bundle.getString(CodeUtils.RESULT_STRING);
Toast.makeText(this, "" + result_Str, Toast.LENGTH_SHORT).show();
}
}
}
}
}