1.添加依赖
>build.gradle
implementation'com.journeyapps:zxing-android-embedded:3.0.2@aar'implementation'com.google.zxing:core:3.2.0'
2.添加权限
>AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
3.主要方法
//扫描条形码
public void onScanBarcode() {
IntentIntegrator integrator =new IntentIntegrator(MainActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("扫描条形码");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.initiateScan();
}
//扫描二维码
public void onScanQrcode() {
IntentIntegrator integrator =new IntentIntegrator(MainActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("扫描二维码");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.initiateScan();
}
//获取返回数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable 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);
}
}
4.竖屏显示
>build.gradle
implementation 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
>AndroidManifest.xml(重新注册CaptureActivity)
<activity android:name="com.journeyapps.barcodescanner.CaptureActivity" android:screenOrientation="fullSensor" tools:replace="android:screenOrientation" />
网友评论