我正在尝试使用ZXing 2.1 库获得成功的结果。我在 Mac OS X 10.7.5 上使用 Java 1.6。我能够对文本进行编码,但不能对任何图像进行解码。相反,我得到的只是com.google.zxing.NotFoundException
.
这看起来很简单,但我无法弄清楚我做错了什么。这是一个简单的重现测试。它将几个条形码编码为图像,然后从内存中解码图像:
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());
}
}
输出将只是
com.google.zxing.NotFoundException
com.google.zxing.NotFoundException
我难住了!谢谢你的帮助。附上输出图像供您参考。
UPC-A EAN-13