我正在尝试创建一个实时 QR 码解码器,它允许用户确认/取消从相机 View 读取的数据。
(当然,根据确认或取消,它会执行一些代码......)

但是有一个不好的地方:
我编写的应用程序即使在对话框中也不断读取二维码数据,我找不到阻止它的方法。

这是我的主要 Activity :

public class MainReadActivity extends Activity {


private SurfaceView cameraView;
private TextView barcodeInfo;
private BarcodeDetector barcodeDetector;
private CameraSource cameraSource;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_read);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);


    cameraView = (SurfaceView) findViewById(R.id.camera_view);

    cameraView.requestFocus();
    barcodeInfo = (TextView) findViewById(R.id.code_info);

    barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();


    cameraSource = new CameraSource.Builder(this, barcodeDetector).build();


    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {


            try {
                cameraSource.start(cameraView.getHolder());
            } catch (IOException ie) {
                Log.e("CAMERA SOURCE", ie.getMessage());
            }

        }

        @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() {
        }

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

            if (barcodes.size() != 0) {
                barcodeInfo.post(new Runnable() {    // Use the post method of the TextView
                    public void run() {

                        ConfirmationDialogFragment myDialog = new ConfirmationDialogFragment();
                        myDialog.show(getFragmentManager(),"");
                        barcodeInfo.setVisibility(View.VISIBLE);
                        barcodeInfo.setText(barcodes.valueAt(0).displayValue);
                    }
                });
            }
        }
    });



}

这是我的 DialogFragment:
public class ConfirmationDialogFragment extends DialogFragment {



public Dialog ConfirmationDialogFragment(Bundle savedInstanceState){

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.dialog_confirmation,null);
    builder.setView(v);


    Dialog dialog = builder.create();

    return dialog;
}
}

谁能帮我?

最好的,
P。

最佳答案

在barcodeDetector回调上停止和启动相机

barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {
        }

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

            if (barcodes.size() != 0) {
                cameraSource.stop();
                barcodeInfo.post(new Runnable() {    // Use the post method of the TextView
                    public void run() {

                        builder
                .setMessage("Are you sure you want to reset the count?")
                .setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        cameraSource.start(cameraView.getHolder());

                    }
                })

                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                    }
                })
                .create();
                        barcodeInfo.setVisibility(View.VISIBLE);
                        barcodeInfo.setText(barcodes.valueAt(0).displayValue);
                    }
                });
            }
        }
    });

关于android - 显示对话框时暂停二维码检测,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36538306/

10-12 04:34