zxing条形码扫描器结果为null

时间:2013-01-04 10:55:10

标签: android zxing

我按照this页面的指南进行操作,然后我意识到了这个意图。它还找到了条形码。但是当

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (scanResult != null) {

          showMessage("result", scanResult.toString());

      }
      // else continue with any other code you need in the method

}

到达对话框(showMessage函数基本上只创建带标题和文本的对话框)显示以下文字:

  

格式:null   内容:null   原始字节:(0bytes)   方向:null   EC级别:null

我是否错过了某些部分,或者仅仅是条形码问题?我已经尝试了所有带有条形码的产品,但我没有改变。

2 个答案:

答案 0 :(得分:2)

//在onActivityResult()旁边,试试这段代码

if (resultCode == IntentIntegrator.REQUEST_CODE) {

            Log.e("inside Request code~~~~~~~~>", "Barcode>>>>");
            IntentResult scanResult = IntentIntegrator.parseActivityResult(
                    requestCode, resultCode, data);
            if (scanResult == null) {
                       Log.e("Scan Result~~~~~~~~>", "value>>> Null");
                return;
            }
            final String result = scanResult.getContents();
            final String result1 = scanResult.getFormatName();
            if (result != null) {
                handlerBarcode.post(new Runnable() {
                    @Override
                    public void run() {
                        // txtScanResult.setText(result);
                        // txtScanResultFormat.setText(result1);
                        Toast.makeText(Activity_Form_Data_4.this,
                                "Code:" + result + "  Format:" + result1,
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }

        }

答案 1 :(得分:1)

我的项目曾经做过类似的事情:

public class MenuScreen extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_menu_screen, menu);
        return true;
    }

    public void onScanCodeClick(View view) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.setPackage("com.google.zxing.client.android");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        try {
            startActivityForResult(intent, 0);
        } catch (ActivityNotFoundException aex) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("No Application Found");
            builder.setMessage("We could not find an application to scan QR CODES."
                    + " Would you like to download one from Android Market?");
            builder.setPositiveButton("Yes, Please",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                            marketIntent.setData(Uri
                                    .parse("market://details?id=com.google.zxing.client.android"));
                            startActivity(marketIntent);
                        }
                    });
            builder.setNegativeButton("No, Thanks", null);
            builder.create().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");

                // Handle successful scan

                Intent intent2 = new Intent();
                intent2.setClass(this, MenuCodeSuccess.class);
                intent2.putExtra("qrDetails", contents);
                startActivity(intent2);

            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

}

onScanCodeClick只是一个onClickListener for button。您当然可以初始化按钮并使用此代码。

这是一个xml布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2" >

    <Button
        android:id="@+id/menuScreen_SCANCODE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onScanCodeClick"
        android:text="@string/scanButton" />

</TableLayout>

请不要关心布局的风格,所有你需要的只是在java类中。 :)但它应该是有效的。