【问题标题】:Zxing CaptureActivity.handleDecode()Zxing CaptureActivity.handleDecode()
【发布时间】:2017-03-17 23:03:52
【问题描述】:

我是Zxing新手,当我点击一个按钮时,我想扫描一个二维码图像。这是我的MainActivity.java

    private Button scan;
   scan = (Button) findViewById(R.id.btn_scan);
   scan.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent intent=new Intent(MainActivity.this,CaptureActivity.class);
        startActivityForResult(intent, SCAN_CODE);  

    }
    });

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
        case SCAN_CODE:
             Intent myIntent=getIntent();
             Bundle bundle=myIntent.getExtras();
             QR=bundle.getString("QR");
            break;
        default:
            break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

它会调用CaptureActivity.handleDecode(),这是CaptureActivity.java

      public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
            //I want to get the text in the image.
              String result = rawResult.getText(); 
              Intent intent = new Intent();  
              intent.putExtra("QR", result);  
              if(result!=null && !"".equals(result))
              setResult(RESULT_OK, intent);
              else{
                  setResult(RESULT_CANCELED, intent);
              }
              finish();
}

但它有例外 enter image description here

我不知道为什么?

【问题讨论】:

  • 请提供一个完整的例子。与其编写“这是 CaptureActivity.java”,不如使用public class CaptureActivity ...在您的代码中显示。同样,您的第一个代码 sn-p 应该是有效的 Java,这意味着它应该在一个类中。
  • 另外,请将异常复制粘贴到这里。
  • 谢谢你的建议,这个问题已经解决了,下次按你说的做。谢谢。

标签: android zxing


【解决方案1】:

你应该使用data.getStringExtra("QR"),而不是

Intent myIntent = getIntent();
Bundle bundle = myIntent.getExtras();
QR = bundle.getString("QR");

其中dataonActivityResult() 的参数。

并使用!result.isEmpty() 而不是!"".equals(result)

它更具可读性。

希望对你有帮助!

【讨论】:

  • 非常感谢您的方法已经解决了
猜你喜欢
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多