我在Java项目中使用ZXing 3.3.1库从图像文件中读取PDF 417(从纸上扫描并保存为jpg,jpeg)。
该库适用于某些文件,而且对于尽可能多的文件而言也很糟糕。
我使用的配置:
@SuppressWarnings({ "unchecked", "rawtypes" })
private BarCodeReader(){
BarCodeReader.HINTS = new HashMap();
BarCodeReader.HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
BarCodeReader.HINTS.put(DecodeHintType.TRY_HARDER, true);
List<BarcodeFormat> possibleFormats = new ArrayList<BarcodeFormat>();
possibleFormats.add(BarcodeFormat.PDF_417);
BarCodeReader.HINTS.put(DecodeHintType.POSSIBLE_FORMATS, possibleFormats);
}
我如何尝试阅读文件:
@SuppressWarnings({ "unchecked" })
public String readBarcode(String filePath) {
BarCodeResult result = null;
String errorMessage = "";
String readMessage = "";
FileInputStream resizedInput = null;
FileInputStream fis = null;
boolean error = false;
boolean resized = false;
try{
fis = new FileInputStream(filePath);
BufferedImage image = ImageIO.read(fis);
if (image.getHeight() > MAX_HEIGHT || image.getWidth() > MAX_WIDTH){
resizedInput = new FileInputStream(filePath);
image = resizeImage(ImageIO.read(resizedInput), MAX_WIDTH, MAX_HEIGHT);
resized = true;
}
BufferedImageLuminanceSource luminanceSource = new BufferedImageLuminanceSource(image);
HybridBinarizer binarizer = new HybridBinarizer(luminanceSource);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
Result decodeResult = new MultiFormatReader().decode(bitmap, HINTS);
readMessage = decodeResult.getText();
result = new BarCodeResult(error, readMessage, resized, errorMessage, filePath);
} catch (Throwable e){
error = true;
errorMessage = e.getClass().getName();
}
if(fis!=null){
closeInput(fis, resizedInput);
}
if(result != null){
return result.toJsonString();
} else {
return errorJsonString("Unable to get barcode. Null result.");
}
}
还有另一个配置参数来优化库吗?