2017-03-14 4 views
1

私はcom.google.android.gms.vision依存関係を使用してQRコードをスキャンしています。 私が直面している問題は、QRコードを何度もスキャンし続けることです。私は一度だけスキャンしたい。一度停止する方法QRコードがスキャンされていますか?

は、ここに私のコードです:

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()); 


        } 
       }); 
      } 


     } 
    }); 
+0

私は現在、QR関連するもののため[ZXing](https://github.com/dm77/barcodescanner)使用しています、それはスキャンするにははるかに簡単かつ直接的なアプローチを持っており、パース結果。あなたが今使っているものを変えることができたら、それを調べることをお勧めします:) – Barak

答えて

1

ただ、結果セットと、スキャンがその活動に完成されたコール仕上げ。その後、親アクティビティ(onActivityResult)の結果を取得する必要があります。

PS。 startActivityForResultを使用してQRScannerアクティビティを開始する必要があります。

編集:QRScanが同じアクティビティ停止カメラの場合。そして、あなたはその表面を隠す必要があるかもしれません。

乾杯:)

関連する問題