【问题标题】:Setting up zxing 2.0 barcode scanner in android app在 Android 应用程序中设置 zxing 2.0 条码扫描器
【发布时间】:2012-03-03 14:25:11
【问题描述】:

我正在尝试为 Android 应用设置条码扫描器。我能够使用 phonegap 提供的插件设置条形码扫描仪。这在某些手机上运行良好,但不适用于某些 HTC 手机。我在网上搜索,有人建议升级到zxing 2.0应该可以解决问题。因此,我下载了 zxing-2.0 并按照以下步骤操作: 1.解压zxing-2.0,构建核心包。 2. 将android包添加到eclipse中作为一个Android项目并选择它作为库(命名为zxing-2)。 3. 将 core.jar 作为外部 jar 添加到 zxing-2 中,使其在 eclipse 上成功构建。 4.添加zxing-2作为我的主应用程序(myApp)的项目依赖项。还在 myApp 中添加 core.jar 作为外部 jar。

MyApp 的 mainfest.xml 现在包含(之前它包含的操作名称为 com.phonegap.plugins.barcodescanner.SCAN):

<activity android:name="com.google.zxing.client.android.CaptureActivity"
              android:screenOrientation="landscape"
              android:configChanges="orientation|keyboardHidden"
              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>

    <activity android:name="com.google.zxing.client.android.encode.EncodeActivity" 
              android:label="@string/share_name">
        <intent-filter>
            <action android:name="com.google.zxing.client.android.ENCODE"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

我正在使用电话间隙插件中提供的 BarcodeScanner.java 来调用条形码扫描仪。我将其中的 scan() 方法更改为:

//new version
public void scan() {
    Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
    intentScan.addCategory(Intent.CATEGORY_DEFAULT);

    this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
}

//earlier version(it worked with zxing 1.7)
public void scan() {
    Intent intentScan = new Intent("com.phonegap.plugins.barcodescanner.SCAN");
    intentScan.addCategory(Intent.CATEGORY_DEFAULT);

    this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
}

项目构建良好,但是当我运行它并触发扫描时,我得到以下运行时异常:

03-03 19:43:13.499: E/AndroidRuntime(276): FATAL EXCEPTION: main
03-03 19:43:13.499: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phonegap.myApp/com.google.zxing.client.android.CaptureActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class phonegap
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.os.Looper.loop(Looper.java:123)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-03 19:43:13.499: E/AndroidRuntime(276):  at java.lang.reflect.Method.invokeNative(Native Method)
03-03 19:43:13.499: E/AndroidRuntime(276):  at java.lang.reflect.Method.invoke(Method.java:521)
03-03 19:43:13.499: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-03 19:43:13.499: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-03 19:43:13.499: E/AndroidRuntime(276):  at dalvik.system.NativeStart.main(Native Method)
03-03 19:43:13.499: E/AndroidRuntime(276): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class phonegap
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.GenericInflater.inflate(GenericInflater.java:317)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.GenericInflater.inflate(GenericInflater.java:263)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:422)
03-03 19:43:13.499: E/AndroidRuntime(276):  at com.google.zxing.client.android.CaptureActivity.onCreate(CaptureActivity.java:167)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-03 19:43:13.499: E/AndroidRuntime(276):  ... 11 more
03-03 19:43:13.499: E/AndroidRuntime(276): Caused by: java.lang.ClassNotFoundException: android.preference.phonegap in loader dalvik.system.PathClassLoader[/data/app/com.phonegap.myApp-1.apk]
03-03 19:43:13.499: E/AndroidRuntime(276):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
03-03 19:43:13.499: E/AndroidRuntime(276):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
03-03 19:43:13.499: E/AndroidRuntime(276):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.GenericInflater.createItem(GenericInflater.java:375)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
03-03 19:43:13.499: E/AndroidRuntime(276):  at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
03-03 19:43:13.499: E/AndroidRuntime(276):  ... 19 more

谁能帮我弄清楚我在这里做错了什么或者我错过了什么。

谢谢。

【问题讨论】:

  • 嗨,Nitesh,我可以得到你的 ZXing-2.0 的 core.jar 吗?很难为我构建,因为我不知道为什么,它总是在我的 cmd 中失败,我使用 EClipse 和 window7 顺便说一句,每当我到达例如。 C:/usr/thetmonaye/android-sdk-15> cd core,然后总是说,文件无法获取,所以我需要ZXing 2.0的内核,请给我thetmon4@gmail.com? :) 有美好的一天

标签: android cordova zxing barcode-scanner phonegap-plugins


【解决方案1】:

您的主要问题是您试图通过 Intent 进行集成,但也包括core/android/。你不应该像这样使用android/,只有在构建你自己的内部扫描时才使用core/。完全删除这些。

您想要从项目中获得的只是android-integration/,而您只需在这里:http://code.google.com/p/zxing/wiki/ScanningViaIntent

我不太了解 Phonegap,但不值得在 Android 上为此使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2013-05-25
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    相关资源
    最近更新 更多