Android的使用ZXing生成QR code [英] Android Using ZXing Generate QR Code

查看:1027
本文介绍了Android的使用ZXing生成QR code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想产生的Andr​​oid编程QR code时有一些问题。这里是<一个href=\"http://www.mysample$c$c.com/2012/09/android-generate-qr-$c$c-using-zxing.html#comment-form_3020102673987308109\"相对=nofollow>教程我紧随其后。当我的生成单击按钮,我调用此方法:

I was having some problem when trying to generate QR code in Android Programming. Here is the Tutorial I followed. When my generate button on click, I am calling this method:

private void generateQR(){
    String qrInputText = "test";

    //Find screen size
    WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    Point point = new Point();
    display.getSize(point);
    int width = point.x;
    int height = point.y;
    int smallerDimension = width < height ? width : height;
    smallerDimension = smallerDimension * 3/4;

    //Encode with a QR Code image
    QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(
        qrInputText, 
        null, 
        Contents.Type.TEXT,  
        BarcodeFormat.QR_CODE.toString(), 
        smallerDimension
    );
    try {
        Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
        ImageView myImage = (ImageView) findViewById(R.id.ivImage);
        myImage.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}

和则对于QR $ C $岑codeR和内容类,我跟着一个教程。然而,当我点击生成,我收到错误信息为:

And then for the QRCodeEncoder and Contents classes, I followed the one in the tutorial. However, when I click on generate, I am getting the error message as:

01-30 16:37:03.093: I/dalvikvm(1069): Could not find method com.google.zxing.BarcodeFormat.valueOf, referenced from method com.example.qrcodescan.QRCodeEncoder.encodeContents
01-30 16:37:03.093: W/dalvikvm(1069): VFY: unable to resolve static method 338: Lcom/google/zxing/BarcodeFormat;.valueOf (Ljava/lang/String;)Lcom/google/zxing/BarcodeFormat;
01-30 16:37:03.093: D/dalvikvm(1069): VFY: replacing opcode 0x71 at 0x0005
01-30 16:37:03.093: W/dalvikvm(1069): VFY: unable to resolve static field 287 (QR_CODE) in Lcom/google/zxing/BarcodeFormat;
01-30 16:37:03.093: D/dalvikvm(1069): VFY: replacing opcode 0x62 at 0x0011
01-30 16:37:03.093: W/dalvikvm(1069): VFY: unable to resolve static field 287 (QR_CODE) in Lcom/google/zxing/BarcodeFormat;
01-30 16:37:03.093: D/dalvikvm(1069): VFY: replacing opcode 0x62 at 0x0015
01-30 16:37:03.093: W/dalvikvm(1069): VFY: unable to resolve static field 294 (CHARACTER_SET) in Lcom/google/zxing/EncodeHintType;
01-30 16:37:03.093: D/dalvikvm(1069): VFY: replacing opcode 0x62 at 0x0018
01-30 16:37:03.093: E/dalvikvm(1069): Could not find class 'com.google.zxing.MultiFormatWriter', referenced from method com.example.qrcodescan.QRCodeEncoder.encodeAsBitmap
01-30 16:37:03.093: W/dalvikvm(1069): VFY: unable to resolve new-instance 150 (Lcom/google/zxing/MultiFormatWriter;) in Lcom/example/qrcodescan/QRCodeEncoder;
01-30 16:37:03.093: D/dalvikvm(1069): VFY: replacing opcode 0x22 at 0x001d
01-30 16:37:03.093: D/dalvikvm(1069): DexOpt: unable to opt direct call 0x0159 at 0x1f in Lcom/example/qrcodescan/QRCodeEncoder;.encodeAsBitmap
01-30 16:37:03.093: D/AndroidRuntime(1069): Shutting down VM
01-30 16:37:03.093: W/dalvikvm(1069): threadid=1: thread exiting with uncaught exception (group=0x40c341f8)
01-30 16:37:03.101: E/AndroidRuntime(1069): FATAL EXCEPTION: main
01-30 16:37:03.101: E/AndroidRuntime(1069): java.lang.NoClassDefFoundError: com.google.zxing.BarcodeFormat
01-30 16:37:03.101: E/AndroidRuntime(1069):     at com.example.qrcodescan.MainActivity.generateQR(MainActivity.java:95)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at com.example.qrcodescan.MainActivity.access$0(MainActivity.java:77)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at com.example.qrcodescan.MainActivity$2.onClick(MainActivity.java:54)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at android.view.View.performClick(View.java:3620)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at android.view.View$PerformClick.run(View.java:14292)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at android.os.Handler.handleCallback(Handler.java:605)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at android.os.Looper.loop(Looper.java:137)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at android.app.ActivityThread.main(ActivityThread.java:4512)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at java.lang.reflect.Method.invoke(Method.java:511)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
01-30 16:37:03.101: E/AndroidRuntime(1069):     at dalvik.system.NativeStart.main(Native Method)

我也加libs文件夹下的core.jar添加。有任何想法吗?先谢谢了。

I did added the core.jar under libs folder. Any ideas? Thanks in advance.

推荐答案

下面是如何从一个字符串生成QR code图像 -

Here is how you can generate an image with QR code from a string -

首先,添加以下行的的build.gradle 的文件在 Android的工作室项目:

First add the following line to build.gradle file in your Android Studio project:

dependencies {
    ....
    compile 'com.google.zxing:core:3.2.1'
}

或者,如果仍然使用的与ADT-Eclipse的插件添加ZXing的的 core.jar添加的距离的 Maven仓库到的的您的Eclipse ADT项目(这里的全屏):

Or if still using Eclipse with ADT-plugin add ZXing's core.jar from Maven repository to the libs subdir of your Eclipse ADT project (here fullscreen):

然后用从我的<一个样本code href=\"https://github.com/afarber/android-newbie/blob/master/QREn$c$cr/src/de/afarber/qren$c$cr/MainActivity.java\"相对=nofollow> MainActivity.java :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView = (ImageView) findViewById(R.id.qrCode);
    try {
        Bitmap bitmap = encodeAsBitmap(STR);
        imageView.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}

Bitmap encodeAsBitmap(String str) throws WriterException {
    BitMatrix result;
    try {
        result = new MultiFormatWriter().encode(str, 
            BarcodeFormat.QR_CODE, WIDTH, WIDTH, null);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    int w = result.getWidth();
    int h = result.getHeight();
    int[] pixels = new int[w * h];
    for (int y = 0; y < h; y++) {
        int offset = y * w;
        for (int x = 0; x < w; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, w, h);
    return bitmap;
}

这篇关于Android的使用ZXing生成QR code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆