Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding ZXing in android app

Tags:

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.

like image 485
boogieman Avatar asked May 11 '11 19:05

boogieman


People also ask

What is ZXing Android Embedded?

ZXing Android Embedded is a barcode scanning library for Android, using ZXing for decoding. The project is loosely based on the ZXing Android Barcode Scanner application but is not affiliated with the official ZXing project. By default, only SDK 24+ will work, even though the library specifies 19 as the minimum version.

How to use ZXing library in Android with Gradle?

Note: Choose API 24 and onwards as Minimum SDK . In order to use the Zxing library in our application we need to add it’s dependency in our application’s gradle file. For adding the dependency Go to Gradle Scripts > build.gradle (Module: app) and add the following dependencies.

How does Google use ZXing?

Google uses ZXing by web search to obtain millions of barcodes on the web indexable. It also creates the foundation of Android’s Barcode Scanner app and is combined into Google Product and Book Search.

How to convert ZXing project to an app?

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.


2 Answers

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.

like image 89
Caspar Harmer Avatar answered Oct 22 '22 11:10

Caspar Harmer


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.

like image 23
Ralf Avatar answered Oct 22 '22 10:10

Ralf