使用zxing进行QR码编码和解码 [英] QR Code encoding and decoding using zxing

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

问题描述

好的,所以我要抓住这里有人曾经使用过zxing的机会。我正在开发一个Java应用程序,它需要做的一件事就是将一个字节数组数据编码为QR代码,然后再对其进行解码。

Okay, so I'm going to take the off chance that someone here has used zxing before. I'm developing a Java application, and one of the things it needs to do is encode a byte array of data into a QR Code and then decode it at a later time.

以下是我的编码器的示例:

Here's an example of what my encoder looks like:

byte[] b = {0x48, 0x45, 0x4C, 0x4C, 0x4F};
//convert the byte array into a UTF-8 string
String data;
try {
    data = new String(b, "UTF8");
}
catch (UnsupportedEncodingException e) {
 //the program shouldn't be able to get here
 return;
}

//get a byte matrix for the data
ByteMatrix matrix;
com.google.zxing.Writer writer = new QRCodeWriter();
try {
 matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
}
catch (com.google.zxing.WriterException e) {
 //exit the method
 return;
}

//generate an image from the byte matrix
int width = matrix.getWidth(); 
int height = matrix.getHeight(); 

byte[][] array = matrix.getArray();

//create buffered image to draw to
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

//iterate through the matrix and draw the pixels to the image
for (int y = 0; y < height; y++) { 
 for (int x = 0; x < width; x++) { 
  int grayValue = array[y][x] & 0xff; 
  image.setRGB(x, y, (grayValue == 0 ? 0 : 0xFFFFFF));
 }
}

//write the image to the output stream
ImageIO.write(image, "png", outputStream);

此代码中的起始字节数组仅用于测试它。实际的字节数据会有所不同。

The beginning byte array in this code is just used to test it. The actual byte data will be varied.

这是我的解码器的样子:

Here's what my decoder looks like:

//get the data from the input stream
BufferedImage image = ImageIO.read(inputStream);

//convert the image to a binary bitmap source
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

//decode the barcode
QRCodeReader reader = new QRCodeReader();

Result result;
try {
 result = reader.decode(bitmap, hints);
} catch (ReaderException e) {
 //the data is improperly formatted
 throw new MCCDatabaseMismatchException();
}

byte[] b = result.getRawBytes();
System.out.println(ByteHelper.convertUnsignedBytesToHexString(result.getText().getBytes("UTF8")));
System.out.println(ByteHelper.convertUnsignedBytesToHexString(b));

convertUnsignedBytesToHexString(byte)是一种方法转换十六进制字符串中的字节数组。

convertUnsignedBytesToHexString(byte) is a method which converts an array of bytes in a string of hexadecimal characters.

当我尝试将这两个代码块一起运行时,这是输出:

When I try to run these two blocks of code together, this is the output:

48454c4c4f
202b0b78cc00ec11ec11ec11ec11ec11ec11ec

显然文本正在编码,但实际的数据字节完全关闭。任何帮助都将在这里得到赞赏。

Clearly the text is being encoded, but the actual bytes of data are completely off. Any help would be appreciated here.

推荐答案

因此,对于任何不想花两天时间搜索互联网解决这个问题,当您将字节数组编码为QR码时,您必须使用 ISO-8859-1 字符集,而不是 UTF- 8

So, for future reference for anybody who doesn't want to spend two days searching the internet to figure this out, when you encode byte arrays into QR Codes, you have to use the ISO-8859-1character set, not UTF-8.

这篇关于使用zxing进行QR码编码和解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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