0
我們使用zxing從圖像中解碼qrcode,大多數qrcode通常可以從原始圖像中提取,但有些則不能。我將顯示解碼器代碼,並且我們應該共同討論導致NotFoundException的原因並找出解決方案。com.google.zxing.NotFoundException同時使用zxing解碼qrcode
首先,需要一些斑馬線依賴性:
<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));
}
錯誤:com.google:
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)
所致。 zxing.NotFoundException
如下提供的模板QR碼:
感謝所有幫助不大。