這邊的 blank 指的是一張非常接近白色的圖片, 而 Empty 則是指一張 byte[].length == 0 的檔案
private static boolean isBlank(BufferedImage bi) {
long count = 0;
long total = 0;
double totalVariance = 0;
double stdDev = 0;
int height = bi.getHeight();
int width = bi.getWidth();
int[] pixels = new int[width * height];
PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0,
width);
try {
pg.grabPixels();
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
count++;
int pixel = pixels[j * width + i];
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
int pixelValue = new Color(red, green, blue, 0).getRGB();
total += pixelValue;
double avg = total / count;
totalVariance += Math.pow(pixelValue - avg, 2);
stdDev = Math.sqrt(totalVariance / count);
}
}
} catch (Exception e) {
}
return (stdDev < 1000);
}
view raw gistfile1.java hosted with ❤ by GitHub
這邊是 pdfbox 擷取圖片的範例, 簡單說就是以去計算 ByteArrayOutputStream 的 byte[] 的長度
private boolean isEmptyImage(PDXObjectImage image) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.write2OutputStream(baos);
baos.flush();
int size = baos.toByteArray().length;
baos.close();
if (size < 1) {
String msg = "ignore empty image";
System.out.println(msg);
log.info(msg);
return true;
}
return false;
}
view raw gistfile1.java hosted with ❤ by GitHub
REF: [http://www.rgagnon.com/javadetails/java-0631.html](http://www.rgagnon.com/javadetails/java-0631.html)