[英]Decoding QRCode with zxing. Java
I generated a QRCode using zxing library. 我使用zxing库生成了QRCode。
QRCode qrcode = QRCode.from("Encoding string").withSize(17,17).to(ImageType.PNG);
ByteArrayOutputStream out = QRCode.from(output.toString()).withSize(10, 10).to(ImageType.PNG).stream();
FileOutputStream fout = new FileOutputStream(new File("D:\\QR_Code.JPG"));
fout.write(out.toByteArray());
fout.flush();
fout.close();
It works fine but now I want to decode generated QRCode. 它工作正常,但现在我想解码生成的QRCode。 Is it possible to decode QRCode from an image with zxing? 是否可以使用zxing从图像解码QRCode? If so, can you give me a hint how to do it because I haven't found appropriate class or method. 如果是这样,您能给我一个提示,因为我没有找到合适的类或方法。 Thanks in advance. 提前致谢。
Here is what you can do: 您可以执行以下操作:
QRCodeReader
to decode qrcode data from a BinaryBitmap
你需要的实例QRCodeReader
到QRCode的数据从解码BinaryBitmap
HybridBinarizer
and pass it as a constructor argument to create your BinaryBitmap
您需要实例化HybridBinarizer
并将其作为构造函数参数传递,以创建BinaryBitmap
HybridBinarizer
needs an instance of LuminanceSource
HybridBinarizer
需要一个LuminanceSource
实例 BufferedImageLuminanceSource
看看BufferedImageLuminanceSource
Here is an example that would decode data from a buffered image: 这是一个示例,它将解码来自缓冲图像的数据:
public static String qrDecodeFromImage(BufferedImage img) {
if(img!=null) {
LuminanceSource bfImgLuminanceSource = new BufferedImageLuminanceSource(img);
BinaryBitmap binaryBmp = new BinaryBitmap(new HybridBinarizer(bfImgLuminanceSource));
QRCodeReader qrReader = new QRCodeReader();
Result result;
try {
result = qrReader.decode(binaryBmp);
return result.getText();
} catch (NotFoundException e) {} catch (ChecksumException e) {} catch (FormatException e) {}
}
return null;
}
You'll need to have the ZXing project included in your project(source, etc). 您需要将ZXing项目包含在您的项目中(源等)。
Then you can use all sorts of ZXing classes to performing decoding/encoding, etc: 然后,您可以使用各种ZXing类执行解码/编码等操作:
Look into these classes: BinaryBitmap
, QRCodeReader
, ParsedResult
, ResultParser
and give this a try: 查看以下类: BinaryBitmap
, QRCodeReader
, ParsedResult
, ResultParser
然后尝试一下:
Bitmap b = ...;//TODO: create a bitmap from your source...
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(b)));
Result result = null;
QRCodeReader reader = new QRCodeReader();
try {
result = reader.decode(bitmap);
ParsedResult parsedResult = ResultParser.parseResult(result);
//TODO: use parsedResult
}
catch(OutOfMemoryError e) {
}
catch(Exception e) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.