使用 ZXing 创建 Android 条码扫描应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2050263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:22:33  来源:igfitidea点击:

Using ZXing to create an Android barcode scanning app

androidbarcodebarcode-scannerzxing

提问by wajiw

I've been searching for how to add a barcode scanner to my app. Are there any examples or how can I do this easily?

我一直在寻找如何将条码扫描仪添加到我的应用程序中。是否有任何示例或我如何轻松做到这一点?

回答by Christopher Orr

The ZXing project provides a standalone barcode reader application which — via Android's intent mechanism — can be called by other applications who wish to integrate barcode scanning.

ZXing 项目提供了一个独立的条码阅读器应用程序——通过 Android 的意图机制——可以被其他希望集成条码扫描的应用程序调用。

The easiest way to do this is to call the ZXing SCANIntentfrom your application, like this:

最简单的方法是SCANIntent从您的应用程序调用 ZXing ,如下所示

public Button.OnClickListener mScan = new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }
};

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}

Pressing the button linked to mScanwould launch directly into the ZXing barcode scanner screen (or crash if ZXing isn't installed). Once a barcode has been recognised, you'll receive the result in your Activity, here in the contentsvariable.

按下链接到的按钮mScan将直接启动到 ZXing 条码扫描仪屏幕(如果未安装 ZXing,则会崩溃)。识别出条形码后,您将Activitycontents变量中收到结果。

To avoid the crashing and simplify things for you, ZXing have provided a utility classwhich you could integrate into your application to make the installation of ZXing smoother, by redirecting the user to the Android Market if they don't have it installed already.

为了避免崩溃并为您简化事情,ZXing提供了一个实用程序类,您可以将其集成到您的应用程序中,使 ZXing 的安装更加顺畅,如果用户尚未安装它,则将用户重定向到 Android Market。

Finally, if you want to integrate barcode scanning directly into your application without relying on having the separate ZXing application installed, well then it's an open source project and you can do so! :)

最后,如果您想将条码扫描直接集成到您的应用程序中,而不依赖于安装单独的 ZXing 应用程序,那么它是一个开源项目,您可以这样做!:)



Edit:Somebody edited this guide into this answer (it sounds a bit odd, I can't vouch as to its accuracy, and I'm not sure why they're using Eclipse in 2015):

编辑:有人将本指南编辑为这个答案(听起来有点奇怪,我不能保证其准确性,而且我不确定他们为什么在 2015 年使用 Eclipse):

Step by step to setup zxing 3.2.1 in eclipse

在eclipse中一步一步设置zxing 3.2.1

  1. Download zxing-master.zip from "https://github.com/zxing/zxing"
  2. Unzip zxing-master.zip, Use eclipse to import "android" project in zxing-master
  3. Download core-3.2.1.jar from "http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/"
  4. Create "libs" folder in "android" project and paste cor-3.2.1.jar into the libs folder
  5. Click on project: choose "properties" -> "Java Compiler" to change level to 1.7. Then click on "Android" change "Project build target" to android 4.4.2+, because using 1.7 requires compiling with Android 4.4
  6. If "CameraConfigurationUtils.java" don't exist in "zxing-master/android/app/src/main/java/com/google/zxing/client/android/camera/". You can copy it from "zxing-master/android-core/src/main/java/com/google/zxing/client/android/camera/" and paste to your project.
  7. Clean and build project. If your project show error about "switch - case", you should change them to "if - else".
  8. Completed. Clean and build project. You can click on "Proprties" > "Android" > click on "Is Libraries" to use for your project.
  1. 从“ https://github.com/zxing/zxing”下载zxing-master.zip
  2. 解压zxing-master.zip,使用eclipse在zxing-master中导入“android”项目
  3. 从“ http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/”下载core-3.2.1.jar
  4. 在“android”项目中创建“libs”文件夹并将 cor-3.2.1.jar 粘贴到 libs 文件夹中
  5. 单击项目:选择“属性”->“Java 编译器”将级别更改为 1.7。然后点击“Android”将“Project build target”改为android 4.4.2+,因为使用1.7需要用Android 4.4编译
  6. 如果“zxing-master/android/app/src/main/java/com/google/zxing/client/android/camera/”中不存在“CameraConfigurationUtils.java”。您可以将其从“zxing-master/android-core/src/main/java/com/google/zxing/client/android/camera/”复制并粘贴到您的项目中。
  7. 清理并构建项目。如果您的项目显示关于“switch - case”的错误,您应该将它们更改为“if - else”。
  8. 完全的。清理并构建项目。您可以单击“属性”>“Android”> 单击“是库”以用于您的项目。

回答by user496827

I had a problem with implementing the code until I found some website (I can't find it again right now) that explained that you need to include the package name in the name of the intent.putExtra.

我在实现代码时遇到了问题,直到我找到了一些网站(我现在找不到它),该网站解释说您需要在 intent.putExtra 的名称中包含包名称。

It would pull up the application, but it wouldn't recognize any barcodes, and when I changed it from.

它会启动应用程序,但它无法识别任何条形码,并且当我更改它时。

intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

to

intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");

It worked great. Just a tip for any other novice Android programmers.

它工作得很好。只是给任何其他 Android 新手程序员的提示。

回答by Yack

Using the provided IntentInegrator is better. It allows you to prompt your user to install the barcode scanner if they do not have it. It also allows you to customize the messages. The IntentIntegrator.REQUEST_CODE constant holds the value of the request code for the onActivityResult to check for in the above if block.

使用提供的 IntentInegrator 更好。它允许您提示您的用户安装条码扫描器(如果他们没有的话)。它还允许您自定义消息。IntentIntegrator.REQUEST_CODE 常量保存了 onActivityResult 的请求代码的值,以便在上面的 if 块中进行检查。

IntentIntegrator intentIntegrator = new IntentIntegrator(this); // where this is activity 
intentIntegrator.initiateScan(IntentIntegrator.ALL_CODE_TYPES); // or QR_CODE_TYPES if you need to scan QR

IntentIntegrator.java

IntentIntegrator.java

回答by Aracem

If you want to include into your code and not use the IntentIntegrator that the ZXing library recommend, you can use some of these ports:

如果您想包含到您的代码中而不使用 ZXing 库推荐的 IntentIntegrator,您可以使用其中一些端口:

I use the first, and it works perfectly! It has a sample project to try it on.

我用的是第一个,效果很好!它有一个示例项目可以试用。

回答by Michael Peterson

Using Zxing this way requires a user to also install the barcode scanner app, which isn't ideal. What you probably want is to bundle Zxing into your app directly.

以这种方式使用 Zxing 需要用户还安装条码扫描仪应用程序,这并不理想。您可能想要的是将 Zxing 直接捆绑到您的应用程序中。

I highly recommend using this library: https://github.com/dm77/barcodescanner

我强烈推荐使用这个库:https: //github.com/dm77/barcodescanner

It takes all the crazy build issues you're going to run into trying to integrate Xzing or Zbar directly. It uses those libraries under the covers, but wraps them in a very simple to use API.

它需要您尝试直接集成 Xzing 或 Zbar 时遇到的所有疯狂构建问题。它在幕后使用这些库,但将它们包装在一个非常易于使用的 API 中。

回答by Jim Baca

Barcode Detection is now available in Google Play services. Code labof the setup process, here are the api docs, and a sample project.

条形码检测现已在Google Play 服务中可用。 设置过程的代码实验室,这里是api 文档示例项目

回答by shyyko.serhiy

You can use this quick start guide http://shyyko.wordpress.com/2013/07/30/zxing-with-android-quick-start/with simple example project to build android app without IntentIntegrator.

您可以使用此快速入门指南http://shyyko.wordpress.com/2013/07/30/zxing-with-android-quick-start/和简单的示例项目来构建没有 IntentIntegrator 的 android 应用程序。