Shihab Uddin
Shihab Uddin

Reputation: 6931

how to get QR code type

I am working with Zxing library to decode QR code for android. I can get QR Code. Now, I want to catch what types of QR code it returns say (URL,Phone Nubmer,Plain Text etc). I tried to explore ResultParser class but i can't use it.

Updated: I remove my wrong procedure using ResultParser class.

Here is my onActivityResult Code.

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            contents = intent.getStringExtra("SCAN_RESULT");
        // check the contents strings here 
          //  Need to get actual return type of result 
            if (contents.contains("https:") || contents.contains("http:")) {
                aBarcode.setBarcode_Type(WEB_URL);
                Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(contents));
                startActivity(browserIntent);

            } else {
                aBarcode.setBarcode_Type(PLAIN_TEXT);
                Toast.makeText(getApplicationContext(), contents,
                        Toast.LENGTH_LONG).show();
            }
            barcodeList.add(aBarcode);
            // show previous barcodes detail
            showBarcodeReaderHistory();
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            Log.e("Barcode Result", "Result canceled");
        }
    }
}

Updated :I open ScanActivity like:

// start scan after button click
        btnScanQRCode.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                 Intent intent = new Intent(
                 "com.google.zxing.client.android.SCAN");
                 intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                 startActivityForResult(intent, 0);

            }
        });

Hope, i clear it to all. Waiting for your great knock. Thank you.

Upvotes: 1

Views: 3038

Answers (2)

Chinmoy Debnath
Chinmoy Debnath

Reputation: 2824

You can modifie the CaptureActivity of the library to get the QR Code type into your Activity.

In CaptureActivity there is a method name handleDecodeExternally which returns the data to your Activity add this line

intent.putExtra("QR_TYPE", getString(resultHandler.getDisplayTitle()));

before the Activity finishes.

And then get this value to your Activity's onActivityResult

String type = intent.getStringExtra("QR_TYPE");

Finally you have the type of QR Code.

Thank you

Edit

In your zixing library project under com.google.zxing.client.android package there is an Activity named CaptureActivity. And on that Activity there is method named handleDecodeExternally which Briefly show the contents of the barcode, then handle the result outside Barcode Scanner. In that method there is an intent by which you have send the data what you found on QR to your activity.

add that line before you leave this activity, After adding it will be like this --

      // ------------- Mine Added -------------------
      intent.putExtra("QR_TYPE", getString(resultHandler.getDisplayTitle()));
      // --------------------------------------------

      sendReplyMessage(R.id.return_scan_result, intent); 

i think you will find this sendReplyMessage(R.id.return_scan_result, intent); in 650 +/- line in CaptureActivity. Thank you

Upvotes: 2

Bryan Herbst
Bryan Herbst

Reputation: 67239

You shouldn't be getting the result from the returned Intent's extras yourself. Instead, do it like so:

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

You can then get all the data you need through the IntentResult. Specifically to get the type, you can call scanResult.getFormatName()

Upvotes: 2

Related Questions