0

I am developing an application and a part of that involves QR scanning. After a lot of research I was successful in developing a stand alone scanning app. When a user opens the scanner and scans a particular QR code, he gets some value, e.g. a URL. Now I want to store the data acquired on scanning into my android code. Can anybody help me how do i go about it?

From what I can see, I need to work with capture activity class of Zxing. However, I am not sure what exactly needs to be done. All the blogs that I read online direct me to use an Intent to call barcode scanning. However, the purpose of my app is not to just scan the products. I need to store the information of the scanned product and later use it for some other purpose.

Kindly help me.

Thanks, Amey

Here is the code from Zxing.. this is the main activity that deals with all the scanning. to what i have learnt by reading online, i need to capture the data returned when a barcode is scanned..

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    if (resultCode == RESULT_OK) { 
        if (requestCode == HISTORY_REQUEST_CODE) { 

            int itemNumber = intent.getIntExtra(Intents.History.ITEM_NUMBER, -1); 

            if (itemNumber >= 0) { 
                HistoryItem historyItem = historyManager.buildHistoryItem(itemNumber);             
                decodeOrStoreSavedBitmap(null, historyItem.getResult()); 
            } 
        }
    } 
} 


if (Intents.Scan.ACTION.equals(action)) { // Scan the formats the intent requested, and   return the result to the calling activity 
    source = IntentSource.NATIVE_APP_INTENT; 

    decodeFormats = DecodeFormatManager.parseDecodeFormats(intent); 
    if (intent.hasExtra(Intents.Scan.WIDTH) && intent.hasExtra(Intents.Scan.HEIGHT)) {

        int width = intent.getIntExtra(Intents.Scan.WIDTH, 0); 
        int height = intent.getIntExtra(Intents.Scan.HEIGHT, 0); 

        if (width > 0 && height > 0) { 
            cameraManager.setManualFramingRect(width, height); 
        } 
    }
}

4 Answers 4

2

Using it via intent is the easiest way and it is possible to store the result of the scan, you'll just have to do it yourself. How it works is all in Zxing's docs at http://code.google.com/p/zxing/wiki/ScanningViaIntent

From the above link

First add code to invoke the Intent:

IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();

Second, add this to your Activity to handle the result:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanResult != null) {
            // handle scan result
            //here is where you would get the data from the scanResult
            //and store locally by writing to a file or however you 
            //intend to store it
        }
        // else continue with any other code you need in the method
}

I haven't used this version Zxing, the one I used was at least 2 years ago but the process is the same

1 - start Zxing via Intent 2 - scan QR code 3 - retrieve the info from the scan in onActivityResult.

8
  • Hi Triggs, if i understand you correctly, the method that you have discussed here is viable if i want my app to launch external bar code scanning app.. so when the user opens my app, he wil be prompted to download another bar code scanning app.. i would like to avoid this.. currently my app scans the bar code on its won, thanks to Zxing! i wish to go ahead with this architecture while intending to capture the data scanned by the scanner. Sep 27, 2012 at 19:54
  • Ah I see, to better understand your problem can you post some code that shows the issue you are having?
    – triggs
    Sep 27, 2012 at 20:31
  • Hi, here is the code from Zxing.. this is the main activity that deals with all the scanning. to what i have learnt by reading online, i need to capture the data returned when a barcode is scanned.. Sep 27, 2012 at 21:02
  • public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (resultCode == RESULT_OK) { if (requestCode == HISTORY_REQUEST_CODE) { int itemNumber = intent.getIntExtra(Intents.History.ITEM_NUMBER, -1); if (itemNumber >= 0) { HistoryItem historyItem = historyManager.buildHistoryItem(itemNumber); decodeOrStoreSavedBitmap(null, historyItem.getResult()); } } } } Sep 27, 2012 at 21:03
  • if (Intents.Scan.ACTION.equals(action)) { // Scan the formats the intent requested, and return the result to the calling activity source = IntentSource.NATIVE_APP_INTENT; decodeFormats = DecodeFormatManager.parseDecodeFormats(intent); if (intent.hasExtra(Intents.Scan.WIDTH) && intent.hasExtra(Intents.Scan.HEIGHT)) { int width = intent.getIntExtra(Intents.Scan.WIDTH, 0); int height = intent.getIntExtra(Intents.Scan.HEIGHT, 0); if (width > 0 && height > 0) { cameraManager.setManualFramingRect(width, height); } } Sep 27, 2012 at 21:06
2

Hi I have finally found an answer to this question. It was not that difficult as i thought (since Zxings code has been written by Zxing team and not by me.. anyway..)

So if you want to store the data captured by qr scanner(provided by Zxing) in your android code (for whatever purpose.. in my case i want to send this data to the web server.. anyway..) then you just need to modify the following function.. here is where you get the result of the scanned activity..

public void handleDecode(Result rawResult, Bitmap barcode) {
inactivityTimer.onActivity();
lastResult = rawResult;
Log.d("last result", "checking if raw result is what i expect");
System.out.println(lastResult);
ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult);
historyManager.addHistoryItem(rawResult, resultHandler);
}

I have added Log and print statements to check if i am getting the correct result. and yes, it did give me a correct answer.. you can find this in CaptureActivity class.

@triggs: thanks for your help! You did get me on a right track :-)

2

I chanced upon your thread searching for help with this set of code as well. In my case I had to send information back to the main application (ZXing is a library in my project - I know, I have talked to my clients about this but we were not able to use Intents due to business requirements).

Here is my solution if you need to pass information back to another activity from another project.

Project A is the main application, whereas the ZXing project will be called as such.

Edit handleDecode() in ZXing's CaptureActivity.java:

public void handleDecode(Result rawResult, Bitmap barcode) {
    inactivityTimer.onActivity();
    lastResult = rawResult;
    ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult); 
if (source == IntentSource.NATIVE_APP_INTENT) {
        Intent resultIntent = new Intent();
        resultIntent.putExtra("result", rawResult.toString());
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    } 

}//end handleDecode()

And in your Project A's activity that is calling CaptureActivity,

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    NullQRCodeDialogFragment dialog = new NullQRCodeDialogFragment();
    String result = "";
        if (resultCode == RESULT_OK) {
            result = intent.getStringExtra("result");
             if (result.equals(null)){
                  //TODO
             } else {
                  //TODO
             }
}//end onActivityResult

Hope this helps! This is my first post here and I'm so glad that I could contribute =)

0
0

Here is the solution that I am using. It is working fine for me.

Intent intent = new Intent(SelectOptionActivity.this, CaptureActivity.class);
                intent.putExtra("SCAN_MODE", "ONE_D_MODE");
                intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,EAN_13,EAN_8,UPC_A,QR_CODE");
                intent.setAction(Intents.Scan.ACTION);
                startActivityForResult(intent, 1);


public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            final String contents = intent.getStringExtra(Intents.Scan.RESULT);
            final String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);

        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.