Reputation: 398
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.FormatException
What mistake am I making?
Upvotes: 0
Views: 1991
Reputation: 2506
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.
Upvotes: 1