march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          之前我在ScreenDemo類中用generateSnapshot()方法實現了對當前屏幕的截圖,這里在原來的基礎上增加了printPointRGB方法用于獲取屏幕上的某一點的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 坐標。
               * 
          @param y 圖像上指定像素位置的 y 坐標。
               * 
          @return 返回包含 rgb 顏色分量值的數組。元素 index 由小到大分別對應 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轉換為16進制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 閱讀(1971) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 太仆寺旗| 儋州市| 牙克石市| 民乐县| 沁阳市| 阳曲县| 太原市| 全椒县| 蚌埠市| 祁门县| 叶城县| 房产| 奉节县| 百色市| 林西县| 西畴县| 禹州市| 阳泉市| 莱阳市| 扬中市| 霍林郭勒市| 兴安盟| 济阳县| 孟连| 巴林左旗| 资中县| 黔南| 铜陵市| 资溪县| 通江县| 彝良县| 泾源县| 广河县| 新疆| 杨浦区| 安阳县| 神农架林区| 贵定县| 长汀县| 黎川县| 临夏县|