美文网首页
简单使用Zxing API

简单使用Zxing API

作者: 在凌晨失了眠 | 来源:发表于2020-11-06 14:23 被阅读0次

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" />

相关文章

网友评论

      本文标题:简单使用Zxing API

      本文链接:https://www.haomeiwen.com/subject/restbktx.html