Scanning Barcodes (ZXing) in a own Thread and displaying a ProgessDialog - android

since I know this side most of my questions have been answered be using the search. But this seems to be a special one.
I am new to Google Android development, so I want to learn by doing. I want to create an app, that scans barcodes and displays a list of possible products. I am using ZXing (got this from here! ;)) to scan the barcdes. Works perfect. I'm quering the google shopping api with the scanned barcode and get parse the rss feed, since there is a result (but in most cases there is ^^).
This all works great. But it takes up to four seconds after I scan the app returns to my activitiy. This seems very long to, isn't it? So I thought to move the scanning part into a single thread and while quering the result from the api there should be a ProgressDialog for the user, that he knows whats going on there.
Thats, my Code so far:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String scannedCode = intentResult.getContents();
String format = intentResult.getFormatName();
//Gets information to the scanned product by the
//Google-Shopping-API in form of a rss feed.
AndroidFeedParser afp = new AndroidFeedParser("https://www.googleapis.com/shopping/search/v1/public/products?key=" +
AndroidFeedParser.API_KEY + "&country=DE&q=" + scannedCode + "&alt=" + AndroidFeedParser.FEED_CODE);
//Parses the given rss feed and gives a FeedItem-List
List<FeedItem> item = afp.parse();
Toast toast = null;
if(item.size() < 1)
//products is NOT listed within the Google-Shopping-API
toast = Toast.makeText(this, "Dieses Produkt ist nicht in der Datenbank.", Toast.LENGTH_LONG);
else
//product is listed within the Google-Shopping-API
toast = Toast.makeText(this, item.get(1).getPrice().toString(), Toast.LENGTH_LONG);
toast.show();
Log.d("SEARCH_EAN", "OK, EAN: " + scannedCode + ", FORMAT: " + format);
} else {
Log.e("SEARCH_EAN", "IntentResult je NULL!");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e("SEARCH_EAN", "CANCEL");
}
} }
I told ya, this works awesome. But this takes to much time, I guess the query to the api costs that much time. I hope you understand my question and you can help me! I do know, that there must be an Interface Runnable implemented and the method run(), but I do not know what part of code I do have to separate to archive that wish above. Please help me!
Best regards,
Basti
(Excuse me for my bad english, I'm from Germany)

You should start a new asynchronous task from line when you create AndroidFeedParser... but maybe your app is already done? ;)

Related

In Android Studio I want a QR code to return an Image on screen when the QR Code is scanned

I have developed an app where a QR Code Scanner returns two pieces of text that the QR Code holds. I am new to coding using android studio and for the most part have used tutorials to get this far. I am now looking to return an Image on screen, that a QR Code holds when it is scanned. Is this possible?
Below is the code that returns the text to the app from the QR Code.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
//if qrcode has nothing in it
if (result.getContents() == null) {
Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
} else {
//if qr contains data
try {
//converting the data to json
JSONObject obj = new JSONObject(result.getContents());
//setting values to textviews
textViewName.setText(obj.getString("name"));
textViewAddress.setText(obj.getString("address"));
} catch (JSONException e) {
e.printStackTrace();
//if control comes here
//that means the encoded format not matches
//in this case you can display whatever data is available on the qrcode
//to a toast
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}
You can create a QR code with the URL to fetch the image and show it. QR codes can store a very small amount of data to hold an image.
EDIT:
To show the actual image instead of its URL, you just need to add an ImageView to your layout (you can remove the TextViews if you want) and load the image on it after reading the QR code. In order to do that the simplest way possible, you can use the Picasso library. This post explains very well how to install it and use it.
Give it a try and let me know if you need some more help.

PhoneGap Barcode Scanner Issue

I am developing an application in cordova but my barcode scanner from Zxing opens and closes Automatically after scanning the product in need to add a scan/exit button and a close button the scanner shouldnt open and close automatically. I also need to check if the scanned product exist in the database (SQL SERVER) and return product infomation i have tried to google but to no avail please help.the following is my code in Eclipse. I need to know how i can modify The UI and add my own control of my ZXING barcode scanner plugin i am developing in Eclipse and have a web api service hosted in IIS which i can access on my android App.Please ASAP or show me how i can modify this barcode UI to ADD my controls.
public void scan() {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, REQUEST_CODE);
}
My OnStartActivity code is here is the code but it seems like it is the same as yours.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
JSONObject obj = new JSONObject();
try {
obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
obj.put(CANCELLED, false);
} catch (JSONException e) {
Log.d(LOG_TAG, "This should never happen");
}
//this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
this.callbackContext.success(obj);
} else if (resultCode == Activity.RESULT_CANCELED) {
JSONObject obj = new JSONObject();
try {
obj.put(TEXT, "");
obj.put(FORMAT, "");
obj.put(CANCELLED, true);
} catch (JSONException e) {
Log.d(LOG_TAG, "This should never happen");
}
//this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
this.callbackContext.success(obj);
} else {
//this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
this.callbackContext.error("Unexpected error");
}
}
}
Good Afternoon I have an app that do what you are looking for... first of all I create an intent so i can use any QR scanner I use an external app in order to get the value of the QR code here.
public void scanNow(View view) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
so in my onStartActivity for result send that info to the server side and I wait for the answer...
public void onActivityResult(int requestCode, int resultCode, Intent intent){
if(requestCode == 0){
if(resultCode == RESULT_OK){
contents = intent.getStringExtra("SCAN_RESULT");// here is the content of the qr scanner
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
String messLoc = "Visita Guardada Con Exito";
Log.i("xZing", "contents: " + contents + " format: " + format);// Handle successful scan
Toast.makeText(this, messLoc, Toast.LENGTH_LONG).show();
new Thread(new Task()).start(); // here I start the thread for the connection
return;
}
else if(resultCode == RESULT_CANCELED);{// Handle cancel
Log.i("xZing", "Cancelled");
}
}
}
Please let me know if this help you
UPDATE:
I just started using Zbar instead Zxing. It's a whole lot easier to work with. Extremely easy to embed (no 3rd party app for the scanner). It has a lot let file to load into the project. Also, it has an included example that you can pretty much copy and paste into your code. The example is perfect for what you are trying to do and would only require a few edits to gain the functions you are looking for. So try using the zbar library.
Zbar - https://github.com/dm77/ZBarScanner
A tutorial - http://community.magicsoftware.com/en/library?book=en/Magicxpa/&page=Android_Barcode_Scanning_(Using_ZBar_SDK)_Sample.htm
The tutorial isn't that good but it helps with setting it up. NOTE: the links they have don't work if you click them..you have to copy and paste the text into your browser.
The database stuff I explained below still is relevant but ignore the parts about zxing.
Best of luck to you!
Original answer
I may be able to help somewhat for you SQL issues. Do you have any database helper set up?
First off I would google around and find a simply database example to set up the database. There are a number of good ones out there that show you how to set up a database in SQLite for android. The one I used to learn some of the basics:http://hmkcode.com/android-simple-sqlite-database-tutorial/ . You can use the example of Book to create a product class with all the values for columns that match your needs. Then you simply create automatic getters and setters with eclipse click "Source -> generate getters and setters". Onces you've done that you can use the tutorial below to set up your qr scanner. As for keeping the window open, I don't think you need to do that. Just create an activity that the scanner closes into. In that acticity you can have the output and have the scan results compared to the database.
I was able to use the tutorial http://%20http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162 to integrate zxing into my app. Once I did that
An example of parsing the data from the qr code:
First you call the scan:
public void onClick(View v){
//respond to clicks
if(v.getId()==R.id.scanQRButton){
//scan
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
The scan results code then looks like this:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
scanContent = scanningResult.getContents();
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
Now the finishing method for the scan button:
public void onClick(View v){
//respond to clicks
if(v.getId()==R.id.scanQRButton){
//scan
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
formatTxt.setText( "Scan Initiated");
contentTxt.setText(" Scan Results: " + scanContent);
if(scanContent != null){
String userid,medname,tabstaken,dob;
// Here I am breaking apart the scan results and
//saving them into variables.
//Do this then call the database for your product and compare
StringTokenizer st = new StringTokenizer(scanContent, ",");
// token 0
dob = st.nextToken();
//token 1
medname = st.nextToken();
//token 2
tabstaken = st.nextToken();
//token 3
//rxnumber
// So here you setup the db so you can access it
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
//This is used to call the results
HashMap<String,String> user = new HashMap<String, String>();
//Use a method such as getProductResults() for your case
user = db.getUserDetails();
enter code here
//An example of me storing the user
userid = user.get("uid");
//debug.setText("Userid: "+ userid+ " medname: " + medname + " tabs: " +tabstaken);
UserLogEntry userlog = new UserLogEntry(getApplicationContext(),userid,medname,tabstaken);
userlog.addUserLog();
}
}
}
If you need to see my database class let me know. Obviously this isn't the exact code you need, but it shows you how to use the results from the QR and call the DB results to do comparisons. Hopefully its helpful

Prompt voice recognition from within onActivityResult in Android

I hope I'll be clear enough.
In my app, I have set it so that pressing the back button activates google's voice recognition service. In the onActivityResult, it checks whether the first word is "call" or "text" and proceed accordingly with the rest of the spoken sentence. For text specifically, it goes like "text [contact name] message [message content]" and then sends it using an smsManager. But since it might get the name or the message wrong, I want it to read out the message and the person's name first for confirmation which I did just fine.
The problem is, to confirm or cancel, I want to also use voice recognition. If after the message is read out, the user says something like send and only then it should proceed to send the message. So, what I need to know is how/if can I implement this (newVoiceCommand) in the code below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
s="";
text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
decide = text.get(0).split("\\s");
for (int i=1; i<decide.length;i++) s= s+decide[i] + " ";
if (decide [0].equals("text")){
if (!s.equals("")){
msg = s.split(" message");
char[] stringArray = msg[0].toCharArray();
stringArray[0] = Character.toUpperCase(stringArray[0]);
msg[0] = new String(stringArray);
contact = get_Number (test(msg[0]));
Intent intent = getIntent();
finish();
startActivity(intent);
String temp = "Are you sure you want to send " + msg[1] + " to " + test(msg[0]);
speakOut (temp);
if (newVoiceCommand.equals("send")){
try {
SmsManager smsManager = SmsManager.getDefault();
out.append(contact);
smsManager.sendTextMessage(contact, null, msg[1], null, null);
Toast.makeText(getApplicationContext(), "SMS Sent! to "+msg[0] + " at " +contact,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
else if (newVoiceCommand.equals("no")) out.append("Not sending");
}
}
else if (decide[0].equals("call")){
out.append (s);
if (!s.equals("")) {
call (s);
}
}
}
break;
}
}
}
My current attempt:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
s="";
//Grab the speech results and save them in an arraylist
text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//Split each word into an array item
decide = text.get(0).split("\\s");
//Re-concatenate the words starting with the second word together
for (int i=1; i<decide.length;i++) s= s+decide[i] + " ";
//If the first word is "text", then send an SMS using the rest of the information spoken
if (decide [0].equals("text")){
if (!s.equals("")){
check (s);
Intent spIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
spIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en_US");
try {
startActivityForResult(spIntent, RESULT_SPEECH2);
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
switch (requestCode) {
case RESULT_SPEECH2: {
if (resultCode == RESULT_OK && null != data) {
text2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
s2 = "";
if (text2.get(0).equals("send")){
smsText (s);
}
else if (text2.get(0).equals("cancel")) return;
}
}
In short, all of the voice recognition results are going to come to you through an asynchronous call to onActivityResult. You don't block and wait for the result, so you need a way to determine whether the recognition is in response to a command or a confirmation at the time you receive the result.
This is what the requestCode parameter of Activity.startActivityForResult() is for:
Sometimes you want to get a result back from an activity when it ends.
For example, you may start an activity that lets the user pick a
person in a list of contacts; when it ends, it returns the person that
was selected. To do this, you call the startActivityForResult(Intent,
int) version with a second integer parameter identifying the call. The
result will come back through your onActivityResult(int, int, Intent)
method.
If you use two different values for requestCode (e.g., define private final static int REQUEST_SPEECH_COMMAND = 1, REQUEST_SPEECH_CONFIRMATION = 2) when you start the voice recognition activity, you can respond differently in onActivityResult. Note that you'll need to store the results of the first voice recognition (the action, message, and recipient) so this information will be available the second time around.

Android - Measuring the time between opening an activity via intent and when results are received

I have a simple application that opens the Barcode Scanner App by ZXing.
Now, I want to know how much time it takes for a scan to accomplish. Basically, the time when the intent was started up to the time that the result is obtained. Now, I know this is simple, just put a nanoTime object when the intent is executed and another nanoTime when I get the result, subtract the two and multiply to get it in seconds/milliseconds.
However, I have the intent on loop, because I want to scan a sequence of QR Codes and I figured that a for loop is the best way to go about it.
So I have several global time variables
long start, end, time;
Here is the code for the intent:
//Multi Scan Button
public Button.OnClickListener onMultiScan = new Button.OnClickListener() {
public void onClick(View v) {
//start = System.nanoTime();
for(int i = 0 ; i < 5 ; i++){
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
//start = System.nanoTime();
startActivityForResult(intent, multiScan);
}
}
};
And here is the code to get the input.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//ordinary scan
if (requestCode == ordinaryScan) {
if (resultCode == RESULT_OK) {
end = System.nanoTime();
time = (end - start) / 1000000;
System.out.println("Time" + " " + /*i + +*/ time);
String contents = data.getStringExtra("SCAN_RESULT");
Toast.makeText(getApplicationContext(), contents, Toast.LENGTH_SHORT).show();
}
else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
//multiple scan
if (requestCode == multiScan) {
end = System.nanoTime();
time = (end - start) / 1000000;
System.out.println("Time" + " " + /*i + +*/ time);
if (resultCode == RESULT_OK) {
String content = data.getStringExtra("SCAN_RESULT");
//codes = codes + contents + " ";
inputs[counter] = content;
counter += 1;
if(counter == 5){
//output();
verify();
}
}
else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
I commented out the part where I start the timer since I'm not sure where to put them. What happens is if I call it before the loop, I get one result only, if I put it inside the loop, I get multiple results which aren't so far from one another.
Obtaining the time it takes for an intent to finish is easy if it's only called once, but if it's called more than once on loop, it gets tricky.
So, is there a way to properly take the time needed for an intent to open and get the result? I'm open to suggestions even if it means rewriting how I call the activity more than once (I'm currently using a loop).
I also prefer to be able to get the time right after a code is scanned right away, so I know the time between the activity call and the activity result. I don't want to wait for each call to finish before I get the time it took for each call to finish. I want real time update. Thanks.
If you still want to use the loop, a possible approach would be not to use a single multiscan requestcode but one for each run of the loop.
Something like startActivityForResult(intent, MULTISCAN_BASE + i);
In this way you will be able to track all the runs separately, because you will be able to recognize which run the result refers to :
i = requestCode - MULTISCAN_BASE
The final step is putting your start time in an array, one for each cycle of the loop.
int[] start = new int[5];
Checking when you are done and get the sum of the times is then trivial.
Okay, found a way to make it work. It's a bit unrefined to be honest but it works and updates real time. The basic concept is to have a multiScan requestCode that is incremented every time that it gives a result as handled in onActivityResult method. Then there's an if statement that checks if the multiScan requestCode has reached the maximum number of iterations desired.
So here are the global variables:
int multiScan = 1;
int multiScanBase = 1;
long start, end, time;
And then the multiScanButton would look something like:
//Multi Scan Button
public Button.OnClickListener onMultiScan = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
start = System.nanoTime();
startActivityForResult(intent, multiScan);
}
};
Then the onActivityResult method would look something like this:
//multiple scan
if (requestCode < multiScanBase + 5 ) {
end = System.nanoTime();
time = (end - start) / 1000000;
System.out.println("Request Code = " + multiScan);
System.out.println("Time" + " " + /*i + +*/ time);
if (resultCode == RESULT_OK) {
String content = data.getStringExtra("SCAN_RESULT");
//codes = codes + contents + " ";
inputs[counter] = content;
counter += 1;
if(counter == 5){
//output();
verify();
}
}
else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
//This is the part where I check for the requestCode
//In this example, I wanted to iterate the activity 5 times
//So if the request code is still less than the base + number of iterations
//Increment the multiScan requestCode and run the method again
//In this way, the time for each scan is given in real time
if(requestCode < multiScanBase + 5){
multiScan += 1;
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, multiScan);
}
}
So there. The code works for me and updates are given in real time. Thanks for the ideas on how to do this.
Merry Christmas everyone.

getting scan result when using zxing?

I am currently using the Zxing library in my app. After scanning the bar code of a book for example, how do I get things like the image, description, etc. from the scan result.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == RESULT_OK) {
IntentResult scanResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
} else if (resultCode == RESULT_CANCELED) {
showDialog("failed", "You messed up");
}
}
}
Thanks for your help
Zxing scans a variety of barcodes/QR codes, so the first thing you need to do is figure out if its a product UPC or a QR code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
String response = data.getAction();
if(Pattern.matches("[0-9]{1,13}", response)) {
// response is a UPC code, fetch product meta data
// using Google Products API, Best Buy Remix, etc.
} else {
// QR code - phone #, url, location, email, etc.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(response));
startActivity(intent);
}
}
}
There are a number of web services available that will return product meta data given a UPC code. A fairly comprehensive one would be Google's Search API for Shopping. For example, you can get a json representations of the product with UPC = 037988482481 with an URL that looks like this:
https://www.googleapis.com/shopping/search/v1/public/products?country=US&key=your_key_here&restrictBy=gtin:037988482481
You'll need to replace "your_key_here" with your Google API key.
Best Buy also offers a RESTful products API for all of the products they carry which is searchable by UPC code.
You'll want to use an AsyncTask to fetch the product metadata once you have the UPC.
You dont need the 'IntentResult' or 'IntentIntegrator' for that.
You can do this:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
And then in onActivityResult:
if (requestCode == 0) {
if ((resultCode == RESULT_CANCELED) || (resultCode == -1000)) {
Toast.makeText(WebViewActivity.this, "Nothing captured",
Toast.LENGTH_SHORT).show();
} else {
String capturedQrValue = data.getStringExtra("barcode_data");
}
}
With this you scan the barcode, now there is in the Zxing code another library that uses Google API for looking up that ISBN.
In the class Intents.java you have the info of what extras the intent needs and the class ISBNResultHandler shows what is the result.
Hope it helps someone in the future.
You can check what the Android ZXing app does. The source for the Android client is in: ZXing Android client source code. For ISBN numbers, the source code for handling that is: Android ZXing app's ISBN Result Handler code
For product and book search, the Android code invokes these two functions from ResultHandler.java:
// Uses the mobile-specific version of Product Search, which is formatted for small screens.
final void openProductSearch(String upc) {
Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
"/m/products?q=" + upc + "&source=zxing");
launchIntent(new Intent(Intent.ACTION_VIEW, uri));
}
final void openBookSearch(String isbn) {
Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
"/books?vid=isbn" + isbn);
launchIntent(new Intent(Intent.ACTION_VIEW, uri));
}
In your onActivityResult do like following code
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
String qrCode=result.getContents();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
You haven't called result.getContents().
I just want to add that if you want to be able to press back without a RuntimeException surround your if statement with a try catch block. so:
try{
/// get scanned code here
} catch(RuntimeException e) {
e.getStackTrace();
{
Hope that helped the inevitable crash you would face without it.

Categories

Resources