user1396135
user1396135

Reputation: 11

zxing barcode scanner results in null

I followed guide from this page and I get the intent fired up. It also found barcode. However when

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

}

is reached the dialog(showMessage function basically just creates dialog with title and text) shows following text:

Format: null Contents: null Raw bytes: (0bytes) Orientation: null EC level: null

Have I missed some part or is it just issue with bar codes? I have tried every product with barcode that I have lying around but no change.

Upvotes: 1

Views: 3615

Answers (2)

Maulik
Maulik

Reputation: 3316

// In side onActivityResult(), try this code

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();
                    }
                });
            }

        }

Upvotes: 2

deadfish
deadfish

Reputation: 12304

My project used to do something like that:

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 is just an onClickListener for button. You can of course init your button and use this code instead.

And here is an xml layout:

<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>

Please don't care the style of layout, all what you need is just in java class. :) but it should be working anyway.

Upvotes: 2

Related Questions