embed – Embedding ZXing in android app-ThrowExceptions

Exception or error:

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.

How to solve:

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.

###

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.

Leave a Reply

Your email address will not be published. Required fields are marked *