在 Android Studio 中集成 ZXing [英] Integrate ZXing in Android Studio

查看:75
本文介绍了在 Android Studio 中集成 ZXing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将开始解释我所做的所有步骤,最后说明问题所在.

I'll start explaining all the steps I have done and in the end what is the problem.

  1. 下载 ZXing-2.2 https://code.google.com/p/zxing/下载/列表
  2. 在 zxing-2.2 中提取所有内容.
  3. 下载并安装 Apache Ant http://www.youtube.com/watch?v=XJmndRfb1TU
  4. 使用 Windows 命令行 (Run->CMD) 导航到解压目录
  5. 在命令行窗口中 - 输入ant -f core/build.xml"按回车键让 Apache 工作,这很神奇

此时就像将ZXing库直接集成到我的Android中申请

但是 Wooops,构建文件:core\build.xml 不存在!构建失败.好的.6. 导入ZXing - 缺少core/build.xml

But Wooops, "Buildfile: core\build.xml does not exists! Build failed. ok. 6. Importing ZXing - missing core/build.xml

现在是的,我有我的 core.jar.

Now yes, i have my core.jar.

  1. 打开Android Studio,文件->导入项目->在/zxing-2.2/中选择/android/->从现有源创建项目->项目名称:andoid->源文件...全部勾选下一步->库(不能什么都不做)下一个 -> 模块(android 已检查)下一个 -> SDK 1.7 下一个 -> 完成

打开项目 -> 构建 -> 重建项目

With Project Open -> Build -> Rebuild project

100 个错误19条警告

100 errors 19 warnings

文件 -> 项目结构 -> 库 -> 添加 -> Java -> 选择我之前创建的 core.jar 并确定 -> 库核心"将被添加到所选模块中.(android) OK -> 在 Project Structure 对话框中 OK.

File -> project Structure -> Libraries -> Add -> Java -> Select core.jar that i create before and OK -> Library 'core' will be added to the selected modules. (android) OK -> And OK in the Project Structure Dialog.

构建 -> 重建项目

15 个错误20条警告

15 errors 20 warnings

所有错误都是错误:需要常量表达式和我看到 Android 中 ZXing 项目的 Switch 案例错误我将所有开关更改为 if elses.

All errors are error: constant expression required and I see Error in Switch cases of ZXing project in android I change all switchs for if elses.

0 错误20条警告

好的,现在继续:

File -> New project -> zxing_demo Next -> Next -> Blank Activity Next -> Finish

File -> New project -> zxing_demo Next -> Next -> Blank Activity Next -> Finish

在新项目中 -> 文件 -> 导入模块 -> 搜索并选择/android/OK -> 从现有源创建模块 Next -> Next -> Next -> Next -> Finish

In new project -> File -> Import module -> Search and select /android/ OK -> Create module from existing sources Next -> Next -> Next -> Next -> Finish

现在我可以在资源管理器中看到/android//zging_demoProject/和外部库

Now I can see in the explorer /android/ /zging_demoProject/ and External Libraries

现在我改变我的代码你扫描二维码

Now i change my code tu scan QR

AndroidManifest.xml

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zxing_demo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" />
<uses-feature
    android:name="android.hardware.touchscreen"
    android:required="false" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.zxing_demo.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:clearTaskOnLaunch="true"
        android:stateNotNeeded="true"
        android:configChanges="orientation|keyboardHidden"
        android:name="com.google.zxing.client.android.CaptureActivity"
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter >
            <action android:name="com.google.zxing.client.android.SCAN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

MainActivity.java

MainActivity.java

package com.example.zxing_demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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
        }
    }
}

}

现在测试,运行 -> 调试

Now test, Run -> Debug

然后崩溃.

日志猫

08-31 02:58:28.085  20665-20665/com.example.zxing_demo E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.zxing_demo/com.google.zxing.client.android.CaptureActivity}: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
    at android.app.ActivityThread.access$600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4448)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
    ... 11 more

我可以在 AndroidManifest.xml 中看到这一行

I can see in AndroidManifest.xml in this line

android:name="com.google.zxing.client.android.CaptureActivity"

CaptureActivity"为红色,错误提示:无法解析符号CaptureActivity"

"CaptureActivity" in red and the error say: Cannot resolve symbol 'CaptureActivity'

文件 -> 项目结构 -> 模块 -> zxing_demo -> 依赖 -> 添加 -> 模块依赖 -> android OK -> Apply and OK

File -> Project Structure -> Modules -> zxing_demo -> Dependencies -> Add -> Module dependency -> android OK -> Apply and OK

现在 CaptureActivity 看起来不错

Now CaptureActivity looks good

再次调试

08-31 03:06:58.513  21740-21740/com.example.zxing_demo E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.zxing_demo/com.google.zxing.client.android.CaptureActivity}: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
    at android.app.ActivityThread.access$600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4448)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
    ... 11 more

我想我会使用应用程序和意图,但现在我想做这项工作,如果有人现在发生了什么,请告诉我.

I think I will use the application and intents, but now I want do this work, if someone now whats happen tell me please.

推荐答案

我正在将 ZXING 集成到一个 Android 应用程序中,但没有很好的输入来源,我会给你一个对我有用的提示 - 因为结果证明它很容易.

I was integrating ZXING into an Android application and there were no good sources for the input all over, I will give you a hint on what worked for me - because it turned out to be very easy.

有一个真正方便的 git 存储库,它提供 zxing android 库项目作为 AAR 存档.

There is a real handy git repository that provides the zxing android library project as an AAR archive.

你所要做的就是将它添加到你的 build.gradle

All you have to do is add this to your build.gradle

repositories {
    jcenter()
}

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

Gradle 为编译代码并使其在您的应用中可访问而发挥了所有作用.

and Gradle does all the magic to compile the code and makes it accessible in your app.

要在之后启动扫描仪,请使用此类/方法:来自活动:

To start the Scanner afterwards, use this class/method: From the Activity:

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

来自片段:

IntentIntegrator.forFragment(this).initiateScan(); // `this` is the current Fragment
// If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead.

有几个自定义选项:

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

他们有一个示例项目 并提供了几个集成示例:

They have a sample-project and are providing several integration examples:

如果您已经访问过链接,您会看到我只是从 git README 中复制并粘贴了代码.如果没有,请去那里获得更多见解和代码示例.

If you already visited the link you going to see that I just copy&pasted the code from the git README. If not, go there to get some more insight and code examples.

这篇关于在 Android Studio 中集成 ZXing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆