在Android Studio中集成ZXing

Vic*_*any 88 android zxing android-studio

我将开始解释我所做的所有步骤,最后是什么问题.

  1. 下载ZXing-2.2 https://code.google.com/p/zxing/downloads/list
  2. 全部在zxing-2.2中提取.
  3. 下载并安装Apache Ant http://www.youtube.com/watch?v=XJmndRfb1TU
  4. 使用Windows Commandline(Run-> CMD)导航到解压缩的目录
  5. 在命令行窗口中 - 输入'ant -f core/build.xml'按回车键,让Apache发挥作用

此刻就像将ZXing库直接集成到我的Android应用程序中

但Wooops,"Buildfile:core\build.xml不存在!构建失败.确定.6.导入ZXing - 缺少core/build.xml

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

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

使用Project Open - > Build - > Rebuild项目

100个错误19个警告

文件 - >项目结构 - >库 - >添加 - > Java - >选择我之前创建的core.jar和OK - >库'core'将添加到所选模块中.(android)确定 - >在项目结构对话框中单击确定.

构建 - >重建项目

15个错误20个警告

所有错误都是错误:需要持续表达式,我在android中的ZXing项目的Switch案例中看到错误 我改变了所有开关的if elses.

0错误20警告

好的,现在继续:

文件 - >新项目 - > zxing_demo下一步 - >下一步 - >空白活动下一步 - >完成

在新项目 - >文件 - >导入模块 - >搜索并选择/ android/OK - >从现有源创建模块下一步 - >下一步 - >下一步 - >下一步 - >完成

现在我可以在explorer/android// zging_demoProject /和外部库中看到

现在我改变我的代码tu扫描QR

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>
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

现在测试,运行 - >调试

和CRASH.

logcat的

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
Run Code Online (Sandbox Code Playgroud)

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

android:name="com.google.zxing.client.android.CaptureActivity"
Run Code Online (Sandbox Code Playgroud)

红色的"CaptureActivity"和错误说:无法解析符号'CaptureActivity'

文件 - >项目结构 - >模块 - > zxing_demo - >依赖项 - >添加 - >模块依赖 - > android确定 - >应用并确定

现在CaptureActivity看起来不错

再次调试

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
Run Code Online (Sandbox Code Playgroud)

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

Lan*_*tel 221

我正在将ZXING整合到一个Android应用程序中,并没有很好的输入来源,我会给你一个关于什么对我有用的提示 - 因为它结果非常简单.

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

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

repositories {
    jcenter()
}

dependencies {
    implementation 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
    implementation 'com.google.zxing:core:3.2.0'
}
Run Code Online (Sandbox Code Playgroud)

和Gradle完成了编译代码并使其在您的应用程序中可访问的神奇功能.

要在之后启动扫描程序,请使用此类/方法:从活动:

new IntentIntegrator(this).initiateScan(); // `this` is the current Activity
Run Code Online (Sandbox Code Playgroud)

从片段:

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

有几种自定义选项:

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();
Run Code Online (Sandbox Code Playgroud)

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

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

  • 巨大的帮助.我花了一天时间试图从另一个项目中导入Zxing并且无处可去.然后我发现了这个链接和*BAM*它已经完成了.只需按照上面的代码示例链接:https://github.com/zxing/zxing/wiki/Scanning-Via-Intent. (3认同)
  • 要更改方向,请创建一个新的Activity,扩展CaptureActivity,并在AndroidManifest.xml中指定方向. (2认同)

小智 6

buttion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new com.google.zxing.integration.android.IntentIntegrator(Fragment.this).initiateScan();
            }
        });

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            if(result.getContents() == null) {
                Log.d("MainActivity", "Cancelled scan");
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
            } else {
                Log.d("MainActivity", "Scanned");
                Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            // This is important, otherwise the result will not be passed to the fragment
            super.onActivityResult(requestCode, resultCode, data);
        }
    }



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'
}
Run Code Online (Sandbox Code Playgroud)


Sme*_*eet 5

任何面临相同问题的人,请遵循以下简单步骤:

使用选项android从下载的zxing-masterzip文件中导入项目,Import project (Eclipse ADT, Gradle, etc.)并在应用级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'

    }
Run Code Online (Sandbox Code Playgroud)

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

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

更新(29.05.2019)

添加这些依赖项:

dependencies {
    implementation 'com.google.zxing:core:3.4.0'
    implementation 'com.google.zxing:android-core:3.3.0'
}
Run Code Online (Sandbox Code Playgroud)