2014-09-10 91 views
2

我正在使用ZBar android庫掃描QR碼。到目前爲止,掃描代碼並將掃描結果顯示在textView中。我想在掃描代碼時觸發操作,就像掃描qr代碼並相應觸發操作的其他應用程序一樣。這是我實現如何使用ZBAR for Android觸發QR碼掃描操作?

public class CameraTestActivity extends Activity 
{ 
    private Camera mCamera; 
    private CameraPreview mPreview; 
    private Handler autoFocusHandler; 

    TextView scanText; 
    Button scanButton; 

    ImageScanner scanner; 

    private boolean barcodeScanned = false; 
    private boolean previewing = true; 

    static { 
     System.loadLibrary("iconv"); 
    } 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     autoFocusHandler = new Handler(); 
     mCamera = getCameraInstance(); 

     /* Instance bar code scanner */ 

     scanner = new ImageScanner(); 
     scanner.setConfig(0, Config.X_DENSITY, 3); 
     scanner.setConfig(0, Config.Y_DENSITY, 3); 

     mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB); 
     FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview); 
     preview.addView(mPreview); 

     scanText = (TextView)findViewById(R.id.scanText); 

     scanButton = (Button)findViewById(R.id.ScanButton); 

     scanButton.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
       if (barcodeScanned) { 
        barcodeScanned = false; 
        scanText.setText("Scanning..."); 
        mCamera.setPreviewCallback(previewCb); 
        mCamera.startPreview(); 
        previewing = true; 
        mCamera.autoFocus(autoFocusCB); 
        } 
       } 
      }); 
    } 

    public void onPause() { 
     super.onPause(); 
     releaseCamera(); 
    } 

    /** A safe way to get an instance of the Camera object. */ 
    public static Camera getCameraInstance(){ 
     Camera c = null; 
     try { 
      c = Camera.open(); 
     } catch (Exception e){ 
     } 
     return c; 
    } 

    private void releaseCamera() { 
     if (mCamera != null) { 
      previewing = false; 
      mCamera.setPreviewCallback(null); 
      mCamera.release(); 
      mCamera = null; 
     } 
    } 

    private Runnable doAutoFocus = new Runnable() { 
      public void run() { 
       if (previewing) 
        mCamera.autoFocus(autoFocusCB); 
      } 
    }; 

    PreviewCallback previewCb = new PreviewCallback() { 
     public void onPreviewFrame(byte[] data, Camera camera) { 
      Camera.Parameters parameters = camera.getParameters(); 
      Size size = parameters.getPreviewSize(); 

      Image barcode = new Image(size.width, size.height, "Y800"); 
      barcode.setData(data); 

      int result = scanner.scanImage(barcode); 

      if (result != 0) { 
       previewing = false; 
       mCamera.setPreviewCallback(null); 
       mCamera.stopPreview(); 

       SymbolSet syms = scanner.getResults(); 

       for (Symbol sym : syms) { 
        scanText.setText("barcode result " + sym.getData()); 
        barcodeScanned = true; 
       } 
      } 
     } 
    }; 

    // Mimic continuous auto-focusing 
    AutoFocusCallback autoFocusCB = new AutoFocusCallback() { 
      public void onAutoFocus(boolean success, Camera camera) { 
       autoFocusHandler.postDelayed(doAutoFocus, 1000); 
     } 
     }; 
    } 
+0

您是否嘗試過它由回調函數處理程序類? – Tejas 2014-09-10 09:35:44

+0

不可以,你可以舉個例子嗎?我第一次使用這個庫, – 2014-09-10 10:39:26

+0

試試這個庫。 [鏈接](https://github.com/dm77/barcodescanner)它也有片段支持。 – Tejas 2014-09-10 14:21:24

回答

0

試試這個,我用了zbar和並使用此代碼觸發掃描成功的提示對話框:

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    if (requestCode == 0) { 
     if (resultCode == getActivity().RESULT_OK) { 
      // Handle successful scan 
      String contents = intent.getStringExtra("SCAN_RESULT"); 
      //contents has your scanned string 
      //you can trigger your action here 


     } else if (resultCode == getActivity().RESULT_CANCELED) { 
      // Handle cancel 
     } 
    } 
}