march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          之前我在ScreenDemo類中用generateSnapshot()方法實(shí)現(xiàn)了對當(dāng)前屏幕的截圖,這里在原來的基礎(chǔ)上增加了printPointRGB方法用于獲取屏幕上的某一點(diǎn)的RGB。
          import java.awt.Color;
          import java.awt.Rectangle;
          import java.awt.Robot;
          import java.awt.Toolkit;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.IOException;

          import javax.imageio.ImageIO;


          public class ScreenDemo {
              public static File generateSnapshot() {
                  //File snapshotFile = new File("D:/" + System.currentTimeMillis() + ".jpg");
                  File snapshotFile = new File("D:/snapshot.jpg");
                  int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
                  int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
                  try {
                      Robot robot;
                      robot = new Robot();
                      BufferedImage image = robot.createScreenCapture(new Rectangle(width, height)); 
                      ImageIO.write(image, "jpg", snapshotFile);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return snapshotFile;  
              }
              
              /**
               * 取得圖像上指定位置像素的 rgb 顏色分量。
               *
               * 
          @param image 源圖像。
               * 
          @param x 圖像上指定像素位置的 x 坐標(biāo)。
               * 
          @param y 圖像上指定像素位置的 y 坐標(biāo)。
               * 
          @return 返回包含 rgb 顏色分量值的數(shù)組。元素 index 由小到大分別對應(yīng) r,g,b。
               
          */
              public static int[] getRGB(BufferedImage image, int x, int y) {
                  int[] rgb = null;
           
                  if (image != null && x < image.getWidth() && y < image.getHeight()) {
                      rgb = new int[3];
                      int pixel = image.getRGB(x, y);
                      rgb[0] = (pixel & 0xff0000) >> 16;
                      rgb[1] = (pixel & 0xff00) >> 8;
                      rgb[2] = (pixel & 0xff);
                  }
           
                  return rgb;
              }
           
              /**
               * 將RGB轉(zhuǎn)換為16進(jìn)制Hex
               *
               * 
          @param r red顏色分量
               * 
          @param g green顏色分量
               * 
          @param b blue顏色分量
               * 
          @return
               
          */
              public static String toHex(int r, int g, int b) {
                  return "#" + toHexValue(r) + toHexValue(g) + toHexValue(b);
              }
           
              private static String toHexValue(int number) {
                  StringBuilder builder = new StringBuilder(Integer.toHexString(number & 0xff));
                  while (builder.length() < 2) {
                      builder.append("0");
                  }
                  return builder.toString().toUpperCase();
              }
              
              public static void printPointRGB(int x, int y) {
                  try {
                      BufferedImage bi = ImageIO.read(new File("D:/snapshot.jpg"));
                      int[] rgb = getRGB(bi, x, y);
                      Color color = new Color(rgb[0], rgb[1], rgb[2]);
                      System.out.println("red = " + color.getRed());
                      System.out.println("green = " + color.getGreen());
                      System.out.println("blue = " + color.getBlue());
                      System.out.println("hex = " + toHex(color.getRed(), color.getGreen(), color.getBlue()));
                  } catch (IOException ex) {
                      ex.printStackTrace();
                  }
              }
              
              public static void main(String[] args) {
                  generateSnapshot();
                  printPointRGB(100, 100);
              }
          }
          posted on 2015-02-22 22:12 marchalex 閱讀(1975) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 广汉市| 天镇县| 马边| 元朗区| 汤原县| 临猗县| 越西县| 嘉定区| 宁津县| 腾冲县| 收藏| 上虞市| 南澳县| 红原县| 油尖旺区| 焦作市| 泉州市| 扎兰屯市| 阿拉善盟| 苏州市| 霍城县| 桂林市| 睢宁县| 永宁县| 平利县| 东平县| 大安市| 镇安县| 甘洛县| 黑山县| 南溪县| 昌江| 呼玛县| 钟祥市| 克山县| 平湖市| 隆回县| 福泉市| 宜宾市| 张掖市| 抚宁县|