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小程序
          主站蜘蛛池模板: 淅川县| 海盐县| 德州市| 健康| 元谋县| 宁陵县| 盐池县| 上犹县| 咸宁市| 咸阳市| 鄂伦春自治旗| 克什克腾旗| 巴东县| 岳阳县| 安宁市| 大冶市| 临澧县| 广德县| 赤峰市| 金昌市| 四会市| 库伦旗| 凤庆县| 全南县| 北辰区| 察雅县| 筠连县| 红河县| 莱西市| 阳原县| 深圳市| 泾川县| 睢宁县| 松溪县| 巴南区| 大连市| 同江市| 滕州市| 南安市| 眉山市| 南郑县|