22

So I'm another unlucky android development beginner who needs to have ZXing barcode scanner embedded in his app.

There is plenty of questions asking how to do this here on stackoverflow but none of them has an answer that is really understandable and explanatory for a beginner. All the answers say something like "all you have to do is build the core lib of ZXing project, reference it your project and then copy some code from the ZXing's android/directory to your app and you're done". But this is not very helpful for a noob.

I have ZXings core library referenced in my project. I have the Barcode Scanner app source open. I'm trying to read and understand the code of Barcode Scanner app but it is too complex for my level of knowledge.

I just want to have a button in my app that, when pressed, opens a barcode scanner, the scanner should only be able to scan a barcode, decode it and return me the numbers, it doesn't need to send the code anywhere to get any info etc, ill take care of these things myself. I just need a simple scanner that starts on button click, scans the code and gives me the result. But i can't figure how to do it myself. I assume this shouldn't be hard to do if you only need to copy some text from the ZXing scanner, you just need to understand its code.

So if someone can explain this (tell which parts of code to copy, how to start the scanner in a buttons onClick method etc) please do so, I'm sure there's plenty of people who will be really thankful for this just as i will be.

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
boogieman
  • 567
  • 2
  • 5
  • 10

2 Answers2

22

I had to do exactly what you are being asked to do. It wasn't that easy, but it wasn't too bad either. It was also my very first (commercial or otherwise) Android app. What I did was:

  • Get the ZXing project compiled and running on your machine. There is a good tutorial on how to do this here.
  • Adapt this code to suit your needs. I stripped a lot of the project away to just the basic scanner. I then built the rest of my project around it. Here's how to do that.

  • Adapt the CaptureActivity in core to be as simple as possible. All you need is the number returned from the core scanner code. Here's a picture of the structure of my project:

Structure of Adapted ZXing project

  • What you'll find is that you need to Modify 4 files for your modified Activity Class to work. These files are The CameraCaptureActivity class, The CaptureActivityHandler class, and the Decode Thread and Decode handler classes. I've hosted these files here.

  • Take these four files and put them in a copy of your ZXing working project. Remove the original CaptureActivity and the other Original Threads and Handler Classes that you have replaced. (Ignore the CaptureActivityHandlerDemo file, as it was put up there by mistake)

  • Change the package names to match the working ZXing package names. Make sure that your Activity is named the same in all four of these Classes. It might be a good idea to pass in an interface that implements "getHolder" rather than the Activity Class itself.

  • Make sure you have updated your manifest with any changes to your Activity Name. Make sure you have a way of navigating to the activity - perhaps make it the default class in the intent filter area.

  • I have included a layout file for your activity also - it's very simple, but it's all your need to get the scanner up and working. it's called camera_capture.xml

Hopefully this is all you need to get up and running. It's not an easy process and unfortunately I can't give you my entire project as it's a commercial product.

Good Luck!!

PS please post any questions as comments on this answer and I'll do my best to help.

Community
  • 1
  • 1
Caspar Harmer
  • 8,097
  • 2
  • 42
  • 39
  • Thanks a lot CaspNZ, your answer is really helpful and i think i almost made it work but i still got some problems. I copied your stripped down classes, corected package names and manifest file. I also changed the CameraCaptureActivity to extend Activity instead of Rotateable activity and made it to not implement IParentActivity because i cant find these classes anywhere. Now when i start the captureactivity from my apps main activity the scanner shows up on the screen but i does nothing, i just see the camera view but it doesnt scan codes. – boogieman May 12 '11 at 13:52
  • Ok, there's a few possibilities here - 1, the scanner isn't being initialized (make sure that this is happening in your onCreate method and your onResume methods), 2, a callback is failing for some reason and 3 There is an error or a code change that is preventing the callback from happening. – Caspar Harmer May 12 '11 at 20:59
  • Check either the Handler or the Thread class to make sure that all code types are being scanned. I think I disabled all codes other than QR codes. Put some breakpoints on the handler and thread classes to see if the code is getting there. Let me know if this helps. – Caspar Harmer May 12 '11 at 21:02
  • Oh and sorry about the Rotateable Activity and the Interfaces - I forgot to remove them... – Caspar Harmer May 12 '11 at 21:02
  • It works! It did work from the beginning its just i didnt handle the decode in handleDecode method so there was no beep or any other indications that the barcode is scanned so i thought it doesnt scan the barcode at all, stupid me :D Thanks a lot CaspNZ, I really appreciate your help. – boogieman May 13 '11 at 10:45
  • Hi, thank you very much for this detailed guide. Could you please explain how to call the scan activity so i will be able to get the results? Now my application starts with another layout. Is there a way to add a simple onClick code to get the scanned information? – Marco Jun 04 '11 at 16:34
  • Is it possible to put the scanner in a fragment using this method? I've managed to use the scanner full screen without needing to install BarcodeScanner but cannot find a way to shrink it in to a small box. There are github libraries that do this but none I can find that support the barcode format I need. – Declan McKenna Jul 17 '14 at 09:36
7

I forked the Barcode Scanner and converted it into a library project, and removed most of the functionality you don't need if you're only scanning barcodes via IntentIntegrator.

The project with instructions is available at https://github.com/embarkmobile/zxing-android-minimal.

It is really easy to include it in your project if you're already using Maven. If not - you can copy the code over to your project, using the approach described by CaspNZ.

Ralf
  • 14,655
  • 9
  • 48
  • 58