【问题标题】:Android - Barcode scannerAndroid - 条码扫描器
【发布时间】:2017-03-13 19:17:09
【问题描述】:

我需要创建一个应用程序,其中一项功能将具有条形码扫描仪。我正在寻找一些代码示例来制作条形码扫描仪,但我没有找到任何完整的示例代码。

我唯一找到的是一个适用于 Zxing 应用程序的代码示例。但我不想执行任何辅助应用程序。我想要一体机。

有人知道一些例子吗?

谢谢。

【问题讨论】:

  • 如果您觉得有用,请接受答案
  • 我看不出有一个很好的理由将它们合二为一。在 Android 中从另一个应用程序启动 Activity 是无缝的,如果您使用现有应用程序,则无需维护它。

标签: android barcode-scanner


【解决方案1】:

ZXing 是开源的!如果您真的想实现自己的条码扫描器,请查看源代码。

您可以在线浏览代码here,它被许可为 Apache License 2.0。

【讨论】:

    【解决方案2】:

    Zxing 具有出色的基于 Intent 的 API,旨在用作辅助应用程序。我建议检查用户是否安装了 Zxing 应用程序,如果没有,将他们重定向到 Google Play 商店进行下载。

    【讨论】:

      【解决方案3】:

      我知道我在这里回答已经很晚了,但是所有人都在寻找这个问题的最新答案,不再需要依赖第三方 api,Google 通过 Google Play Services 7.8 提供Barcode Scanning APIs。更多信息请参考CodeLabsDocumentationGithub Sample

      【讨论】:

        【解决方案4】:

        如果你想在你的应用中实现条码扫描器而不依赖于其他应用,你可以使用ZXing Android Embedded,你只需要在你的 gradle 依赖中声明它的依赖并在你的内部使用它的特性应用程序。

        要使用它,请将以下内容添加到您的 build.gradle 文件(项目/模块)中:

        repositories {
            jcenter()
        }
        
        dependencies {
            compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
            compile 'com.google.zxing:core:3.2.1'
            compile 'com.android.support:appcompat-v7:23.1.0'   // Version 23+ is required
        }
        
        android {
            buildToolsVersion '23.0.2' // Older versions may give compile errors
        }
        

        现在在您的代码中,您以这种方式开始扫描活动:

        public void scanBarcode() {
            IntentIntegrator integrator = new IntentIntegrator(this);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
            integrator.setPrompt("Scan the barcode");
            integrator.setCameraId(0);  // Use a specific camera of the device
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(true);
            integrator.initiateScan();        
        }
        

        并以这种方式处理结果:

        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
            if (scanResult != null && scanResult.getContents() != null) {
                String content = scanResult.getContents().toString();
                // content = this is the content of the scanned barcode
        
                // do something with the content info here
            }
        }
        

        更多信息可以在 ZXing Android Embedded github repo 中找到,链接如下。

        来源:https://github.com/journeyapps/zxing-android-embedded

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-25
          • 2011-12-21
          相关资源
          最近更新 更多