试图用Zxing读取条形码,但似乎没有调用onActivityResult

时间:2012-11-25 12:23:42

标签: android android-intent zxing

正如标题所说,我正在尝试扫描1D条形码,到目前为止,我有以下代码:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
    }

    public void test(View view){

        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "1D_CODE_MODE");
        startActivityForResult(intent, 0);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {  
          switch (requestCode) {
          case IntentIntegrator.REQUEST_CODE:
             if (resultCode == Activity.RESULT_OK) {

                IntentResult intentResult = 
                   IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

                if (intentResult != null) {

                   String contents = intentResult.getContents();
                   String format = intentResult.getFormatName();

                   TextView uno = (TextView) findViewById(R.id.textView1);
                   uno.setText(contents);
                   Toast.makeText(this, "Numero: " + contents, Toast.LENGTH_LONG).show();
                   Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: " + format);
                } else {
                   Log.e("SEARCH_EAN", "IntentResult je NULL!");
                }
             } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.e("SEARCH_EAN", "CANCEL");
             }
         }
    }   
}

当然,我已将IntentResult和IntentIntegrator添加到项目中。

因此,当按下按钮时,扫描仪正在被正确调用,它似乎完全扫描了代码(它在扫描后显示“Text found”),但似乎没有调用onActivityResult,因为TextView没有更新,Toast没有出现。

知道错误是什么?

提前致谢!

3 个答案:

答案 0 :(得分:3)

您的第一个错误是没有使用IntentIntegrator.initiateScan(),将其替换为您自己的startActivityForResult()手动呼叫。

你的第二个错误是假设IntentIntegrator.REQUEST_CODE0。事实并非如此。

因此,对于您当前的代码,您发送的请求代码为0,请求代码为onActivityResult(),返回0,您忽略该请求,因为你只是在寻找IntentIntegrator.REQUEST_CODE

只需通过调用test()替换initiateScan()方法的正文,您就应该处于更好的状态。 Here is a sample project演示了IntentIntegrator

的用法

答案 1 :(得分:1)

我解决了同样的问题。

public class MainActivity extends Activity {

    private TextView tvStatus, tvResult;

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

        this.tvStatus = (TextView) findViewById(R.id.tvStatus);
        this.tvResult = (TextView) findViewById(R.id.tvResult);

        Button scanBtn = (Button) findViewById(R.id.btnScan);

        scanBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                try {

                    Intent intent = new Intent(
                            "com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_FORMATS", "QR_CODE_MODE");
                    startActivityForResult(intent,
                            IntentIntegrator.REQUEST_CODE);
                } catch (Exception e) {

                    Log.e("BARCODE_ERROR", e.getMessage());
                }
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        IntentResult scanResult = IntentIntegrator.parseActivityResult(
                requestCode, resultCode, intent);

        if (scanResult != null) {

            this.tvStatus.setText(scanResult.getContents());
            this.tvResult.setText(scanResult.getFormatName());
        }
    }
}

答案 2 :(得分:0)

onActivityResault函数必须被覆盖。只需在函数声明之前添加@Override即可解决。