Android ZXing QR扫描

时间:2015-05-18 17:40:12

标签: java android eclipse zxing

抱歉我的英文。我尝试多次包括libruary ZXing扫描qr代码。但不是成功的。我从this下载libruary然后我添加eclipse并包含core.jar并检查Android libruary。然后我在我的应用程序中添加libruary ZXing文件夹,但我有这个错误:

05-18 20:34:59.504: W/dalvikvm(19141): Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lcom/google/zxing/client/android/CaptureActivity;
05-18 20:34:59.504: W/dalvikvm(19141): Class init failed in newInstance call (Lcom/google/zxing/client/android/CaptureActivity;)
05-18 20:34:59.504: D/AndroidRuntime(19141): Shutting down VM
05-18 20:34:59.504: W/dalvikvm(19141): threadid=1: thread exiting with uncaught exception (group=0x40d4b258)
05-18 20:34:59.509: E/AndroidRuntime(19141): FATAL EXCEPTION: main
05-18 20:34:59.509: E/AndroidRuntime(19141): java.lang.ExceptionInInitializerError
05-18 20:34:59.509: E/AndroidRuntime(19141):    at java.lang.Class.newInstanceImpl(Native Method)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at java.lang.Class.newInstance(Class.java:1319)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1992)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at android.os.Looper.loop(Looper.java:154)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at android.app.ActivityThread.main(ActivityThread.java:4624)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at java.lang.reflect.Method.invokeNative(Native Method)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at java.lang.reflect.Method.invoke(Method.java:511)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-18 20:34:59.509: E/AndroidRuntime(19141):    at dalvik.system.NativeStart.main(Native Method)
05-18 20:34:59.509: E/AndroidRuntime(19141): Caused by: java.lang.NoClassDefFoundError: com.google.zxing.ResultMetadataType
05-18 20:34:59.509: E/AndroidRuntime(19141):    at com.google.zxing.client.android.CaptureActivity.<clinit>(CaptureActivity.java:93)
05-18 20:34:59.509: E/AndroidRuntime(19141):    ... 15 more

我的主要活动:

public class MainActivity extends Activity {

    static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Intent intent = new Intent(ACTION_SCAN);
        //intent.setPackage("com.example.bonsitemanager");
        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");

              Log.e(contents, contents);
              Log.e(format, format);

           } else if (resultCode == RESULT_CANCELED) {
              // Handle cancel
              Log.i("App","Scan unsuccessful");
           }
      }
   }
}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bonsiteqr"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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:name="com.google.zxing.client.android.CaptureActivity"
         android:configChanges="orientation|keyboardHidden"
         android:screenOrientation="landscape"
         android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
         android:windowSoftInputMode="stateAlwaysHidden" >
         <intent-filter>
             <action android:name="com.google.zxing.client.android.SCAN" />
             <category android:name="android.intent.category.DEFAULT" />
         </intent-filter>
     </activity>


    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

在eclipse中逐步设置zxing 3.2.1

  1. 从“https://github.com/zxing/zxing
  2. 下载zxing-master.zip
  3. 解压缩zxing-master.zip,使用eclipse导入zxing-master中的“android”项目
  4. 从“http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/
  5. 下载core-3.2.1.jar
  6. 在“android”项目中创建“libs”文件夹并将cor-3.2.1.jar粘贴到libs文件夹中
  7. 点击项目:选择“属性” - &gt; “Java编译器”将级别更改为1.7。然后点击“Android”将“Project build target”更改为android 4.4.2+,因为使用1.7需要使用Android 4.4进行编译
  8. 如果“zxing-master / android / app / src / main / java / com / google / zxing / client / android / camera /”中不存在“CameraConfigurationUtils.java”。你可以从“zxing-master / android-core / src / main / java / com / google / zxing / client / android / camera /”复制它并粘贴到你的项目中。
  9. 清理并构建项目。如果您的项目显示有关“switch - case”的错误,则应将其更改为“if - else”。
  10. 已完成。清理并建立项目。
  11. 参考链接:How to use Zxing in android