Reputation: 1440
I am attempting to get a successful result with the ZXing 2.1 library. I am using Java 1.6 on Mac OS X 10.7.5. I am able to encode text but not decode any images. Rather, all I get are one-line stack traces of com.google.zxing.NotFoundException
.
This seems straightforward but I cannot figure out what I am doing wrong. Here is a simple test to reproduce. It encodes a couple barcodes to images and then decodes the images from memory:
public class App {
public static void main(String[] args) {
// Try UPC-A.
try {
testEncodeDecode(BarcodeFormat.UPC_A, "012345678905"); // Valid UPC-A.
} catch (Exception e) {
e.printStackTrace();
}
// Try EAN-13.
try {
testEncodeDecode(BarcodeFormat.EAN_13, "9310779300005"); // Valid EAN-13.
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testEncodeDecode(BarcodeFormat barcodeFormat, String text)
throws WriterException, NotFoundException, ChecksumException, FormatException, IOException {
// Size of buffered image.
int width = 200;
int height = 100;
// Encode to buffered image.
Writer writer = new MultiFormatWriter();
BitMatrix bitMatrix = writer.encode(text, barcodeFormat, width, height);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
// Write to disk for debugging.
String formatName = "png";
File outputFile = new File(text + "." + formatName);
ImageIO.write(bufferedImage, formatName, outputFile);
// Decode from buffered image.
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
// Never gets this far!
System.out.println("result=" + result.getText());
}
}
Output will simply be
com.google.zxing.NotFoundException
com.google.zxing.NotFoundException
I'm stumped! Thanks for your help. Output images are attached for your reference.
UPC-A EAN-13
Upvotes: 3
Views: 3409
Reputation: 31
I was facing a similar problem in the beginning but passing hints solved it for me.
You can try passing TRY_HARDER
first. It should work. If it doesn't then try passing the POSSIBLE_FORMATS
hint as you already know the formats. Check and see if both the hints work.
Upvotes: 1
Reputation: 66866
From looking at this briefly, I think the problem is that there is not enough quiet zone on either side. The spec, IIRC, requires 9 modules on left and right, and this has about 2.
The detector is fairly lenient but not quite that much, to avoid false positives. Normally the area outside the image is treated like a big plane of white (and indeed, these scan just fine, on a white background like this page) so it would scan. For this format I see a note in the code that this is specifically disabled to avoid false positives.
You could try disabling this or generating a wider code to test that. If you find a change that doesn't increase false positives in the test set but makes this pass, that's probably worth committing.
Upvotes: 0