Android QR生成器

时间:2013-06-19 14:09:20

标签: java android qr-code zxing noclassdeffounderror

几周以来,我一直在尝试使用ZXING库来创建一个应用程序,该应用程序生成并显示包含文本的简单QR代码。我已经阅读了关于这个主题的所有其他问题,但无法使其发挥作用。

我认为我很接近,目前我已将core.jar作为我项目的参考包含在内,而且我还将zxing文件夹中的android库作为引用库。

这是我的代码:

String uniqueID="hi";
    BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
    int width0 = 500;
    int height0 = 500;
    QRCodeWriter writer = new QRCodeWriter();
    try
    {
        EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = writer.encode(uniqueID, barcodeFormat, width0, height0, hint);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++)
        {
            int offset = y * width;
            for (int x = 0; x < width; x++)
            {

                pixels[offset + x] = bitMatrix.get(x, y) ? android.graphics.Color.BLACK : android.graphics.Color.WHITE;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        ImageView imageview = (ImageView)findViewById(R.id.qrCode);
        imageview.setImageBitmap(bitmap);
    } catch (WriterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

此代码没有任何错误。当我运行应用程序并单击运行此代码的按钮时,我得到以下logcat输出:

06-19 15:33:34.493: W/dalvikvm(1340): VFY: unable to resolve static field 1076 (QR_CODE) in Lcom/google/zxing/BarcodeFormat;
06-19 15:33:34.493: D/dalvikvm(1340): VFY: replacing opcode 0x62 at 0x0002
06-19 15:33:34.493: D/dalvikvm(1340): DexOpt: unable to opt direct call 0x114f at 0x0a in Lcom/example/qrgen/MainActivity;.genQR
06-19 15:33:34.493: I/dalvikvm(1340): DexOpt: unable to optimize static field ref 0x043c at 0x14 in Lcom/example/qrgen/MainActivity;.genQR
06-19 15:33:34.963: D/libEGL(1340): loaded /system/lib/egl/libEGL_mali.so
06-19 15:33:34.993: D/libEGL(1340): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-19 15:33:34.998: D/libEGL(1340): loaded /system/lib/egl/libGLESv2_mali.so
06-19 15:33:35.003: D/(1340): Device driver API match
06-19 15:33:35.003: D/(1340): Device driver API version: 10
06-19 15:33:35.003: D/(1340): User space API version: 10 
06-19 15:33:35.003: D/(1340): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
06-19 15:33:35.073: D/OpenGLRenderer(1340): Enabling debug mode 0
06-19 15:33:38.883: D/AndroidRuntime(1340): Shutting down VM
06-19 15:33:38.883: W/dalvikvm(1340): threadid=1: thread exiting with uncaught exception (group=0x420312a0)
06-19 15:33:38.913: E/AndroidRuntime(1340): FATAL EXCEPTION: main
06-19 15:33:38.913: E/AndroidRuntime(1340): java.lang.IllegalStateException: Could not execute method of the activity
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.view.View$1.onClick(View.java:3691)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.view.View.performClick(View.java:4211)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.view.View$PerformClick.run(View.java:17267)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.os.Handler.handleCallback(Handler.java:615)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.os.Looper.loop(Looper.java:137)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.app.ActivityThread.main(ActivityThread.java:4898)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at dalvik.system.NativeStart.main(Native Method)
06-19 15:33:38.913: E/AndroidRuntime(1340): Caused by: java.lang.reflect.InvocationTargetException
06-19 15:33:38.913: E/AndroidRuntime(1340):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.view.View$1.onClick(View.java:3686)
06-19 15:33:38.913: E/AndroidRuntime(1340):     ... 11 more
06-19 15:33:38.913: E/AndroidRuntime(1340): Caused by: java.lang.NoClassDefFoundError: com.google.zxing.BarcodeFormat
06-19 15:33:38.913: E/AndroidRuntime(1340):     at com.example.qrgen.MainActivity.genQR(MainActivity.java:36)
06-19 15:33:38.913: E/AndroidRuntime(1340):     ... 14 more

问题似乎在于com.google.zxing.BarcodeFormat,但我检查了我引用的库,并且该类确实存在。关于什么可能出错的任何想法?

1 个答案:

答案 0 :(得分:2)

您最近是否更新了ADT版本?因为最新版本(ADT 22)导致依赖关系(例如你的ZXing)与NoClassDefFoundError崩溃......在这种情况下,这与你的代码完全没有关系。

最常见的解决方案是already on stackoverflow,检查它是否有效!