配合对屏幕的截图操作,即可实现黑屏检测。稍微修改可以实现对任意指定颜色的图片进行判断,也可以实现纯色检测。
黑图检测代码如下。
public class SimpleColor { public static boolean isSimpleColorImg(String imgPath, float percent) throws IOException { BufferedImage src = ImageIO.read(new File(imgPath)); int height = src.getHeight(); int width = src.getWidth(); int count = 0, pixel = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { pixel = src.getRGB(i, j); Color color = new Color(pixel); int R = color.getRed(); int G = color.getGreen(); int B = color.getBlue(); if(R < 30 && G < 30 && B < 30) { count++; } if(count / (height * width) > percent) { return true; } } } return false; } public static void main(String args[]) throws IOException { System.out.println(isSimpleColorImg("/home/wangzhou/Downloads/black.jpeg", 0.99f)); } }
看看透明图。
public static boolean isTransparentScreen(String imgPath, float percent) throws IOException { BufferedImage src = ImageIO.read(new File(imgPath)); int height = src.getHeight(); int width = src.getWidth(); int count = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if((src.getRGB(i,j)>>24)==0){ count++; } if (count / (height * width) > percent) { return true; } } } return false; }