无法捕获条形码扫描结果

时间:2015-12-12 02:07:52

标签: java android zxing barcode-scanner

我正在使用Zxing库来扫描项目并存储结果。当我扫描时,扫描结果(数字组)暂时显示在条形码扫描仪上,但当我尝试通过Toast捕获并显示它以进行测试时,它将返回null。

//this method occurs when I click a button
final String ZXING_SCAN = "com.google.zxing.client.android.SCAN";
public void scanItem(View view){
        try {
            //works. able to activate the barcode scanner. 
            i = new Intent(ZXING_SCAN);
            i.putExtra("SCAN_MODE", "PRODUCT_MODE");
            startActivityForResult(i, 0);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

//Using this method to capture the scan result. The toast prints out null for both values.
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 0){
            if(resultCode == RESULT_OK){
                String content = i.getStringExtra("SCAN_RESULT");
                String format = i.getStringExtra("SCAN_RESULT_FORMAT");

                //This is he toast that shows null for both. 
                Toast.makeText(getApplicationContext(), "Content: " + content + ", Format: " + format, Toast.LENGTH_LONG).show();
            }
            else {
                //Toast.makeText(getApplicationContext(), "Barcode not working...", Toast.LENGTH_LONG).show();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

而不是

String content = i.getStringExtra("SCAN_RESULT");  
String format = i.getStringExtra("SCAN_RESULT_FORMAT"); 

onActivityResult尝试

String content = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");