// 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 # default min=5% // java test-rig/Pixhash.java --min 1 // // We avoid AWT's BufferedImage because some headless JDKs ship without // it; ImageIO is in java.desktop which IS present in the openjdk-17-jdk // package in our image, so it's fine. 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 [--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); } } }