简体   繁体   English

在Android应用程序中嵌入ZXing

[英]Embedding ZXing in android app

So I'm another unlucky android development beginner who needs to have ZXing barcode scanner embedded in his app. 所以我是另一个不幸的Android开发初学者需要在他的应用程序中嵌入ZXing条码扫描器。

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. 有很多问题要求如何在stackoverflow上执行此操作,但没有一个问题的答案对于初学者来说是真正可以理解和解释的。 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". 所有答案都说“你需要做的就是构建ZXing项目的核心库,引用它你的项目,然后将一些代码从ZXing的android /目录复制到你的应用程序,你就完成了”。 But this is not very helpful for a noob. 但这对菜鸟来说并不是很有帮助。

I have ZXings core library referenced in my project. 我的项目中引用了ZXings核心库。 I have the Barcode Scanner app source open. 我打开了Barcode Scanner应用程序源。 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. 我认为如果你只需要从ZXing扫描仪复制一些文本就不难做到,你只需要了解它的代码。

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. 因此,如果有人可以解释这一点(告诉你要复制的代码部分,如何在onClick方法按钮中启动扫描仪等)请这样做,我相信有很多人会非常感谢这一点,就像我会是。

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. 这也是我的第一个(商业或其他)Android应用程序。 What I did was: 我做的是:

  • Get the ZXing project compiled and running on your machine. 在您的机器上编译并运行ZXing项目。 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. 使CoreA中的CaptureActivity尽可能简单。 All you need is the number returned from the core scanner code. 您只需要从核心扫描器代码返回的数字。 Here's a picture of the structure of my project: 这是我项目结构的图片:

适应性ZXing项目的结构

  • What you'll find is that you need to Modify 4 files for your modified Activity Class to work. 您会发现,您需要修改4个文件以使修改后的活动类生效。 These files are The CameraCaptureActivity class, The CaptureActivityHandler class, and the Decode Thread and Decode handler classes. 这些文件是CameraCaptureActivity类,CaptureActivityHandler类以及Decode Thread和Decode处理程序类。 I've hosted these files here . 我在这里托管了这些文件。

  • Take these four files and put them in a copy of your ZXing working project. 将这四个文件放入ZXing工作项目的副本中。 Remove the original CaptureActivity and the other Original Threads and Handler Classes that you have replaced. 删除原始的CaptureActivity以及已替换的其他原始线程和处理程序类。 (Ignore the CaptureActivityHandlerDemo file, as it was put up there by mistake) (忽略CaptureActivityHandlerDemo文件,因为它被错误地放在那里)

  • Change the package names to match the working ZXing package names. 更改包名称以匹配工作的ZXing包名称。 Make sure that your Activity is named the same in all four of these Classes. 确保所有这四个类中的Activity名称相同。 It might be a good idea to pass in an interface that implements "getHolder" rather than the Activity Class itself. 传入一个实现“getHolder”的接口而不是Activity类本身可能是个好主意。

  • 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. 确保您有一种导航到活动的方法 - 也许使其成为intent过滤器区域中的默认类。

  • 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 它被称为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. PS请发表任何问题作为对此答案的评论,我会尽力帮助。

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. 我将条形码扫描器分叉并将其转换为库项目,如果您只是通过IntentIntegrator扫描条形码,则删除了大多数不需要的功能。

The project with instructions is available at https://github.com/embarkmobile/zxing-android-minimal . 有关说明的项目可在https://github.com/embarkmobile/zxing-android-minimal获得

It is really easy to include it in your project if you're already using Maven. 如果您已经在使用Maven,那么将它包含在您的项目中非常容易。 If not - you can copy the code over to your project, using the approach described by CaspNZ. 如果不是 - 您可以使用CaspNZ描述的方法将代码复制到项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM