Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

embed - Embedding ZXing in android app

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...