1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
// Verify that a screencap PNG is non-uniform - i.e., MediaCodec
// actually painted something. Exits 0 if the image has at least
// `--min` percent of pixels that differ from pixel (0,0); 1 otherwise.
//
// java test-rig/Pixhash.java <png> # default min=5%
// java test-rig/Pixhash.java <png> --min 1
//
// ImageIO/BufferedImage live in java.desktop, which the image's
// openjdk-17-jdk-headless package does ship (headless only disables
// display/input, not imaging) - no extra dependency needed.
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public final class Pixhash {
public static void main(String[] args) throws IOException {
if (args.length < 1) {
System.err.println("usage: java Pixhash.java <png> [--min N]");
System.exit(2);
}
double minPercent = 5.0;
for (int i = 1; i < args.length; i++) {
if ("--min".equals(args[i]) && i + 1 < args.length) {
minPercent = Double.parseDouble(args[++i]);
}
}
BufferedImage img = ImageIO.read(new File(args[0]));
if (img == null) {
System.err.println("pixhash: could not read " + args[0]);
System.exit(2);
}
int w = img.getWidth(), h = img.getHeight();
int ref = img.getRGB(0, 0);
long total = (long) w * h;
long diff = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (img.getRGB(x, y) != ref) diff++;
}
}
double pct = 100.0 * diff / total;
System.out.printf("pixhash: %d/%d differ from (0,0)=%08x => %.2f%%%n",
diff, total, ref, pct);
if (pct < minPercent) {
System.err.printf("pixhash: only %.2f%% differ, need %.2f%%%n", pct, minPercent);
System.exit(1);
}
}
}
|