android - 如何将 ZXing 库集成到 Android Studio 进行条码扫描?

标签 android ide dependencies zxing core

我一直在互联网上寻找如何将 zxing 库包含到我的项目中,我找到了这个教程:http://blog.dihaw.com/integrating-zxing-in-your-android-app-as-standalone-scanner/

但是当我到达需要检查 BeepManager 以添加 R 导入的地步时,我的项目(即使在 MainActivity 上)中出现各种错误,无法找到 R。

我也找到了这个 https://github.com/journeyapps/zxing-android-embedded/blob/master/README.md这似乎更容易,因为它是由 gradle 自动集成的,但是当我同步它时会弹出一个错误,它找不到文件。

任何帮助将不胜感激 :) 我是 Android Studio 的新手。

编辑:

我将第二种方法的设置(带有 gradle 设置的那个)添加到我的 build.gradle 并弹出 4 个错误:

Error:Failed to find: com.embarkmobile:zxing-android-legacy:2.0.0 
Error:Failed to find: com.google.zxing:core:3.0.1 
Error:Failed to find: com.embarkmobile:zxing-android-integration:2.0.0 
Error:Failed to find: com.embarkmobile:zxing-android-minimal:2.0.0

有什么帮助吗?

---答案---

为了解决这个问题,我需要在 Gradle 上禁用 Offline Work。

  • Android Studio 的设置>Gradle>取消勾选“离线工作”

最佳答案

您需要将以下内容添加到您的 build.gradle 文件中:

repositories {
    mavenCentral()

    maven {
        url "http://dl.bintray.com/journeyapps/maven"
    }
}

dependencies {
    // Supports Android 4.0.3 and later (API level 15)
    compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'

    // Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.
    // If you only plan on supporting Android 4.0.3 and up, you don't need to include this.
    compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'

    // Convenience library to launch the scanning and encoding Activities.
    // It automatically picks the best scanning library from the above two, depending on the
    // Android version and what is available.
    compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'

    // Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.
    // This mostly affects encoding, but you should test if you plan to support these versions.
    // Older versions e.g. 2.2 may also work if you need support for older Android versions.
    compile 'com.google.zxing:core:3.0.1'
}

我的 build.gradle 文件如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.myapplication2"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()

    maven {
        url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    // Supports Android 4.0.3 and later (API level 15)
    compile 'com.embarkmobile:zxing-android-minimal:2.0.0@aar'

    // Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.
    // If you only plan on supporting Android 4.0.3 and up, you don't need to include this.
    compile 'com.embarkmobile:zxing-android-legacy:2.0.0@aar'

    // Convenience library to launch the scanning and encoding Activities.
    // It automatically picks the best scanning library from the above two, depending on the
    // Android version and what is available.
    compile 'com.embarkmobile:zxing-android-integration:2.0.0@aar'

    // Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.
    // This mostly affects encoding, but you should test if you plan to support these versions.
    // Older versions e.g. 2.2 may also work if you need support for older Android versions.
    compile 'com.google.zxing:core:3.0.1'
}

代码是here .

另外,关于如何使用它,请引用我在 Stackoverflow here 上的回答

它包含我测试过的方法和简单代码。

关于android - 如何将 ZXing 库集成到 Android Studio 进行条码扫描?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27851512/

相关文章:

c - Visual Studio - C 编程

delphi - 在代码编辑器中折叠 If-Then-Else 语句 (Delphi 2007)

java - 如何获取项目中任何项目不再需要的类列表

java - 错误 : package com. google.android.gms.drive 不存在

java - Android - 按住按钮时重复功能

android - WSO2EMM - 应用程序管理从不返回安装/未安装应用程序的用户列表

java - ConstraintLayout View 大小 - 与父级的比例

android - 可反射的 flutter 反射 : working example needed

eclipse - IntelliJ中的快捷键等效于Ctrl + Shift + G

java - Maven:我的项目有一个不在任何 Maven 仓库(系统范围)中的依赖项。它会包含在我的最终 jar 中吗?