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小程序
          主站蜘蛛池模板: 陆良县| 炉霍县| 布拖县| 绿春县| 乳山市| 万年县| 独山县| 雷山县| 乐业县| 高密市| 滦南县| 巨鹿县| 丹棱县| 二连浩特市| 廉江市| 大冶市| 安西县| 云霄县| 宣化县| 福鼎市| 大埔区| 巴林左旗| 类乌齐县| 井陉县| 逊克县| 翁源县| 昌宁县| 榆社县| 亳州市| 安溪县| 射洪县| 碌曲县| 巴林右旗| 都昌县| 靖宇县| 定兴县| 中西区| 潞西市| 双峰县| 吴江市| 华阴市|