转的其他人的。
自己试了可以。
忘记具体哪个地方了。
package com.yang.rm.system; import java.awt.AWTException; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ReadColorTest { /** * 读取一张图片的RGB值 * * @throws Exception */ public void getImagePixel(String image) throws Exception { int[] rgb = new int[3]; File file = new File(image); BufferedImage bi = null; try { bi = ImageIO.read(file); } catch (Exception e) { e.printStackTrace(); } int width = bi.getWidth(); int height = bi.getHeight(); int minx = bi.getMinX(); int miny = bi.getMinY(); System.out.println("width=" + width + ",height=" + height + "."); System.out.println("minx=" + minx + ",miniy=" + miny + "."); for (int i = minx; i < width; i++) { for (int j = miny; j < height; j++) { int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字 rgb[0] = (pixel & 0xff0000) >> 16; rgb[1] = (pixel & 0xff00) >> 8; rgb[2] = (pixel & 0xff); System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")"); } } } /** * 返回屏幕色彩值 * * @param x * @param y * @return * @throws AWTException */ public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。 Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。 rb = new Robot(); Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包 Dimension di = tk.getScreenSize(); // 屏幕尺寸规格 System.out.println(di.width); System.out.println(di.height); Rectangle rec = new Rectangle(0, 0, di.width, di.height); BufferedImage bi = rb.createScreenCapture(rec); int pixelColor = bi.getRGB(x, y); return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。 } /** * @param args */ public static void main(String[] args) throws Exception { int x = 0; ReadColorTest rc = new ReadColorTest(); x = rc.getScreenPixel(100, 345); System.out.println(x + " - "); rc.getImagePixel("D:\\car.jpg"); } }
i=76,j=554:(189,157,118) i=76,j=555:(171,139,100) i=76,j=556:(176,144,105) i=76,j=557:(183,153,117) i=76,j=558:(172,145,115) i=76,j=559:(114,87,57) i=76,j=560:(156,127,93) i=76,j=561:(173,143,105) i=76,j=562:(164,132,94) i=76,j=563:(170,138,99) i=76,j=564:(170,136,99) i=76,j=565:(167,135,97) i=76,j=566:(174,144,108) i=76,j=567:(158,128,90) i=76,j=568:(183,151,112) i=76,j=569:(185,153,115) i=76,j=570:(206,177,145) i=76,j=571:(134,107,78) i=76,j=572:(113,88,58) i=76,j=573:(190,165,135) i=76,j=574:(154,130,104) i=76,j=575:(94,68,41) i=76,j=576:(182,151,120) i=76,j=577:(190,157,122) i=76,j=578:(170,137,102) i=76,j=579:(167,136,105) i=76,j=580:(149,123,98) i=76,j=581:(91,69,46) i=76,j=582:(134,109,89) i=76,j=583:(139,114,94) i=76,j=584:(127,102,80) i=76,j=585:(126,101,81) i=76,j=586:(111,85,68) i=76,j=587:(111,85,70) i=76,j=588:(113,88,68) i=76,j=589:(119,94,72) i=76,j=590:(142,118,92) i=76,j=591:(140,116,88) i=76,j=592:(151,127,101) i=76,j=593:(86,64,41) i=76,j=594:(103,82,65) i=76,j=595:(114,93,76) i=76,j=596:(110,85,63) i=76,j=597:(166,139,110) i=76,j=598:(172,142,108) i=76,j=599:(139,107,69) i=76,j=600:(173,141,100) i=76,j=601:(205,172,131) i=76,j=602:(142,108,71) i=76,j=603:(201,168,133) i=76,j=604:(165,134,103) i=76,j=605:(136,109,79) i=76,j=606:(147,122,92) i=76,j=607:(144,118,91) i=76,j=608:(142,116,93) i=76,j=609:(140,114,91) i=76,j=610:(121,94,67) i=76,j=611:(168,139,109) i=76,j=612:(159,126,91) i=76,j=613:(167,133,95) i=76,j=614:(191,154,110) i=76,j=615:(197,159,120) i=76,j=616:(198,167,136) i=76,j=617:(102,76,51) i=76,j=618:(110,89,72) i=76,j=619:(116,97,83) i=76,j=620:(127,109,95) i=76,j=621:(92,74,62) i=76,j=622:(102,85,75) i=76,j=623:(94,80,71) i=76,j=624:(96,81,74) i=76,j=625:(95,80,73) i=76,j=626:(107,90,82) i=76,j=627:(99,82,74) i=76,j=628:(100,83,73) i=76,j=629:(110,93,83) i=76,j=630:(105,88,78) i=76,j=631:(104,90,81)