如何使用视觉向条形码扫描仪添加盒子?

问题描述

在我的Android应用中,通过单击一个按钮,我打开了一个自定义AlertDialog用户可以在其中扫描条形码,AlertDialog的{​​{1}}我以SurfaceView开始CameraSource

问题是用户不清楚用打开的相机在该窗口上应该怎么做,所以我想知道是否可以添加像跟踪器那样在用户试图扫描的条形码周围画一个框的功能,或者像这样的东西。

这是构建AlertDialog时调用方法

BarcodeDetector

解决方法

第三方库可以做到这一点。我正在提供有用的代码。此库的简要说明位于 GitHub.


在gradle文件中,请执行以下给出的

//for qr code scanner
    implementation 'me.dm7.barcodescanner:zxing:1.9.13'

SimpleScannerActivity.java

public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
        setContentView(mScannerView);                // Set the scanner view as the content view
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.v(TAG,rawResult.getText()); // Prints scan results
        Log.v(TAG,rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode,pdf417 etc.)

        // If you would like to resume scanning,call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

并且不要忘记将CAMERA PERMISSION放在清单文件中

<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />