这是我目前面临的挑战。
我有很多PDF,我必须删除其中的空白页面,而仅显示包含内容(文本或图像)的页面。
问题在于这些pdf是扫描的文档。
因此,空白页会被扫描仪留下一些脏污。
我进行了一些研究,最后得到了这段代码,该代码检查了白色或浅灰色的页面,占页面的99%。我需要灰度系数,因为扫描的文档有时不是纯白色的。
private static Boolean isBlank(PDPage pdfPage) throws IOException {
BufferedImage bufferedImage = pdfPage.convertToImage();
long count = 0;
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
Double areaFactor = (width * height) * 0.99;
for (int x = 0; x < width ; x++) {
for (int y = 0; y < height ; y++) {
Color c = new Color(bufferedImage.getRGB(x, y));
// verify light gray and white
if (c.getRed() == c.getGreen() && c.getRed() == c.getBlue()
&& c.getRed() >= 248) {
count++;
}
}
}
if (count >= areaFactor) {
return true;
}
return false;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句