ZXing FormatException解码ZXing生成的QRCode
ZXing FormatException decoding ZXing generated QRCode
以下代码源自 encoding-and-decoding-random-byte-array-with-zxing,使用 ZXing 对字节数组 (长度为 35,所有元素为 0) 进行编码,然后再次对其进行解码。
package zxing.sandpit;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
public class Problem {
public static void main(String[] args) throws UnsupportedEncodingException, WriterException, NotFoundException, ChecksumException, FormatException {
byte[] bytes = new byte[35];
String dataString = new String(bytes, "ISO-8859-1");
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(
dataString,
BarcodeFormat.QR_CODE, 256, 256);
System.out.println("A");
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
System.out.println("B");
final Result result = new QRCodeReader().decode(
new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(image.getWidth(), image.getHeight(),
image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0,
image.getWidth())))));
System.out.println("C");
byte[] bytes1 = result.getText().getBytes("ISO8859_1");
}
}
虽然对于长度小于 35 的所有数组都能完美工作,但对于大小为 35 的数组,会抛出 FormatException
并且永远不会打印 C
。
A
B
Exception in thread "main" com.google.zxing.FormatException
我犯了什么错误?
ZXing 在生成的二维码中检测到错误标记(误报)。尤其是在使用合成图像时会发生这种情况。尝试解码提示 PURE_BARCODE.
以下代码源自 encoding-and-decoding-random-byte-array-with-zxing,使用 ZXing 对字节数组 (长度为 35,所有元素为 0) 进行编码,然后再次对其进行解码。
package zxing.sandpit;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
public class Problem {
public static void main(String[] args) throws UnsupportedEncodingException, WriterException, NotFoundException, ChecksumException, FormatException {
byte[] bytes = new byte[35];
String dataString = new String(bytes, "ISO-8859-1");
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(
dataString,
BarcodeFormat.QR_CODE, 256, 256);
System.out.println("A");
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
System.out.println("B");
final Result result = new QRCodeReader().decode(
new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(image.getWidth(), image.getHeight(),
image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0,
image.getWidth())))));
System.out.println("C");
byte[] bytes1 = result.getText().getBytes("ISO8859_1");
}
}
虽然对于长度小于 35 的所有数组都能完美工作,但对于大小为 35 的数组,会抛出 FormatException
并且永远不会打印 C
。
A
B
Exception in thread "main" com.google.zxing.FormatException
我犯了什么错误?
ZXing 在生成的二维码中检测到错误标记(误报)。尤其是在使用合成图像时会发生这种情况。尝试解码提示 PURE_BARCODE.