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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;
public class Main3 {
public static void main(String[] arg) { int writecount = 0; int blackcount = 0; int redcount = 0; int greencount = 0; int bluecount = 0;
int count = 0;
System.out.println("开始读取图片");
File file = new File("e:\\3px.gif");
BufferedImage bi = null; if (null != file) {
try { bi = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); }
int width = bi.getWidth(); int height = bi.getHeight(); int minx = bi.getMinX(); int miny = bi.getMinY();
int rgba1 = bi.getRGB(41, 1332); Color mycolor1 = new Color(rgba1, true); int red1 = mycolor1.getRed(); int green1 = mycolor1.getGreen(); int blue1 = mycolor1.getBlue();
System.out.println("mycolor1:" + mycolor1 + ",red:" + red1 + ",green:" + green1 + ",blue:" + blue1);
System.out.println("图片的宽度为:" + width + " 图片的高度为:" + height);
for (int i = minx; i < width; i++) { for (int j = miny; j < height; j++) { int rgba = bi.getRGB(i, j); Color mycolor = new Color(rgba, true); int red = mycolor.getRed(); int green = mycolor.getGreen(); int blue = mycolor.getBlue();
if (red >= 250 && green < 5 & blue < 5) { redcount++; } if (red < 5 && green >= 250 && blue < 5) { greencount++; } if (red < 100 && green < 100 && blue >= 100) { bluecount++; } if (red > 250 && green > 250 && blue > 250) { writecount++; } if (red < 5 && green < 5 && blue < 5) { blackcount++; }
count++; } }
System.out.println("这张图总像素点个数:" + count);
System.out.println("黑色像素点个数有:" + blackcount);
System.out.println("白色像素点个数有:" + writecount);
System.out.println("红色像素点个数有:" + redcount);
System.out.println("绿色色像素点个数有:" + greencount);
System.out.println("蓝色像素点个数有:" + bluecount); } else { System.out.println("未读取到图片"); }
} }
|