使用Zxing库连续扫描QR,无法打开相机

时间:2018-10-30 13:10:26

标签: java android android-camera zxing

我正在创建一个二维码扫描应用程序,它可以连续扫描二维码并在不关闭相机的情况下在同一屏幕上显示结果。

我正在使用ZXing库,但是当我触发连续扫描活动时,它无法打开相机。我不知道这个问题。请检查,我已经在清单中添加了摄像头许可。

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:id="@+id/barcode_scanner"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_above="@+id/buttonsLayout"
        android:layout_alignParentTop="true">

    </com.journeyapps.barcodescanner.DecoratedBarcodeView>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/buttonsLayout"
        android:layout_toLeftOf="@+id/centerHorizont">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pause"
            android:onClick="pause" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Resume"
            android:onClick="resume" />
    </LinearLayout>

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:id="@+id/centerHorizont" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/centerHorizont"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/buttonsLayout"
        android:id="@+id/barcodePreview" />

</RelativeLayout>

Java代码:

public class ContinuousCaptureActivity extends Activity {
    private static final String TAG = ContinuousCaptureActivity.class.getSimpleName();
    private DecoratedBarcodeView barcodeView;
    private BeepManager beepManager;
    private String lastText;

    private BarcodeCallback callback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            if(result.getText() == null || result.getText().equals(lastText)) {
                // Prevent duplicate scans
                return;
            }

            lastText = result.getText();
            barcodeView.setStatusText(result.getText());

            beepManager.playBeepSoundAndVibrate();

            //Added preview of scanned barcode
           ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
           imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
        }

        @Override
        public void possibleResultPoints(List<ResultPoint> resultPoints) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.continuous_scan);

        barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner);
        Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
        barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
        barcodeView.decodeContinuous(callback);

        beepManager = new BeepManager(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        barcodeView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();

        barcodeView.pause();
    }

    public void pause(View view) {
        barcodeView.pause();
    }

    public void resume(View view) {
        barcodeView.resume();
    }

    public void triggerScan(View view) {
        barcodeView.decodeSingle(callback);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

这是错误消息,我得到了! Error Message

1 个答案:

答案 0 :(得分:1)

由于权限而出现问题。我已经添加了手动请求权限的代码,现在问题已解决。

感谢@Roberto Manfreda