123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package com.fire;
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class LightingTestingTask {
- public static void main(String[] args) {
- String fileName = "D://yellow.jpg";
- LightingTestingTask t = new LightingTestingTask();
- t.readImage(fileName);
- }
-
- public void readImage(String path) {
- try {
- // driver.manage().window().maximize();
- int[] rgb = new int[3];
- BufferedImage img = ImageIO.read(new File(path));
- int width = img.getWidth();
- int height = img.getHeight();
- int minx = img.getMinX();
- int miny = img.getMinY();
-
- System.out.println("width=" + width + ",height=" + height + ".");
- System.out.println("minx=" + minx + ",miniy=" + miny + ".");
- int red = 0;
- int yellow = 0;
- int green = 0;
- for (int i = minx; i < width; i++) {
- for (int j = miny; j < height; j++) {
- // System.out.println(i+":"+j);
- int w=20;
- int h=20;
- // if(height-j<=h && width-i>=w) {
- // //高度超出图片范围,但是宽度未超出
- //
- // }else {
- // BufferedImage rect = img.getSubimage(i, j, w, h);
- // }
-
-
- int pixel = img.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
- // Color c = new Color(pixel);
- // int r = c.getRed();
- // int g = c.getGreen();
- // int b = c.getBlue();
- // System.out.println(c + "\t红=" + r + "\t绿" + g + "\t蓝" + b);
- // int r1 = (pixel >> 16) & 0xFF;
- // int g1 = (pixel >> 8) & 0xFF;
- // int b1 = (pixel >> 0) & 0xFF;
- // System.out.println("红="+r1+"\t绿="+g1+"\t蓝="+b1);红=254 绿=167 蓝=186
- rgb[0] = (pixel & 0xff0000) >> 16;
- rgb[1] = (pixel & 0xff00) >> 8;
- rgb[2] = (pixel & 0xff);
- //黄 红=255 绿=250 蓝=194 红=255 绿=246 蓝=191
- //红=250 绿=248 蓝=191 红=247 绿=215 蓝=174 红=254 绿=216 蓝=179
- //绿 红=119 绿=255 蓝=191 红=152 绿=222 蓝=188 红=141 绿=223 蓝=177 红=117 绿=253 蓝=189 红=136 绿=211 蓝=168
-
- // 绿色i=251,j=203:(117,116,114)
- // 绿色i=251,j=204:(116,115,113)
- // 绿色i=251,j=205:(115,114,112)
- // 绿色i=251,j=206:(115,114,112)
- // 绿色i=251,j=207:(115,114,112)
- // 绿色i=251,j=208:(116,115,113)
- // 绿色i=251,j=209:(115,114,112)
- // 绿色i=251,j=210:(115,114,112)
- // 绿色i=251,j=211:(115,114,112)
- // 绿色i=251,j=212:(115,114,112)
- // 绿色i=251,j=213:(116,115,113)
- // 绿色i=251,j=214:(117,116,114)
- // 绿色i=251,j=215:(119,118,116)
-
- if(i==543 && j==483) {
- System.out.println("红="+rgb[0]+"\t绿="+rgb[1]+"\t蓝="+rgb[2]);
- }
- if (rgb[0] != 255 || rgb[1] != 255 || rgb[2] != 255) {
-
- // 红色判断
- if (rgb[0]>=244 && rgb[1] <=170 && rgb[2] <= 186) {
- red++;
- // System.out.println("红色i=" + i + ",j=" + j + ":(" + rgb[0] + ","
- // + rgb[1] + "," + rgb[2] + ")");
- } else if (rgb[0] <= 50 && rgb[1] >= 200 && rgb[2] <= 50) {
- // System.out.println("蓝色i=" + i + ",j=" + j + ":(" + rgb[0] + ","
- // + rgb[1] + "," + rgb[2] + ")");
- } else if (rgb[0] >= 135 && rgb[1] >= 200 && rgb[2] >=165) {
- green++;
- // System.out.println("绿色i=" + i + ",j=" + j + ":(" + rgb[0] + ","
- // + rgb[1] + "," + rgb[2] + ")");
- }else if(rgb[0] >= 244 && rgb[1] >= 215 && rgb[2] >=190) {
- yellow++;
- System.out.println("黄色i=" + i + ",j=" + j + ":(" + rgb[0] + ","
- + rgb[1] + "," + rgb[2] + ")");
- }
- }
- }
- }
- System.out.println("red: "+red+"yellow: "+yellow+"green: "+green);
- String maxColor = getMaxColor(red, yellow,green);
- if("".equals(maxColor)) {
- return ;
- }else if("red".equals(maxColor)) {
- //此时是红色
- System.out.println(maxColor);
- }else if("yellow".equals(maxColor)){
- System.out.println(maxColor);
- }else if("green".equals(maxColor)){
- System.out.println(maxColor);
- }
- System.out.println("跑完了...");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- private String getMaxColor(int red,int yellow,int green){
- String color = "";
- if(red>yellow && red>green) {
- color = "red";
- }else if(yellow>red && yellow>green) {
- color = "yellow";
- }else if(green>red && green>yellow) {
- color = "green";
- }
- return color;
- }
- }
|