This question already has answers here:
I ve been searching for how to add a barcode scanner to my app. Are there any examples or how can I do this easily?
This question already has answers here:
I ve been searching for how to add a barcode scanner to my app. Are there any examples or how can I do this easily?
The ZXing project provides a standalone barcode reader application which — via Android s intent mechanism — can be called by other applications who wish to integrate barcode scanning.
The easiest way to do this is to call the ZXing SCAN
Intent
from your application, like this:
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
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
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
Pressing the button linked to mScan
would launch directly into the ZXing barcode scanner screen (or crash if ZXing isn t installed). Once a barcode has been recognised, you ll receive the result in your Activity
, here in the contents
variable.
To avoid the crashing and simplify things for you, ZXing have provided a utility class which you could integrate into your application to make the installation of ZXing smoother, by redirecting the user to the Android Market if they don t have it installed already.
Finally, if you want to integrate barcode scanning directly into your application without relying on having the separate ZXing application installed, well then it s an open source project and you can do so! :)
Edit: Somebody edited this guide into this answer (it sounds a bit odd, I can t vouch as to its accuracy, and I m not sure why they re using Eclipse in 2015):
Step by step to setup zxing 3.2.1 in eclipse
I had a problem with implementing the code until I found some website (I can t find it again right now) that explained that you need to include the package name in the name of the intent.putExtra.
It would pull up the application, but it wouldn t recognize any barcodes, and when I changed it from.
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
to
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
It worked great. Just a tip for any other novice Android programmers.
Using the provided IntentInegrator is better. It allows you to prompt your user to install the barcode scanner if they do not have it. It also allows you to customize the messages. The IntentIntegrator.REQUEST_CODE constant holds the value of the request code for the onActivityResult to check for in the above if block.
IntentIntegrator intentIntegrator = new IntentIntegrator(this); // where this is activity
intentIntegrator.initiateScan(IntentIntegrator.ALL_CODE_TYPES); // or QR_CODE_TYPES if you need to scan QR only
Using Zxing this way requires a user to also install the barcode scanner app, which isn t ideal. What you probably want is to bundle Zxing into your app directly.
I highly recommend using this library: https://github.com/dm77/barcodescanner
It takes all the crazy build issues you re going to run into trying to integrate Xzing or Zbar directly. It uses those libraries under the covers, but wraps them in a very simple to use API.
If you want to include into your code and not use the IntentIntegrator that the ZXing library recommend, you can use some of these ports:
I use the first, and it works perfectly! It has a sample project to try it on.
Barcode Detection is now available in Google Play services. Code lab of the setup process, here are the api docs, and a sample project.
You can use this quick start guide http://shyyko.wordpress.com/2013/07/30/zxing-with-android-quick-start/ with simple example project to build android app without IntentIntegrator.
I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...
I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...
I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...
I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...
To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?
When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...
I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...
I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...