Class ImagePdfUtils

java.lang.Object
com.extractpdf4j.helpers.ImagePdfUtils

public final class ImagePdfUtils extends Object
ImagePdfUtils

Utility helpers for working with PDF pages as images and converting between AWT BufferedImage and OpenCV Mat types. These functions are used by lattice/OCR flows where pages are rasterized and processed with OpenCV.

Design

  • Rendering is delegated to PDFBox's PDFRenderer.
  • Conversion to Mat is implemented via a temporary PNG round‑trip for broad compatibility with image types; this keeps logic simple and avoids assumptions about pixel formats.
  • Binarization uses OpenCV adaptive thresholding tuned for line detection (inverted binary for morphological line ops).

This class is non‑instantiable.

Since:
2025
Author:
Mehuli Mukherjee
  • Method Details

    • renderPage

      public static BufferedImage renderPage(org.apache.pdfbox.pdmodel.PDDocument doc, int pageIndexZero, float dpi) throws IOException
      Renders a single PDF page to a BufferedImage at the requested DPI.

      The output is rendered as ImageType.BINARY, which is generally suitable for downstream line/edge detection. If grayscale or RGB is needed, change the ImageType accordingly in the implementation.

      Parameters:
      doc - an open PDDocument
      pageIndexZero - zero‑based page index (0 = first page)
      dpi - dots per inch to render at (e.g., 300f for scans)
      Returns:
      a binary BufferedImage of the page
      Throws:
      IOException - if rendering fails
    • bufferedToMat

      public static org.bytedeco.opencv.opencv_core.Mat bufferedToMat(BufferedImage img)
      Converts a BufferedImage to an OpenCV Mat (grayscale) via a temporary PNG encode/decode round‑trip.

      This method favors robustness over raw speed; it handles arbitrary BufferedImage formats without manual channel/buffer shuffling. The temporary file is deleted on success; on failure, a RuntimeException is thrown wrapping the IOException.

      Parameters:
      img - source buffered image (any color model)
      Returns:
      an 8‑bit single‑channel Mat (CV_8UC1)
    • binarizeForLines

      public static org.bytedeco.opencv.opencv_core.Mat binarizeForLines(org.bytedeco.opencv.opencv_core.Mat gray)
      Binarizes a grayscale image for line detection using adaptive thresholding.

      Applies adaptiveThreshold with ADAPTIVE_THRESH_MEAN_C and THRESH_BINARY_INV to emphasize lines/edges against background noise. The block size (15) and constant (10) are conservative defaults for document scans; adjust as needed for different lighting/contrast.

      Parameters:
      gray - input grayscale image (CV_8UC1)
      Returns:
      a binary (inverted) Mat suitable for morphological line ops