下面的代码使用ZXing编码一个字节数组(长度为35,所有元素为0) ,然后再次对其进行解码,编码- 解码 - 随机 - 字节 - 数组 - 带有zxing 。
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我犯了什么错误?
The following code, derived from encoding-and-decoding-random-byte-array-with-zxing, encodes a byte array (of length 35 and all elements 0) using ZXing and then decodes it again.
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"); } }While working perfectly for all arrays of length less than 35, for array size 35, a FormatException is thrown and C is never printed.
A B Exception in thread "main" com.google.zxing.FormatExceptionWhat mistake am I making?
最满意答案
ZXing在生成的qr代码中检测到错误的标记(误报)。 特别是在使用合成图像的情况下。 尝试解码提示PURE_BARCODE。
ZXing detects a wrong marker (false positive) within the generated qr code. That happens especially if synthetic images are used. Try the decoding hint PURE_BARCODE.
更多推荐
发布评论