2011
01.15

Here’s a quick way to get the barcode scanner working on Android devices to use in your own apps. This assumes you are using Eclipse for development.

First you’ll need to make a new project and put these assets with your src folder. (I got them from ZXing)

In your main .java file (mine is just called BarcodeTest), put this function where you want to scan something:

IntentIntegrator.initiateScan(this);

And then put this function in the same file:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
String upc = scanResult.getContents();
 
//put whatever you want to do with the code here
TextView tv = new TextView(this);
tv.setText(upc);
setContentView(tv);
 
}
}
break;
}
}
}

In this example, it’s just showing the code in a TextView after it’s scanned.

UPDATE: A user named Justin emailed me a question and the answer might help a few of you.

Hello I saw your blog and I was wondering if you could send me some links on how I can scan a UPC and use google to search for product information such as name, pic ,sizes and input them to an online db using MySQL. I have my lamp server set up and online I just don’t know do to automate the data entry into the db. I have entering in the data by hand. Any help will be most welcome thanks

Justin

Answer:

I’m not sure how you would get it into the database, but I’ve used the Google Shopping API to get product data after the UPC code is scanned. The Search API might work too.

I found this, it’s a basic tutorial for getting mysql data to echo out on an Android device. If you know anything about mysql then this would be a good start. I would start with messing around with the st.executeQuery() function on their example. Write a different query and see what happens, etc.

Just follow a procedure like this:

1. Scan UPC code
2. Search using Google API to find product data
3. If there are results, parse the XML/JSON
4. Push results to database using the query.

Let me know if you have any issues.

58 comments so far

Add Your Comment
  1. Thanks for posting this. I’m a novice android developer and would like to use a scanner to automatically enter UPC data into a database stored on the local android device.

    How would I go about using the UPC data to store the product name and price as fields in a database? The overall mission is to store the data of products for a user so he can see what he’s been purchasing… e.g., last month I spent XX dollars on this brand of shirt?

    Jim

    • Do you need to store it locally?

      Why not use Zend and use PHP to save it to a MySQL database online somewhere so they can retrieve the data anywhere?

      • Yeah, retrieving it anywhere would be a nice feature. I haven’t heard of Zend.

        I’ve tried scanning the UPC of a lotto ticket (for example), but ZXing barcode scanner only recognizes the corresponding number associated with the barcode… no product or price information. So, I might have to have users enter their products manually. =(

        • Actually, you can put the upc code into a google search bar and i’ll bring up the product. it might help with getting more info on an item.

          • Yep, I used Google Search to find a image, a description, and put it in a database.

  2. This works like a charm. Thanks!

    • Hey can please send me the barcode scannar source code. i have zxing code but it is having some error.. so please i want.. its a request.

  3. Thanks..It Work for me!

  4. I am having LOTS of trouble getting my app to scan a barcode. I have spent hours and hours of research and trail and error and am not understanding what I need to do or what I am doing WRONG! I am just trying to develop a small app that scans UPC barcodes built in a Sq-lite database. Could someone possibly give me their email to look at my code and shed me some light? My project is due in 3 weeks, and I need some serious help! Please

  5. That’s really cool post to share and I think such bar codes for Android are adding extra protection.

  6. can you attach the full source code of your main.java?
    i got problem with this line..
    “IntentIntegrator.initiateScan(this);”

    thanks!

    • Use like this:

      public class BarcodeScanActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      IntentIntegrator.initiateScan(this);
      }

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      switch (requestCode) {
      case IntentIntegrator.REQUEST_CODE: {
      if (resultCode != RESULT_CANCELED) {
      IntentResult scanResult = IntentIntegrator.parseActivityResult(
      requestCode, resultCode, data);
      if (scanResult != null) {
      String upc = scanResult.getContents();

      // put whatever you want to do with the code here
      TextView tv = new TextView(this);
      tv.setText(upc);
      setContentView(tv);
      }
      }
      break;
      }
      }
      }
      }

      • please call back if u r interesterd in developing a simple app for my college project …+9187222223230…obviously not for free :-/

      • i used the same code but i get an error while using IntentIntegrator.initiateScan(this); as Cannot be resolved

      • even i also added the external jar files

  7. Thanks a lot. It worked great and I was able to build my very simple barcode scanner app in less than an hour.

  8. Its great!! thanks a lot.. πŸ˜€

  9. Thanks a lot for this…
    It worked well for me..

  10. thanks alot.. u save my life..

  11. do i have to install the barcode scanner app from the market before using this code? cus i got this message when running your code: this application requires barcode scanner… etc
    help please?

  12. Hi, i am new to android.
    so i just copied all the code snippets n pasted.
    when i run the emulator with API 14(android 4.0) it says you need a barcode scanner, do you want to install?…i clicked yes, and the it says “unfortunately the app has stopped” and my log cat says:
    “05-01 15:50:15.089: E/AndroidRuntime(612): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://search?q=pname:com.google.zxing.client.android }”
    can any one help me with this, moreover is there no need of any layout…except the default main layout?
    and one last thing, can i test whether it is working properly with an emulator or i need an android device to test it.

  13. This is really great. Thanks for sharing.
    But when I can a code, I have to click the back button on phone to view the content of the code. Is there a way to automatically show the scanned content?
    Thanks in advance,
    Hasala

  14. thanks a lot.. for giving this example.

  15. plz tell me what code i write in Android manifest file bcz when i run this application in emulator it shows some error like
    ERROR/Android Run time(470): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://search?q=pname:com.google.zxing.client.android }

    ERROR/Android Runtime(470): IntentIntegrator$1.onClick(IntentIntegrator.java:199)

  16. Hello. I am a beginner in java. But i know a lot of c++, C#.
    Can u help me with this project?

  17. Plz reply!!!! I’m also getting the error saying “This application requires Barcode scanner.Would you like to install it?” what shud I do now???

  18. i want to capture an image of barcode using camera. and than want to scan it and show the results in textview.
    any help plz…???

  19. could some one help with the barcode.I am really new to the barcode scanner concept.i want to know completely from the starting should we need to have the barcode image sotred in our databese aned retrive it.i didnt get the code that was given at the top.what should be the xml file and the code given where should i place it could some one help me plz i am in a great need of the barcode scanner.So some plz kindly help me out with the barcode scanner…………

    Thanks a alot in advance

  20. Hello sir
    can you tell me how to get product info database included UPC Code and other info

  21. hi i m new to developing an android project
    can anybody provide me some list of projects that i can develope for my sem dissertation
    i m thinking of barcode scanner
    ur help will be useful

  22. Hi.i am an android developer.i have got the source code for scanning the barcode and get UPC from it. But i want to send this UPC to database and authenticate with it? how could it possible?…using my sql or something? please give me a basic idea.

    Thanks in advance

    • Is it off your own DB? Run a query when the code gets scanned and pass the upc var. I’m not sure how to run SQL stuff on Android so I can’t really help you there. :( Sorry.

  23. it works like magic πŸ˜€ Thanks a lot Adam πŸ˜€

    • No problem! πŸ˜€

  24. i want to capture an image of barcode using camera. and than want to scan it and show the results in textview.
    any help plz…???

  25. hey i am not aware about android and me n my project partner decided to create application which is based on android he is having some knowledge about android but now he tells that he is don’t know how to scan and search barcode can using this code we are completely get the basic barcode scanner or there is any need for DB in this application????
    Replay fast its important

  26. Hello I am jst starting to work on andrid apps.. but i cant do anything. though I read the books but when i jst putting the codes in the file which are given in any sites… but still there hv so many errors… i cant fix this.. plz help me guys…

    my mail id is sumitsenece@gmail.com..
    If u hv any module on how to create android application at the basic steps then plz mail me.. plz plz..

  27. Hi all i am new to android so acn you suggest me step by step procedure to help me…please i am in an urgent need please help me

  28. Hi Adam,

    It doesn’t work with the Eclipse emulator, it crashes when it tries to install the Barcode App. Should it work with the emulador or only with a real device?

    Thanks in advance

    • hi edu..
      just for your info.. features like barcode scanning, camera and etc wont work in emulator. We need to use actual device for it. Best of luck..

  29. thanks a lot Adam.. It works like a charmm!!

  30. But, how do i make the barcode scan feature works like this:
    1) capture the barcode as image
    2) show the barcode result (number, code format, type, time)
    3) buttons to search for the code on web
    (all in 1 page)

  31. I am also in the process of learning how to create a code scanning app for Android, however after using the steps and code above in Eclipse, have had no luck. Does anyone know of a step-by-step process on how to create such an app, maybe on multiple platforms; not just Android?

  32. Does it work for EAN13 as well

  33. How do i create a database for barcode scanning apps????

  34. Hey Adam,
    Above in the answer you provided to Justin you mentioned you were able to search Google Shopping API after scanning the UPC. I have been trying to do the same thing for weeks now and I admit I have little experience. If you could point me to a good example or possibly let me know how you accomplished this I would be very grateful.

  35. I want to develop a simple application to scan barcode. can u please provide me a sample code.

  36. Thanks a lot.. it’s really help me πŸ˜€

  37. IntentIntegrator.initiateScan(this);
    when i use this line i get error as intentIntegrator cannot be resolved

  38. Hi Adam,

    Great module, easily integrated… you da man!

  39. PLEASE RESPOND SOON>>>>>>>>>>>>
    Hey im trying to create an app… and i need to do something. I need to push a button then i need it to take me to a scan screen and scan a object then tell what the object is? Do you know the input code for eclipse. I only really need the scan code and the code to tell me what the object is? I cant seem to find it anywhere please help me.

    P.S if it works i could pay you about $30

  40. When i run the apk in my device is just a black screen and after a couple of seconds it stop working. Is there any lack of the program? and just a little question what should i put in the main.xml?

  41. This is really great. I want to invoke the scan from a Click Event of a Button, so I add this code on my main Java File:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnRead = (Button)findViewById(R.id.button1);
    btnRead.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    IntentIntegrator.initiateScan(this);
    }
    });
    }

    Shows an error: The method initiateScan(Activity) in the type IntentIntegrator is not applicable for the arguments (new View.OnClickListener(){})

    How can I put this actually to work inside the Click Listener?

    Thanks in advance.

    Greets.

    • change “this” to ACTIVITYNAME.this ex. IntentIntegrator.initiateScan(ScanBarcodeActivity.this);

  42. Create an Android QR Code Generator,
    http://www.keepautomation.com/products/android_barcode/barcodes/qrcode.html

  43. Hello
    when i click the button to open the camera i get β€œThis application requires Barcode scanner.Would you like to install it?” what should i do?

  44. The data scanned via the Android can be pass using url..and then use PHP script to get the data which then will be search or add into the MySQL db

  45. Good sharing! I followed your tutorial and create the barcode scanner successfully. Thanks a lot!