当前位置: 技术问答>java相关
如何从一个Image中得到某个像素的值?
来源: 互联网 发布时间:2014-12-31
本文导语: 如何从一个Image中得到某个像素的值,我听同学说好像用GetPixel,可是在Image中我没有找到这个方法,难道是在别的类中么? | You're offscreen image should be a bufferedimage, which you then draw on th...
如何从一个Image中得到某个像素的值,我听同学说好像用GetPixel,可是在Image中我没有找到这个方法,难道是在别的类中么?
|
You're offscreen image should be a bufferedimage, which you then draw on the screen with drawImage(bufferedImage, x, y, observer).. inside the paint method of course. If you need to get to it's pixels then here are a couple choices..
if all you need is to get the color (RGB component) of the pixel then use the BufferedImage's getRGB() methods as follows:
this returns the color of the pixel at x, y and is returned as an 8-bit alpha, red, green, and blue component packed into an int, such that 1-8 (blue) 9-16(green) 16-24 (red) 25-32 (alpha). If the images data is stored in a different color format is converted before being returned.
if you want to get a rectangle use:
this return colors for a rectangle of pixels. The rectangle's size is according to the first 4 parameters, the rgbArray can be null and if so an int[] array will be created for you, the pixels colors are written into the array starting at the offset, and scansize is how many array elements should be in each row of returned data.
if you desire even more control then you can get the BufferedImage's Raster and do pixel level manipulation there.. using the following:
These will return samples for the pixels located at the given coordinates, each element returned will contain one of the pixel's samples. You can pass in "null" for the array and one will be created for you.
The above do the same as the previous group but returns a rectangle of pixels as described by the first 4 parameters. You can once again pass in a null array and one will be created.
Those are just a sample of the methods you can use to get the pixel to mess with it.. there are plenty more.. using ColorModel classes, SampleModel classes, etc.
Plus almost every "get" method has a "set" counterpart. So once you get the pixel(s) you wish to change, and then do so, you can use the set method to replace the pixel.. such as:
Where rgb as a packed integer containing alpha, red, green, and blue components.
There's a lot you can do with an image using Java2D.. if you would like a more specific example, just post a hypothetical question such as I want to remove all the red color from the pixel at 4, 4.
Hope that helps,
if all you need is to get the color (RGB component) of the pixel then use the BufferedImage's getRGB() methods as follows:
public int getRGB(int x, int y)
this returns the color of the pixel at x, y and is returned as an 8-bit alpha, red, green, and blue component packed into an int, such that 1-8 (blue) 9-16(green) 16-24 (red) 25-32 (alpha). If the images data is stored in a different color format is converted before being returned.
if you want to get a rectangle use:
public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
this return colors for a rectangle of pixels. The rectangle's size is according to the first 4 parameters, the rgbArray can be null and if so an int[] array will be created for you, the pixels colors are written into the array starting at the offset, and scansize is how many array elements should be in each row of returned data.
if you desire even more control then you can get the BufferedImage's Raster and do pixel level manipulation there.. using the following:
public int[] getPixel(int x, int y, int[] iArray) public double[] getPixel(int x, int y, double[] dArray) public float[] getPixel(int x, int y, float[] fArray)
These will return samples for the pixels located at the given coordinates, each element returned will contain one of the pixel's samples. You can pass in "null" for the array and one will be created for you.
public int[] getPixels(int x, int y, int w, int h, int[] iArray) public double[] getPixels(int x, int y, int w, int h, double[] dArray) public float[] getPixels(int x, int y, int w, int h, float[] fArray)
The above do the same as the previous group but returns a rectangle of pixels as described by the first 4 parameters. You can once again pass in a null array and one will be created.
Those are just a sample of the methods you can use to get the pixel to mess with it.. there are plenty more.. using ColorModel classes, SampleModel classes, etc.
Plus almost every "get" method has a "set" counterpart. So once you get the pixel(s) you wish to change, and then do so, you can use the set method to replace the pixel.. such as:
public void setRGB(int x, int y, int rgb)
Where rgb as a packed integer containing alpha, red, green, and blue components.
There's a lot you can do with an image using Java2D.. if you would like a more specific example, just post a hypothetical question such as I want to remove all the red color from the pixel at 4, 4.
Hope that helps,
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。