【问题标题】:ZXingScannerView scan bulk mode in handleresultZXingScannerView在handlerresult中扫描批量模式
【发布时间】:2017-10-23 05:35:09
【问题描述】:

我正在尝试在 me.dm7.barcodescanner:zxing:1.9 库中实现批量扫描模式。这是我的 sn-p 代码。我试图从代码中进行多次扫描,现在我只是试图在消息对话框中显示每个扫描结果。但是,在第一次扫描结果处理程序之后,第二次扫描会自动杀死该活动。

private ZXingScannerView mScannerView;
private boolean mFlash;
private boolean mAutoFocus;
private int mCameraId = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);

ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
mScannerView = new ZXingScannerView(this);
setupFormats();
contentFrame.addView(mScannerView);
}

//i want to make my scanner able to keep scanning getting the result.
//however after the first scan, the second scan will automatically close the activity
@Override
public void handleResult(Result result) {
try {
   if(!result.getText().equals("")){
    //In message dialogue will have 1 button handle on onDialogPositiveClick 
            showMessageDialog("Contents = " + result.getText() + ", Format = 
            " + result.getBarcodeFormat().toString());
   }
 } catch (Exception e) {

 } finally {

 }
}

public void showMessageDialog(String message) {
    DialogFragment fragment = MessageDialogFragment.newInstance("Scan 
     Results", message, this);
    fragment.show(getSupportFragmentManager(), "scan_results");
}

@Override
public void onDialogPositiveClick(DialogFragment dialog) {
    mScannerView.resumeCameraPreview(this);
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();
    closeMessageDialog();
    closeFormatsDialog();
}

@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this);
    mScannerView.startCamera(mCameraId);
    mScannerView.setFlash(mFlash);
    mScannerView.setAutoFocus(mAutoFocus);
}

【问题讨论】:

    标签: android android-studio qr-code zxing barcode-scanner


    【解决方案1】:

    用 onActivityResult 试试

    /*Here is where we come back after the Barcode Scanner is done*/
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    { 
     if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            // contents contains whatever the code was
            String contents = intent.getStringExtra("SCAN_RESULT");
            // Format contains the type of code i.e. UPC, EAN, QRCode etc...
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
    
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_FORMATS", "PRODUCT_MODE,CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF");
            startActivityForResult(intent, 0); // start the next scan
        } else if (resultCode == RESULT_CANCELED) {
    
            //do whatever else you want.
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      您必须为 secondTime Scan 添加处理程序或 TimerTask。在 handleResult 中获得第一个扫描结果后,您必须在延迟一段时间后再次开始扫描,无论您想向处理程序添加什么延迟。

      @Override
          public void handleResult(final Result rawResult) {
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      handleDecode(rawResult);
                  }
              });
              Handler handler = new Handler();
              handler.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      mScannerView.resumeCameraPreview(CaptureActivity.this);
                  }
              }, 4000);// 4 sec delay to restart scan again.
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-09
        • 1970-01-01
        相关资源
        最近更新 更多