直接赋代码
public void getImagePixel(String image) throws Exception {
System.out.println("---------start-----------");
int[] rgb = new int[3];
File file = new File(image);
BufferedImage bi;
//输出到指定文件
String filename = "/Users/zou/Desktop/xxxx";
try {
bi = ImageIO.read(file);
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
// +多少自己取值 值越小表示取的每一个像素越小
for (int j = miny; j < height; j=j+4) {
for (int i = minx; i < width; i=i+4) {
// 转16进制
int pixel = bi.getRGB(i, j);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
String str = "<font color='" + String.format("#%02X%02X%02X", rgb[0], rgb[1], rgb[2]) + "'>猪</font>";
Files.write(Paths.get(filename), str.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}
Files.write(Paths.get(filename), "<br>".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}
System.out.println("------------end----------");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
test rc = new test();
rc.getImagePixel("/Users/zou/Desktop/WeChatdd730411a211ddae88a71623e38f7014.png");
}