LightingTestingTask.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.fire;
  2. import java.awt.Color;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import javax.imageio.ImageIO;
  7. public class LightingTestingTask {
  8. public static void main(String[] args) {
  9. String fileName = "D://yellow.jpg";
  10. LightingTestingTask t = new LightingTestingTask();
  11. t.readImage(fileName);
  12. }
  13. public void readImage(String path) {
  14. try {
  15. // driver.manage().window().maximize();
  16. int[] rgb = new int[3];
  17. BufferedImage img = ImageIO.read(new File(path));
  18. int width = img.getWidth();
  19. int height = img.getHeight();
  20. int minx = img.getMinX();
  21. int miny = img.getMinY();
  22. System.out.println("width=" + width + ",height=" + height + ".");
  23. System.out.println("minx=" + minx + ",miniy=" + miny + ".");
  24. int red = 0;
  25. int yellow = 0;
  26. int green = 0;
  27. for (int i = minx; i < width; i++) {
  28. for (int j = miny; j < height; j++) {
  29. // System.out.println(i+":"+j);
  30. int w=20;
  31. int h=20;
  32. // if(height-j<=h && width-i>=w) {
  33. // //高度超出图片范围,但是宽度未超出
  34. //
  35. // }else {
  36. // BufferedImage rect = img.getSubimage(i, j, w, h);
  37. // }
  38. int pixel = img.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
  39. // Color c = new Color(pixel);
  40. // int r = c.getRed();
  41. // int g = c.getGreen();
  42. // int b = c.getBlue();
  43. // System.out.println(c + "\t红=" + r + "\t绿" + g + "\t蓝" + b);
  44. // int r1 = (pixel >> 16) & 0xFF;
  45. // int g1 = (pixel >> 8) & 0xFF;
  46. // int b1 = (pixel >> 0) & 0xFF;
  47. // System.out.println("红="+r1+"\t绿="+g1+"\t蓝="+b1);红=254 绿=167 蓝=186
  48. rgb[0] = (pixel & 0xff0000) >> 16;
  49. rgb[1] = (pixel & 0xff00) >> 8;
  50. rgb[2] = (pixel & 0xff);
  51. //黄 红=255 绿=250 蓝=194 红=255 绿=246 蓝=191
  52. //红=250 绿=248 蓝=191 红=247 绿=215 蓝=174 红=254 绿=216 蓝=179
  53. //绿 红=119 绿=255 蓝=191 红=152 绿=222 蓝=188 红=141 绿=223 蓝=177 红=117 绿=253 蓝=189 红=136 绿=211 蓝=168
  54. // 绿色i=251,j=203:(117,116,114)
  55. // 绿色i=251,j=204:(116,115,113)
  56. // 绿色i=251,j=205:(115,114,112)
  57. // 绿色i=251,j=206:(115,114,112)
  58. // 绿色i=251,j=207:(115,114,112)
  59. // 绿色i=251,j=208:(116,115,113)
  60. // 绿色i=251,j=209:(115,114,112)
  61. // 绿色i=251,j=210:(115,114,112)
  62. // 绿色i=251,j=211:(115,114,112)
  63. // 绿色i=251,j=212:(115,114,112)
  64. // 绿色i=251,j=213:(116,115,113)
  65. // 绿色i=251,j=214:(117,116,114)
  66. // 绿色i=251,j=215:(119,118,116)
  67. if(i==543 && j==483) {
  68. System.out.println("红="+rgb[0]+"\t绿="+rgb[1]+"\t蓝="+rgb[2]);
  69. }
  70. if (rgb[0] != 255 || rgb[1] != 255 || rgb[2] != 255) {
  71. // 红色判断
  72. if (rgb[0]>=244 && rgb[1] <=170 && rgb[2] <= 186) {
  73. red++;
  74. // System.out.println("红色i=" + i + ",j=" + j + ":(" + rgb[0] + ","
  75. // + rgb[1] + "," + rgb[2] + ")");
  76. } else if (rgb[0] <= 50 && rgb[1] >= 200 && rgb[2] <= 50) {
  77. // System.out.println("蓝色i=" + i + ",j=" + j + ":(" + rgb[0] + ","
  78. // + rgb[1] + "," + rgb[2] + ")");
  79. } else if (rgb[0] >= 135 && rgb[1] >= 200 && rgb[2] >=165) {
  80. green++;
  81. // System.out.println("绿色i=" + i + ",j=" + j + ":(" + rgb[0] + ","
  82. // + rgb[1] + "," + rgb[2] + ")");
  83. }else if(rgb[0] >= 244 && rgb[1] >= 215 && rgb[2] >=190) {
  84. yellow++;
  85. System.out.println("黄色i=" + i + ",j=" + j + ":(" + rgb[0] + ","
  86. + rgb[1] + "," + rgb[2] + ")");
  87. }
  88. }
  89. }
  90. }
  91. System.out.println("red: "+red+"yellow: "+yellow+"green: "+green);
  92. String maxColor = getMaxColor(red, yellow,green);
  93. if("".equals(maxColor)) {
  94. return ;
  95. }else if("red".equals(maxColor)) {
  96. //此时是红色
  97. System.out.println(maxColor);
  98. }else if("yellow".equals(maxColor)){
  99. System.out.println(maxColor);
  100. }else if("green".equals(maxColor)){
  101. System.out.println(maxColor);
  102. }
  103. System.out.println("跑完了...");
  104. } catch (IOException e) {
  105. // TODO Auto-generated catch block
  106. e.printStackTrace();
  107. }
  108. }
  109. private String getMaxColor(int red,int yellow,int green){
  110. String color = "";
  111. if(red>yellow && red>green) {
  112. color = "red";
  113. }else if(yellow>red && yellow>green) {
  114. color = "yellow";
  115. }else if(green>red && green>yellow) {
  116. color = "green";
  117. }
  118. return color;
  119. }
  120. }