我已经开始构建一个使用ZXing库读取条形码的Android应用程序。我正在开发的应用程序使用独立的条形码扫描仪 - 它不使用Android Intent对象来扫描条形码。我在项目中包含了“core.jar”文件,并创建了一个扫描位图的java类。要做到这一点,我必须使用包含另一个名为RGBLuminanceSource的类 - 我在java2s.com网站上找到了。这个类可以读取android位图并用于帮助创建LuminanceSource对象,因为android不能使用AWT“BufferedImage”类来创建“LuminaceSource”对象。我的项目编译和构建没有任何错误,但当我尝试在模拟器(avd android版本1.5)上运行项目时,我得到了可怕的“应用程序”x“已经意外停止工作。请再试一次”消息!我查看了DDMS日志,我唯一能理解的是模拟器找不到我添加到项目中的“RGBLuminanceSource”类。这会导致问题,因为为了解码位图,我必须从RGBLuminaceSource类中创建一个对象。
我不知道为什么会发生这种情况,特别是当项目构建没有任何问题时,我对Android编程很新,所以任何帮助都会非常感谢,谢谢。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.me.testgui;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.common.HybridBinarizer;
import java.io.FileInputStream;
import java.io.InputStream;
/**
*
* @author Gerrard
*/
public class TestClass
{
protected static String scannedBarcode=null;
private static InputStream is;
protected static String testMethod()
{
String st = "THIS IS A TEST!!!!!!!!!!!";
return st;
}
public static void readBarcode(String file) throws NotFoundException
{
try
{
//file = "D:\\NetBeansProjects\\TestGUI\\res\\drawable\\barcode1.jpg";
is = new FileInputStream(file);
// Create a BitMap object of the image to be worked with
Bitmap bm = BitmapFactory.decodeStream(is);//create a bitmap from the imput stream "is" object
RGBLuminanceSource rgbSource = new RGBLuminanceSource(bm);//create an object type RGBLuminanceSource so that the android type bitmap can be read by the ZXing LumincaceSource class
System.out.println("RGBLuminance object created");
LuminanceSource source = rgbSource;
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
com.google.zxing.Result result = reader.decode(bitmap);
String barcode = String.format("Barcode text is: " + result.getText());
scannedBarcode = barcode;
}
catch(Exception e)
{
//Toast.makeText(MainActivity,"ERROR: " +e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
我还包含了DDMS日志以帮助追踪问题的根源
10-12 20:27:07.126: DEBUG/AndroidRuntime(833): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
10-12 20:27:07.149: DEBUG/AndroidRuntime(833): CheckJNI is ON
10-12 20:27:08.966: DEBUG/AndroidRuntime(833): --- registering native functions ---
10-12 20:27:09.038: INFO/jdwp(833): received file descriptor 22 from ADB
10-12 20:27:10.086: INFO/jdwp(833): Ignoring second debugger -- accepting and dropping
10-12 20:27:12.115: DEBUG/AndroidRuntime(833): Shutting down VM
10-12 20:27:12.135: DEBUG/dalvikvm(833): DestroyJavaVM waiting for non-daemon threads to exit
10-12 20:27:12.175: DEBUG/dalvikvm(833): DestroyJavaVM shutting VM down
10-12 20:27:12.185: DEBUG/dalvikvm(833): HeapWorker thread shutting down
10-12 20:27:12.195: DEBUG/dalvikvm(833): HeapWorker thread has shut down
10-12 20:27:12.206: DEBUG/jdwp(833): JDWP shutting down net...
10-12 20:27:12.229: DEBUG/jdwp(833): +++ peer disconnected
10-12 20:27:12.235: INFO/dalvikvm(833): Debugger has detached; object registry had 1 entries
10-12 20:27:12.376: DEBUG/dalvikvm(833): VM cleaning up
10-12 20:27:12.475: DEBUG/dalvikvm(833): LinearAlloc 0x0 used 629804 of 4194304 (15%)
10-12 20:27:13.345: DEBUG/AndroidRuntime(843): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
10-12 20:27:13.356: DEBUG/AndroidRuntime(843): CheckJNI is ON
10-12 20:27:13.902: DEBUG/AndroidRuntime(843): --- registering native functions ---
10-12 20:27:13.955: INFO/jdwp(843): received file descriptor 22 from ADB
10-12 20:27:13.955: INFO/jdwp(843): Ignoring second debugger -- accepting and dropping
10-12 20:27:16.821: DEBUG/PackageParser(554): Scanning package: /data/app/vmdl33973.tmp
10-12 20:27:17.546: INFO/PackageManager(554): Removing non-system package:org.me.testgui
10-12 20:27:17.595: DEBUG/PackageManager(554): Removing package org.me.testgui
10-12 20:27:17.625: DEBUG/PackageManager(554): Activities: org.me.testgui.MainActivity
10-12 20:27:18.067: DEBUG/PackageManager(554): Scanning package org.me.testgui
10-12 20:27:18.107: INFO/PackageManager(554): /data/app/vmdl33973.tmp changed; unpacking
10-12 20:27:18.185: DEBUG/installd(559): DexInv: --- BEGIN '/data/app/vmdl33973.tmp' ---
10-12 20:27:19.125: DEBUG/dalvikvm(849): DexOpt: load 155ms, verify 235ms, opt 2ms
10-12 20:27:19.195: DEBUG/installd(559): DexInv: --- END '/data/app/vmdl33973.tmp' (success) ---
10-12 20:27:19.205: DEBUG/PackageManager(554): Activities: org.me.testgui.MainActivity
10-12 20:27:19.375: INFO/installd(559): move /data/dalvik-cache/data@app@vmdl33973.tmp@classes.dex -> /data/dalvik-cache/data@app@org.me.testgui.apk@classes.dex
10-12 20:27:19.417: DEBUG/PackageManager(554): New package installed in /data/app/org.me.testgui.apk
10-12 20:27:19.705: DEBUG/AndroidRuntime(843): Shutting down VM
10-12 20:27:19.715: DEBUG/dalvikvm(843): DestroyJavaVM waiting for non-daemon threads to exit
10-12 20:27:19.785: DEBUG/ActivityManager(554): Uninstalling process org.me.testgui
10-12 20:27:19.805: DEBUG/dalvikvm(843): DestroyJavaVM shutting VM down
10-12 20:27:19.805: DEBUG/dalvikvm(843): HeapWorker thread shutting down
10-12 20:27:19.815: DEBUG/dalvikvm(843): HeapWorker thread has shut down
10-12 20:27:19.815: DEBUG/jdwp(843): JDWP shutting down net...
10-12 20:27:19.815: DEBUG/jdwp(843): +++ peer disconnected
10-12 20:27:19.815: INFO/dalvikvm(843): Debugger has detached; object registry had 1 entries
10-12 20:27:19.825: DEBUG/dalvikvm(843): VM cleaning up
10-12 20:27:19.935: DEBUG/dalvikvm(843): LinearAlloc 0x0 used 629572 of 4194304 (15%)
10-12 20:27:20.497: WARN/ResourceType(554): No package identifier when getting value for resource number 0x7f060000
10-12 20:27:20.538: WARN/ResourceType(554): No package identifier when getting value for resource number 0x7f060001
10-12 20:27:20.585: DEBUG/HomeLoaders(620): application intent received: android.intent.action.PACKAGE_REMOVED, replacing=true
10-12 20:27:20.615: DEBUG/HomeLoaders(620): --> package:org.me.testgui
10-12 20:27:20.635: DEBUG/HomeLoaders(620): application intent received: android.intent.action.PACKAGE_ADDED, replacing=true
10-12 20:27:20.655: DEBUG/HomeLoaders(620): --> package:org.me.testgui
10-12 20:27:20.665: DEBUG/HomeLoaders(620): --> update package org.me.testgui
10-12 20:27:20.876: DEBUG/dalvikvm(554): GC freed 9144 objects / 423280 bytes in 323ms
10-12 20:27:21.315: WARN/ResourceType(554): No package identifier when getting value for resource number 0x7f060000
10-12 20:27:21.366: WARN/ResourceType(554): No package identifier when getting value for resource number 0x7f060001
10-12 20:27:22.840: DEBUG/AndroidRuntime(854): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
10-12 20:27:22.845: DEBUG/AndroidRuntime(854): CheckJNI is ON
10-12 20:27:23.786: DEBUG/AndroidRuntime(854): --- registering native functions ---
10-12 20:27:23.795: INFO/jdwp(854): received file descriptor 22 from ADB
10-12 20:27:23.829: INFO/jdwp(854): Ignoring second debugger -- accepting and dropping
10-12 20:27:25.965: INFO/ActivityManager(554): Starting activity: Intent { flags=0x10000000 comp={org.me.testgui/org.me.testgui.MainActivity} }
10-12 20:27:26.876: DEBUG/AndroidRuntime(854): Shutting down VM
10-12 20:27:26.886: DEBUG/dalvikvm(854): DestroyJavaVM waiting for non-daemon threads to exit
10-12 20:27:26.936: DEBUG/dalvikvm(854): DestroyJavaVM shutting VM down
10-12 20:27:26.947: DEBUG/dalvikvm(854): HeapWorker thread shutting down
10-12 20:27:26.947: DEBUG/dalvikvm(854): HeapWorker thread has shut down
10-12 20:27:26.947: DEBUG/jdwp(854): JDWP shutting down net...
10-12 20:27:26.976: DEBUG/jdwp(854): +++ peer disconnected
10-12 20:27:26.976: INFO/dalvikvm(854): Debugger has detached; object registry had 1 entries
10-12 20:27:26.986: DEBUG/dalvikvm(854): VM cleaning up
10-12 20:27:27.046: DEBUG/dalvikvm(854): LinearAlloc 0x0 used 639228 of 4194304 (15%)
10-12 20:27:27.286: INFO/ActivityManager(554): Start proc org.me.testgui for activity org.me.testgui/.MainActivity: pid=862 uid=10024 gids={1006}
10-12 20:27:27.809: INFO/jdwp(862): received file descriptor 10 from ADB
10-12 20:27:28.055: INFO/jdwp(862): Ignoring second debugger -- accepting and dropping
10-12 20:27:28.105: WARN/System.err(862): Can't dispatch DDM chunk 46454154: no handler defined
10-12 20:27:28.146: WARN/System.err(862): Can't dispatch DDM chunk 4d505251: no handler defined
10-12 20:27:29.375: WARN/Resources(862): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f050001}
10-12 20:27:29.915: WARN/dalvikvm(862): Unable to resolve superclass of Lorg/me/testgui/RGBLuminanceSource; (30)
10-12 20:27:29.925: WARN/dalvikvm(862): Link of class 'Lorg/me/testgui/RGBLuminanceSource;' failed
10-12 20:27:29.925: ERROR/dalvikvm(862): Could not find class 'org.me.testgui.RGBLuminanceSource', referenced from method org.me.testgui.TestClass.readBarcode
10-12 20:27:29.936: WARN/dalvikvm(862): VFY: unable to resolve new-instance 68 (Lorg/me/testgui/RGBLuminanceSource;) in Lorg/me/testgui/TestClass;
10-12 20:27:29.936: WARN/dalvikvm(862): VFY: rejecting opcode 0x22 at 0x000d
10-12 20:27:29.936: WARN/dalvikvm(862): VFY: rejected Lorg/me/testgui/TestClass;.readBarcode (Ljava/lang/String;)V
10-12 20:27:29.955: WARN/dalvikvm(862): Verifier rejected class Lorg/me/testgui/TestClass;
10-12 20:27:29.975: DEBUG/AndroidRuntime(862): Shutting down VM
10-12 20:27:29.985: WARN/dalvikvm(862): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
10-12 20:27:29.985: ERROR/AndroidRuntime(862): Uncaught handler: thread main exiting due to uncaught exception
10-12 20:27:30.075: ERROR/AndroidRuntime(862): java.lang.VerifyError: org.me.testgui.TestClass
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at org.me.testgui.MainActivity.onCreate(MainActivity.java:182)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at android.os.Looper.loop(Looper.java:123)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at android.app.ActivityThread.main(ActivityThread.java:3948)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at java.lang.reflect.Method.invoke(Method.java:521)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
10-12 20:27:30.075: ERROR/AndroidRuntime(862): at dalvik.system.NativeStart.main(Native Method)
10-12 20:27:30.105: INFO/Process(554): Sending signal. PID: 862 SIG: 3
10-12 20:27:30.105: INFO/dalvikvm(862): threadid=7: reacting to signal 3
10-12 20:27:30.356: INFO/dalvikvm(862): Wrote stack trace to '/data/anr/traces.txt
答案 0 :(得分:2)
回答这个问题以及提出的其他问题:
RGBLuminanceSource
位于zxing
androidtest/
VerifyError
表示它不喜欢字节码。有时,使用Proguard会导致这种情况。如果您想这样做,请使用Android内置的Proguard优化功能。但这可能不是这种情况。core.jar
放到你的项目中。通常你只需将它保留在libs/
中即可。答案 1 :(得分:0)
VerifyError意味着类文件包含的字节码 语法正确,但违反了一些语义限制,例如一个 跳过跨越方法边界的目标。
java.lang.VerifyError可以是编译时的结果 与您在运行时使用的库不同。
修改强>
检查以获取更多信息:
Causes of getting a java.lang.VerifyError
<强> EDIT2:强>
好的,根据你的说法,我猜你没有正确设置RGBLuminanceSource
文件包。由于您将此文件复制到项目中,因此它应该在您的包中。
所以改变这个:
package com.google.zxing.client.androidtest;
对此:
package org.me.correct.package;