2

我在 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.");
    }

}

未找到 PDF 417 条形码的图像示例。 在此处输入图像描述

还有另一个配置参数来优化库吗?

4

0 回答 0