首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扫描二维码后如何停止?

扫描二维码后如何停止?
EN

Stack Overflow用户
提问于 2017-03-14 15:21:28
回答 1查看 2K关注 0票数 1

我正在使用com.google.android.gms.vision依赖来扫描二维码。我面临的问题是它一次又一次地扫描二维码。我要它只扫描一次。

下面是我的代码:

代码语言:javascript
复制
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera_view);
    final TextView barcodeInfo = (TextView) findViewById(R.id.code_info);


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

    cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .setAutoFocusEnabled(true)
            .setRequestedPreviewSize(640, 640)
            .build();

    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                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() {
                        barcodeInfo.setText(    // Update the TextView
                                barcodes.valueAt(0).displayValue
                        );
                         QRData = barcodeInfo.getText().toString().trim();
                        Log.d("TAG", barcodeInfo.getText().toString().trim());


                    }
                });
            }


        }
    });
EN

回答 1

Stack Overflow用户

发布于 2017-03-14 15:37:14

只需设置结果并在该活动中完成扫描时调用finish。在此之后,您需要在父活动(onActivityResult)中捕获结果。

PS。您需要使用startActivityForResult启动QRScanner活动。

编辑:如果QRScan是相同的活动停止相机。也许你需要隐藏这个表面。

干杯:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42779933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档