2017-03-14 2 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)에서 결과를 catch해야합니다.

추신. startActivityForResult로 QRScanner 활동을 시작해야합니다.

편집 : QRScan이 동일한 활동 중지 카메라 인 경우. 그리고 아마 그 표면을 숨길 필요가 있습니다.

건배 :)