com.google.zxing.NotFoundException 使用zxing解码二维码
com.google.zxing.NotFoundException while using zxing to decode qrcode
我们正在使用zxing解码图片中的二维码,大部分二维码通常可以从原始图像中提取出来,但有些不能。我将展示解码器代码,我们是否可以一起讨论导致 NotFoundException 的原因并找出解决方案。
首先,需要一些zxing依赖:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
然后看详细代码:
public static String decodeQrCode(BufferedImage image) throws DependencyServiceException
{
// Convert the image to a binary bitmap source
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// Decode the barcode
QRCodeReader reader = new QRCodeReader();
Result result = null;
try
{
result = reader.decode(bitmap);
}
catch (NotFoundException | ChecksumException | FormatException e)
{
throw new DependencyServiceException(e);
}
return result == null ? null : result.getText();
}
public static void main(String[] args)
{
File file = new File("/tmp/ivan_qr_code.jpg");
String qrCodeOriginUrlExtracted = "";
try
{
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
String qrCodeBase64 = Base64.encodeBase64URLSafeString(imageData);
BufferedImage image = Base64StringToImageUtil.generateImageFromString(qrCodeBase64);
qrCodeOriginUrlExtracted = QrCodeDecoderUtil.decodeQrCode(image);
imageInFile.close();
}
catch (Exception e)
{
// TODO: handle exception
}
System.out.println(String.format("Extracted text from qr code: %1$s", qrCodeOriginUrlExtracted));
}
产生的错误:
Exception in thread "main" {"errorCode": "3000", "debugInfo":"null","message": "com.google.zxing.NotFoundException"}
at com.waijule.common.util.image.QrCodeDecoderUtil.decodeQrCode(QrCodeDecoderUtil.java:44)
at com.waijule.common.util.image.QrCodeDecoderUtil.main(QrCodeDecoderUtil.java:58)
原因:com.google.zxing.NotFoundException
模板二维码提供如下:
感谢所有的小帮助。
整体来说,decode方式要视具体情况而定,DecodeHintType和barcodereader需要提前指定。参考https://zxing.org/w/decode.jspx from zxing的开源,注意方法'processImage'.
感谢这几天的关注。
我们正在使用zxing解码图片中的二维码,大部分二维码通常可以从原始图像中提取出来,但有些不能。我将展示解码器代码,我们是否可以一起讨论导致 NotFoundException 的原因并找出解决方案。
首先,需要一些zxing依赖:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
然后看详细代码:
public static String decodeQrCode(BufferedImage image) throws DependencyServiceException
{
// Convert the image to a binary bitmap source
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// Decode the barcode
QRCodeReader reader = new QRCodeReader();
Result result = null;
try
{
result = reader.decode(bitmap);
}
catch (NotFoundException | ChecksumException | FormatException e)
{
throw new DependencyServiceException(e);
}
return result == null ? null : result.getText();
}
public static void main(String[] args)
{
File file = new File("/tmp/ivan_qr_code.jpg");
String qrCodeOriginUrlExtracted = "";
try
{
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
String qrCodeBase64 = Base64.encodeBase64URLSafeString(imageData);
BufferedImage image = Base64StringToImageUtil.generateImageFromString(qrCodeBase64);
qrCodeOriginUrlExtracted = QrCodeDecoderUtil.decodeQrCode(image);
imageInFile.close();
}
catch (Exception e)
{
// TODO: handle exception
}
System.out.println(String.format("Extracted text from qr code: %1$s", qrCodeOriginUrlExtracted));
}
产生的错误:
Exception in thread "main" {"errorCode": "3000", "debugInfo":"null","message": "com.google.zxing.NotFoundException"}
at com.waijule.common.util.image.QrCodeDecoderUtil.decodeQrCode(QrCodeDecoderUtil.java:44)
at com.waijule.common.util.image.QrCodeDecoderUtil.main(QrCodeDecoderUtil.java:58)
原因:com.google.zxing.NotFoundException
模板二维码提供如下:
感谢所有的小帮助。
整体来说,decode方式要视具体情况而定,DecodeHintType和barcodereader需要提前指定。参考https://zxing.org/w/decode.jspx from zxing的开源,注意方法'processImage'.
感谢这几天的关注。