// PixelColor.java
// Get the color of a specific pixel
import java.awt.image.PixelGrabber;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.image.PixelGrabber;
import java.lang.InterruptedException;public class PixelColor
{
public static void main(String[] args)
{
ImageIcon imageIcon = new ImageIcon("Sample.jpg");
Image picture = imageIcon.getImage();
int height = imageIcon.getIconHeight();
int width = imageIcon.getIconWidth(); int[] pixels = new int[width * height];
PixelGrabber pg = new PixelGrabber(picture, 0, 0, width, height, pixels, 0, width);
try
{
pg.grabPixels();
}
catch (InterruptedException e)
{
System.err.println("interrupted waiting for pixels!");
} // point(x,y)
int x = 0;
int y = 8;
int pixel = pixels[x + y * width];
// int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
// System.out.println("alpha: " + alpha);
System.out.println("red: " + red);
System.out.println("green: " + green);
System.out.println("blue: " + blue);
}
}
// Get the color of a specific pixel
import java.awt.image.PixelGrabber;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.image.PixelGrabber;
import java.lang.InterruptedException;public class PixelColor
{
public static void main(String[] args)
{
ImageIcon imageIcon = new ImageIcon("Sample.jpg");
Image picture = imageIcon.getImage();
int height = imageIcon.getIconHeight();
int width = imageIcon.getIconWidth(); int[] pixels = new int[width * height];
PixelGrabber pg = new PixelGrabber(picture, 0, 0, width, height, pixels, 0, width);
try
{
pg.grabPixels();
}
catch (InterruptedException e)
{
System.err.println("interrupted waiting for pixels!");
} // point(x,y)
int x = 0;
int y = 8;
int pixel = pixels[x + y * width];
// int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
// System.out.println("alpha: " + alpha);
System.out.println("red: " + red);
System.out.println("green: " + green);
System.out.println("blue: " + blue);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货