2016-04-11 52 views
2

我試圖創建一個實時QR碼解碼器,它允許用戶確認/取消從相機視圖讀取的數據。 (當然,根據確認或取消,它會執行一些代碼...)顯示對話框時暫停QR碼檢測

但有一個壞點: 我編碼的應用程序仍然閱讀二維碼數據,即使在對話框中,我找不到防止它的方法。

這是我的主要活動:

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.

回答

1

停止和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); 
        } 
       }); 
      } 
     } 
    }); 
+0

謝謝@André! cameraSource.start(cameraView.getHolder())修復了我的問題!謝謝,繼續搖擺。 –

+0

我發現調用'cameraSource.stop()'導致BarcodeDetector處理器停止處理'receiveDetections'方法中的其餘代碼。 – Dagmar

+0

請參閱https://stackoverflow.com/a/41024780/1912127 – Dagmar

0

我想你忘記一些這樣的。

builder.setPositiveButton(Ok, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); //this close the dialog. 
     } 

    }); 
    builder.setNegativeButton(Back, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      ///do another thing 
     } 
    }); 

我希望這可以幫助您

+0

你說得對啓動相機。但不幸的是沒有解決問題。 請看看我的public {void receiveDetections},它實現了一個可運行的(UPDATED)... –