【问题标题】:How to import ZXING to android studio?如何将 ZXING 导入到 android studio?
【发布时间】:2015-06-13 08:51:42
【问题描述】:

我使用安卓工作室 我想在我的应用程序中导入'ZXING',我找到了很多文章并找到了以下站点

https://github.com/zxing/zxing/

我下载了ZIP并解压,找到了一些教程 但是好像没有说的太详细,需要导入什么?实现二维码扫描

我还是不知道怎么做


4/14 我尝试了提供的列侬 URL “zxing-android-最小” 并导入'gradle-wrapper.jar'

但是当我写 新的 IntentIntegrator (this) .initiateScan (); 仍然出现“Can not resolve symbol 'IntentIntegrator”消息

https://www.dropbox.com/s/2srga9iq75iqe4m/%E8%9E%A2%E5%B9%95%E6%88%AA%E5%9C%96%202015-04-10%2001.33.56.png?dl=0

我确实有一个正确的 '.jar 选择添加为库 但是当出现错误时,他似乎没有被添加


4/10

终于不再出现“Can not resolve symbol 'IntentIntegrator” 这是代码,我哪里错了?

我删除了新的 IntentIntegrator(this) .initiateScan(); '应用程序正常运行

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new IntentIntegrator(this).initiateScan();
}

我的'build.greadle'

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

【问题讨论】:

  • 您不应该将任何东西“导入”到您的项目中。通过 Maven 使用核心库。不要将android 复制到您的项目中。您遇到了麻烦,因为这不是您打算使用它的方式。
  • 你的意思是,我实际上不需要将任何文件导入我的项目?

标签: java android import android-studio zxing


【解决方案1】:

你应该在 build.gradle 文件中定义你的 zxing 依赖:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.google.zxing:core:3.2.0'
}

这是核心条码编码/解码库,您可以使用它来构建您的自定义条码扫描器/生成器应用。

如果您只需要支持一个简单的条码扫描案例,您可以使用ZXing Android Embedded项目嵌入ZXing Android Barcode Scanner应用程序。

这是 ZXing Android Barcode Scanner 应用程序作为 Android 库项目的一个端口,用于嵌入到其他 Android 应用程序中。

如果您决定使用 ZXing Android Embedded 项目,只需在 build.gradle 文件中定义依赖项即可:

repositories {
    mavenCentral()

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

dependencies {
    implementation 'com.journeyapps:zxing-android-embedded:2.3.0@aar'
    implementation 'com.journeyapps:zxing-android-legacy:2.3.0@aar'
    implementation 'com.journeyapps:zxing-android-integration:2.3.0@aar'
    implementation 'com.google.zxing:core:3.2.0'
}

使用默认选项启动意图:

new IntentIntegrator(this).initiateScan(); // `this` is the current Activity

然后得到你的结果:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
    case IntentIntegrator.REQUEST_CODE:
        if (resultCode == Activity.RESULT_OK) {
            // Parsing bar code reader result
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        }
        break;
    }
}

【讨论】:

    【解决方案2】:

    当我使用 zxing 库开发我的应用程序时,我遇到了很多麻烦。所以看看这个zxing minimum:https://github.com/Promptus/zxing-android-minimal/tree/master

    它对我来说效果很好,而且更容易实现。

    编辑:

    在你的项目中打开这个文件:

    /gradle/wrapper/gradle-wrapper.properties

    编辑 distributionUrl 行并设置它:

    distributionUrl=http://services.gradle.org/distributions/gradle-1.8-all.zip 重建您的项目。

    更新:您现在可能想使用 gradle-2.1-all.zip。

    新编辑:

    首先,您必须删除您的libs 文件。然后你必须删除

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

    来自您的 build.gradleMyApplication,因为该 gradle 用于整个项目,最好在每个模块中使用它。

    之后,打开模块appbuild.gradle,添加如下代码:

    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'
    
        // Zxing libraries
        compile 'com.embarkmobile:zxing-android-minimal:2.0.0@aar'
        compile 'com.embarkmobile:zxing-android-integration:2.0.0@aar'
        compile 'com.google.zxing:core:3.0.1'
    
    }
    

    最后,你需要从你的项目中删除google.zxing.integration.android,否则编译时会报错。

    更新:

    要解决后退按钮问题,可以执行以下代码:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (resultCode == RESULT_OK) {
    
            String _code = data.getStringExtra("SCAN_RESULT");
    
            // do whatever you want
    
        }
    
    }
    

    【讨论】:

    • 谢谢你我还在尝试我按照说明导入了gradle-wrapper.jar,但仍然出现Can not resolve symbol 'IntentIntegrator' (Already added in build.gradle)
    • 您添加了maven 存储库吗?如:repositories { mavenCentral() maven { url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/" } }
    • 是的,我要加上那句话,而且,在添加内容依赖的时候还有其他错误{},我会在文章中更新图片
    • 您使用的 gradle-wrapper.jar 版本不包含命令 compile()。因此,您必须改用新版本。顺便说一句,你不需要输入compile 'com.embarkmobile:zxing-android-legacy:2.0.0@aar',我也认为你不需要使用 gradle-wrapper.jar,因为你已经安装了 gradle。
    • 是旧的gradle-wrapper.jar所在的wrapper文件夹吗?我在哪里可以找到新版本?
    【解决方案3】:

    在你的 root-build.gradle 中:

    repositories {
       mavenCentral()
    
       maven {
          url "http://dl.bintray.com/journeyapps/maven"
       }
    }
    

    你的 app-build.gradle 中的一个:

    dependencies {
        // Supports Android 4.0.3 and later (API level 15)
        compile 'com.journeyapps:zxing-android-embedded:2.3.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.journeyapps:zxing-android-legacy:2.3.0@aar'
    
        // Convenience library to launch the scanning 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.3.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.2.0'
    }
    

    更多信息可以在这里找到:https://github.com/journeyapps/zxing-android-embedded

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题,我按照如下所示的简单步骤解决了:

      使用选项Import project (Eclipse ADT, Gradle, etc.) 从下载的zxing-master zip 文件中导入项目android,并在您的应用级别build.gradle 文件中添加以下两行代码,然后您就可以运行了。

      这么简单,是啊……

      dependencies {
              // https://mvnrepository.com/artifact/com.google.zxing/core
              compile group: 'com.google.zxing', name: 'core', version: '3.2.1'
              // https://mvnrepository.com/artifact/com.google.zxing/android-core
              compile group: 'com.google.zxing', name: 'android-core', version: '3.2.0'
      
          }
      

      您始终可以从以下链接中找到最新版本的coreandroid core

      https://mvnrepository.com/artifact/com.google.zxing/core/3.2.1 https://mvnrepository.com/artifact/com.google.zxing/android-core/3.2.0

      【讨论】:

      • 截至 2017 年 7 月,这似乎是当前版本更相关的答案。
      【解决方案5】:

      在列侬解释的所有步骤之后,我通过转到 Android Studio 上的终端并键入 gradlew assemble 来解决“无法解析符号'IntentIntegrator'”的问题。花了一段时间,但现在我可以使用 aar 上声明的所有类。

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-09
        • 1970-01-01
        • 2013-07-15
        相关资源
        最近更新 更多