Android QR /条形码阅读器

时间:2013-04-07 10:58:06

标签: android zxing

我正在使用zxing IntentIntegrator扫描Android应用中的条形码或QR码。扫描完代码后,我需要确定代码是QR码还是条形码。我使用以下代码:

public void onActivityResult(int request, int result, Intent i) {
    IntentResult scan = IntentIntegrator.parseActivityResult(request, result, i);
    if (scan != null) {
        format.setText(scan.getFormatName());
        contents.setText(scan.getContents());
    }
}

scan.getFormatName()正在返回null。我做错了吗?

2 个答案:

答案 0 :(得分:1)

我无法在Google Play上使用最新的条码扫描器和ZXingTest应用程序重现这一点。例如,当从扫描QR码返回时,formatName是“QR_CODE”并相应地打印。

我仍然怀疑你以某种方式触发某人正在拦截我们Intent的其他应用程序。您确定处理此问题的设备上没有其他内容可能无法正确返回吗?

您确定scan不是null并且从不设置文字吗?

答案 1 :(得分:0)

请参阅此代码

 static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    Button scan=(Button)findViewById(R.id.btn);
    scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


      try {

Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
    }
catch (ActivityNotFoundException anfe)
 {
    Toast.makeText(MainActivity.this,"No scanner found",Toast.LENGTH_SHORT).show();

    }

        }
    });

}


public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
            toast.show();
            TextView tv=(TextView)findViewById(R.id.tv);
            tv.setText("Content:" + contents + " Format:" + format);
        }
    }
}

在此您更改了QR码和条形码的intent.putExtra。