Java 如何将ZXING导入android studio?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29513940/
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-11 08:09:08  来源:igfitidea点击:

How to import ZXING to android studio?

javaandroidimportandroid-studiozxing

提问by AM031447

I use android studio I want to import 'ZXING' in my application, I find many articles and found the following site

我使用 android studio 我想在我的应用程序中导入“ZXING”,我找到了很多文章并找到了以下站点

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

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

I downloaded the ZIP and unzip, and find some tutorials But it does not seem to be too detailed about the details, what I need to import? To achieve QRCode scan

我下载了ZIP,解压了,找了一些教程,但是好像不是很详细,需要导入什么?实现二维码扫描

I still have no idea how to do it

我仍然不知道该怎么做



4/14 I tried Lennon URL provided "zxing-android-minimal" And import the 'gradle-wrapper.jar'

4/14 我尝试了 Lennon URL 提供的“zxing-android-minimal”并导入了“gradle-wrapper.jar”

But when I wrote new IntentIntegrator (this) .initiateScan (); Still appear "Can not resolve symbol 'IntentIntegrator" message

但是当我写了 new IntentIntegrator(this) .initiateScan(); 仍然出现“无法解析符号'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

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

I do have a right '.jar select Add As Library But when an error occurs, he does not seem to be added

我确实有一个正确的'.jar select Add As Library 但是出现错误时,他似乎没有添加



4/10

4/10

Finally no longer appear "Can not resolve symbol 'IntentIntegrator" this is the code,What do I wrong?

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

I removed the new IntentIntegrator (this) .initiateScan (); 'applications normal operation

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

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

my 'build.greadle'

我的“build.gradle”

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

采纳答案by Lennon Spirlandelli

I had many troubles when I developed my app using zxing library. So take a look this zxing minimal: https://github.com/Promptus/zxing-android-minimal/tree/master

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

It worked perfectly to me and was easier to implement.

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

EDIT:

编辑:

Open up this file in your project:

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

/gradle/wrapper/gradle-wrapper.properties

/gradle/wrapper/gradle-wrapper.properties

Edit the distributionUrl line and set it too:

编辑 distributionUrl 行并设置它:

distributionUrl=http://services.gradle.org/distributions/gradle-1.8-all.zip Rebuild your project.

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

Update: You might want to use gradle-2.1-all.zip now.

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

NEW EDIT:

新编辑:

First of all, you must remove your libsfile. Then you have to remove

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

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

from your build.gradleof MyApplication, because that gradle is for the whole project, and it's better you use it in each module.

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

After that, open the build.gradleof the module appand add the following code:

之后,打开build.gradle模块的app并添加以下代码:

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'

}

And finally, you need to delete google.zxing.integration.androidfrom your project, otherwise, an error will show up when you compile.

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

UPDATE:

更新:

To resolve the back button problem, you can do the following code:

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

@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

    }

}

回答by krebernisak

You should define your zxing dependency in build.gradle file:

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

repositories {
    mavenCentral()
}

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

This is the Core barcode encoding/decoding library which you can use to build your custom barcode scanner/generator app.

这是核心条码编码/解码库,可用于构建自定义条码扫描器/生成器应用程序。

If you need to support just a simple case of scanning the barcode you can embed ZXing Android Barcode Scanner application using ZXing Android Embeddedproject.

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

This is a port of the ZXing Android Barcode Scannerapplication as an Android library project, for embedding in other Android applications.

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

If you decide to use the ZXing Android Embedded project it's as easy as defining your dependencies in build.gradle file:

如果您决定使用 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'
}

Launch the intent with the default options:

使用默认选项启动意图:

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

And get your result:

并得到你的结果:

@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;
    }
}

回答by Christopher

In your root-build.gradle:

在你的 root-build.gradle 中:

repositories {
   mavenCentral()

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

An in your app-build.gradle:

在你的 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'
}

More information could be found here: https://github.com/journeyapps/zxing-android-embedded

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

回答by Matías Maddonni

After all the steps explained by Lennon, i resolve the problem of "Can not resolve symbol 'IntentIntegrator'" by going to the terminal on Android Studio and typed gradlew assemble. It took for a while but now i can use all of the classes declared on the aar.

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

Hope it helped.

希望它有所帮助。

回答by Smeet

I was also encountered with same issue and I resolved it by following the simple steps as shown below:

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

Import the project androidfrom downloaded zxing-masterzip file using option Import project (Eclipse ADT, Gradle, etc.)and add the dollowing 2 lines of codes in your app level build.gradlefile and and you are ready to run.

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

So simple, yahh...

太简单了,呵呵...

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'

    }

You can always find latest version coreand android corefrom below links:

你总是可以找到最新版本core,并android core从下面的链接:

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

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