user1701593 user1701593 - 5 months ago 31
Android Question

how to capture data from qr scanner using zxing

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

Answer

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