当前位置:  技术问答>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: 

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.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java命名空间java.awt.image类volatileimage的类成员方法: image_ok定义及介绍
  • 请教:如何实现点击一个image后,该image在Applet中移动位置?有示例代码吗?谢谢!!!
  • java命名空间java.awt.image类volatileimage的类成员方法: image_restored定义及介绍
  • 请教:如何实现点击一个image后,该image在Applet中移动位置?有示例代码吗?谢谢!!
  • java命名空间java.awt.image类volatileimage的类成员方法: image_incompatible定义及介绍
  • 如何让一个image对象响应鼠标事件?
  • java命名空间javax.imageio类iioimage的类成员方法: image定义及介绍
  • 一个关于Image的问题
  • java命名空间java.awt.image接口imageobserver的类成员方法: properties定义及介绍
  • 电影缩略图生成工具 Image Grabber
  • java命名空间java.awt.image接口imageobserver的类成员方法: height定义及介绍
  • 如何将Image类型转成BufferedImage?
  • java命名空间java.awt.image接口imageobserver的类成员方法: width定义及介绍
  • 关于Image的问题(特急)
  • java命名空间java.awt.image接口imageobserver的类成员方法: framebits定义及介绍
  • 急!我使用一个继承了JComponent的类绘图,我想将绘的图绘到Image,有什么办法吗?
  • java命名空间java.awt.image接口imageobserver的类成员方法: somebits定义及介绍
  • 关于uboot的Dual Image
  • java命名空间java.awt.image接口imageobserver的类成员方法: allbits定义及介绍
  • Image Upload Preview
  • java命名空间java.awt类image的类成员方法: setaccelerationpriority定义及介绍
  • Image类中的getImage是怎么找图象文件的位置的?


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3