android - 如何使用视觉将盒子添加到条码扫描仪?

标签 android qr-code barcode-scanner

在我的 Android 应用程序中,通过单击一个按钮,我打开了一个自定义 AlertDialog,用户应该能够在其中扫描条形码,AlertDialog 中有一个SurfaceView,其中我使用 BarcodeDetector 启动 CameraSource

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

这是我在构建 AlertDialog 时调用的方法:

private void initialiseDetectorsAndSources(SurfaceView surfaceView) {
        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();

        CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(1600, 1024)
                .setAutoFocusEnabled(true) //you should add this feature
                .build();

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if (ActivityCompat.checkSelfPermission(pterm.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                        cameraSource.start(surfaceView.getHolder());
                    } else {
                        ActivityCompat.requestPermissions(pterm.this, new
                                String[]{Manifest.permission.CAMERA}, 201);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                cameraSource.stop();
            }
        });


        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
                // Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();
                if (barcodes.size() != 0) {


                    alertFidelityTxt.post(new Runnable() {

                        @Override
                        public void run() {

                            if (barcodes.valueAt(0).email != null) {
                                alertFidelityTxt.removeCallbacks(null);
                                codFidelity = barcodes.valueAt(0).email.address;
                                alertFidelityTxt.setText(codFidelity);
                                toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 150);
                                fidelity.setText(codFidelity);
                                linearFidelity.setBackgroundColor(Color.parseColor("#00796B"));
                                alertFidelity.dismiss();
                            } else {
                                codFidelity = barcodes.valueAt(0).displayValue;
                                alertFidelityTxt.setText(codFidelity);
                                toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 150);
                                fidelity.setText(codFidelity);
                                linearFidelity.setBackgroundColor(Color.parseColor("#00796B"));
                                alertFidelity.dismiss();

                            }
                        }
                    });

                }
            }
        });
    }

最佳答案

这可以通过第三方库实现。我正在提供一堆有用的代码。此库的简要说明在 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 放入您的 list 文件中

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

关于android - 如何使用视觉将盒子添加到条码扫描仪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64677956/

相关文章:

android - 在 Android Kotlin 中使文本一个接一个地出现(又名打字机效果)

android - 从硬件设备执行时如何查看代码错误

Android gridview根据动态数字布局其 subview

testing - 用于评估二维码生成质量的工具/测试

ios - 使用 react-native 在 IOS 中扫描 GS1 Databar 条形码

android - 具有相对输出路径的 greenDAO 模式生成;未找到 i/o 失败

php - Google 身份验证器扫描时条形码无效

react-native - React Native如何像QR码标记一样只在广场的角落制作边框

c++ - Zebra KBTools MC92N0 键盘重新映射 C++ -- "error.h"没有这样的文件或目录

Android:如何将通过 USB 连接的外部条码扫描器设备集成到 Android 应用程序