1

I want to make an application in which the user scans a barcode from a product and then the android phone will show some details about the product (from a database). I have installed ZXing barcode scanner on my android phone which i use for debugging my applications. When i use the ZXing application it scans the barcodes. But when i open the ZXing from my android project with the following code which i found in another post here, it doesn't scan the barcode.I can see the red line in my phone's camera but it doesn't show the green dots that 'read' the barcode.

Calling barcode scanner on a button click in android application

Why could this happen? Thanks in advance

Community
  • 1
  • 1
Fivos
  • 558
  • 8
  • 19

4 Answers4

1

try the code below. It's working code I've used it myself.

public class MyTestActivity extends Activity {
    @Override
      public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
    Button b = new Button(this);
    b.setText("Scan");
    b.setWidth(100);
    LinearLayout ll = new LinearLayout(this);

     b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            startActivityForResult(intent, 0);
        }   
    });

        ll.addView(b);
        setContentView(ll);

    }

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

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

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

                Dialog d = new Dialog(this);
                LinearLayout ll = new LinearLayout(this);
                TextView tv = new TextView(this);
                tv.setText(contents+" "+format);
                ll.addView(tv);
                d.setContentView(ll);
                d.show();          
            }
        }
    }
       }

hope it helps

Sergey Benner
  • 4,421
  • 2
  • 22
  • 29
  • It should work just the same without the CAMERA permission. Since you are launching the Zxing activity by intent, and it handles everything with the camera, only the Zxing Application needs to hold the CAMERA permission. – FoamyGuy Feb 02 '12 at 00:14
  • FYI even the androidtest example package of zxing has the permission in the manifest file. – Sergey Benner Feb 02 '12 at 00:21
  • Yes, indeed it's working w/o this permission set. I'd use it as a precaution still. :) – Sergey Benner Feb 02 '12 at 00:29
  • I think at some point more of the user population will care more about what permissions apps are asking for. Camera will be one not taken lightly I believe. I feel it is best to not request any permissions that my app doesn't need. I've built a few applications that implement barcode scanning with intents this way and I've never included CAMERA permission, and never seen a problem from it. – FoamyGuy Feb 02 '12 at 02:07
  • You do not need the permission when integrating by Intent. ZXingTest has it because it also takes pictures for tests. – Sean Owen Feb 02 '12 at 09:02
  • First of all the question was not about the permissions. Second I provided the code which IS working. Third as I wrote already it didn't require the permissions. I took the permissions from manifest of `androidtest` examples of `Zxing 1.7` bundle and it was required by the `CameraManager` which is working directly with `android.hardware.Camera` there and `NOT` by the intent invoker and the last I removed the permissions from the answer. – Sergey Benner Feb 02 '12 at 11:05
  • In my app, I am taking snaps and saving them into my DB. Same I am trying to do with the scanner but `onActivityResult` never calls after successful scan result – Moeez Nov 11 '20 at 09:45
0

Sergey, above code will work for QR code, not for Barcode, have you tested for barcode.

Please let me know anyone work on barcode with Zxing lib.

Rakesh
  • 2,732
  • 29
  • 28
0

I had the same problem, to solve change the

intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); 

to

intent.putExtra("PRODUCT_MODE", "QR_CODE_MODE");
0

Rather than debug your intent code, please just use the pre-packaged integration code we provide as described here: http://code.google.com/p/zxing/wiki/ScanningViaIntent

Sean Owen
  • 66,182
  • 23
  • 141
  • 173