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 阅读(1923) 评论(0)  编辑  收藏 所属分类: java小程序

只有注册用户登录后才能发表评论。


网站导航: