安卓使用中兴生成二维码

2022-09-01 13:37:15

当我尝试在Android编程中生成QR码时遇到了一些问题。这是我遵循的教程。当我点击生成按钮时,我正在调用此方法:

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();
    }
}

然后对于QRCodeEncoder和Contents类,我遵循了教程中的那个。但是,当我单击“生成”时,我收到错误消息为:

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文件夹下添加了核心.jar。有什么想法吗?提前致谢。


答案 1

我已经更新了我在Github上的小型演示项目,并在2022年给出了以下答案:

screenshot

将 ZXing 库添加到您的 app/build.gradle 文件中:

implementation 'com.google.zxing:core:3.4.1'

然后在MainActivity中使用以下函数.java从字符串中生成QR码图像:

Bitmap encodeAsBitmap(String str) throws WriterException {
    QRCodeWriter writer = new QRCodeWriter();
    BitMatrix bitMatrix = writer.encode(str, BarcodeFormat.QR_CODE, 400, 400);

    int w = bitMatrix.getWidth();
    int h = bitMatrix.getHeight();
    int[] pixels = new int[w * h];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            pixels[y * w + x] = bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
    return bitmap;
}

答案 2
public static int white = 0xFFFFFFFF;
public static int black = 0xFF000000;
public final static int WIDTH = 500;


try
    {
        Bitmap bmp =  encodeAsBitmap("Life is a bitch");
        imgView.setImageBitmap(bmp);
    } catch (Exception e) {
        e.printStackTrace();
    }


Bitmap encodeAsBitmap(String str) throws WriterException {
    BitMatrix result;
    Bitmap bitmap=null;
    try
    {
        result = new MultiFormatWriter().encode(str,
                BarcodeFormat.QR_CODE, WIDTH, WIDTH, 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.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, WIDTH, 0, 0, w, h);
    } catch (Exception iae) {
        iae.printStackTrace();
        return null;
    }
    return bitmap;
}

推荐