Search code examples
javaandroidandroid-intentzxingandroid-studio-2.0

Integrating ZXing in my application


I need to build an app which will recognize QR codes without forcing the user to install other apps. In future, I will also need to manipulate the scanned image before recognizing the code (codes scanned by me will have inverted colors).

Trying to follow hints and tutorials from these links:
Integrating the ZXing library directly into my Android application
Embedding ZXing in android app
http://karanbalkar.com/2013/12/tutorial-65-implement-barcode-scanner-using-zxing-in-android/

After creating some basic code and running the app I click my Scan button and get an error that No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }

What I've done:
Create new project
Copy core-3.2.1.jar to libs/
Add intent calling and result handling

intent/result code added by me:

private Button scan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    scan= (Button)findViewById(R.id.btnScan);

    scan.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(intent, 0);
        }
    });
}

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
            MultiFormatWriter writer = new MultiFormatWriter();


        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
            Log.i("App","Scan unsuccessful");
        }
    }
}

How to start the intent? What am I doing wrong?


Solution

  • you should launch scan like this:

    @Override
        public void onClick(View v) {
          IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
          integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
          //in case you want to customize a bit.
          integrator.setPrompt("Scan a QR/Bar code");
          integrator.setCameraId(0);
          integrator.setBeepEnabled(false);
          integrator.initiateScan();
        }
    

    Receive results like this:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case IntentIntegrator.REQUEST_CODE: {
                if (resultCode != RESULT_CANCELED) {
                    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                    String data = scanResult.getContents();
                    // use this data
                } else {
                    // error
                }
                break;
            }
        }
    }
    

    Edit 1:

    Add this to build.gradle of your app as dependencies:

    compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
    compile 'com.google.zxing:core:3.2.0'