Java图像处理所用到的主要类

Java图像处理所用到的重要类

1)打包生成图像数据的类

java.awt.image 
类 MemoryImageSource

java.lang.Object
  java.awt.image.MemoryImageSource
所有已实现的接口:
ImageProducer
public class MemoryImageSource
extends Object
implements ImageProducer
 

此类是 ImageProducer 接口的一个实现,该接口使用一个数组为 Image 生成像素值。下面的示例计算了一幅 100x100 的图像,表示沿 X 轴从黑色渐变到蓝色,沿 Y 轴从黑色渐变到红色:

           int w = 100;
          int h = 100;
          int pix[] = new int[w * h];
          int index = 0;
          for (int y = 0; y < h; y++) {
              int red = (y * 255) / (h - 1);
              for (int x = 0; x < w; x++) {
                  int blue = (x * 255) / (w - 1);
                  pix[index++] = (255 << 24) | (red << 16) | blue;
              }
          }
          Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));

MemoryImageSource 还能够管理随时间的推移而变化的内存图像,以实现动画或自定义呈现。下面的示例显示了如何设置动画源并通知数据的改变(改编自 Garth Dickie 的 MemoryAnimationSourceDemo):

          int pixels[];
          MemoryImageSource source;

          public void init() {
              int width = 50;
              int height = 50;
              int size = width * height;
              pixels = new int[size];

              int value = getBackground().getRGB();
              for (int i = 0; i < size; i++) {
                  pixels[i] = value;
              }

              source = new MemoryImageSource(width, height, pixels, 0, width);
              source.setAnimated(true);
              image = createImage(source);
          }

          public void run() {
              Thread me = Thread.currentThread( );
              me.setPriority(Thread.MIN_PRIORITY);

              while (true) {
                  try {
                      thread.sleep(10);
                  } catch( InterruptedException e ) {
                      return;
                  }

                  // Modify the values in the pixels array at (x, y, w, h)

                  // Send the new data to the interested ImageConsumers
                  source.newPixels(x, y, w, h);
              }
          }
 
(2)生成图像的类

java.awt 
类 Component

java.lang.Object
  java.awt.Component
所有已实现的接口:
ImageObserver, MenuContainer, Serializable

一般的情况下,我们不会直接使用这个类,而是使用它的子类,如Frame等

而在它的成员方法中,对于图像处理有一个很有用的方法就是

createImage

public Image createImage(ImageProducer producer)
由指定的图像生成器创建一幅图像。

 

参数:
producer - 图像生成器
返回:
生成的图像
从以下版本开始:
JDK1.0
当我们对图像像素数据处理完以后,我们就可以利用这个像素矩阵生成一个MemoryImageSource对象
再利用上述方法生成一个Image对象。
 
(3)获取/生成图像的类

java.awt 类 Toolkit

java.lang.Object
  java.awt.Toolkit
这个类提供多种读取与创建图像的方法,如:
Image createImage(byte[] imagedata)            创建一幅图像,该图像对存储在指定字节数组中的图像进行解码。
abstract  Image createImage(byte[] imagedata, int imageoffset, int imagelength)            创建一幅图像,该图像以指定偏移量和长度对存储在指定字节数组中的图像进行解码。
abstract  Image createImage(ImageProducer producer)            使用指定的图像生成器创建一幅图像。
abstract  Image createImage(String filename)            返回从指定文件获取像素数据的图像。
abstract  Image createImage(URL url)            返回一幅图像,该图像从指定 URL 获取像素数据。

abstract  Image getImage(String filename)            返回一幅图像,该图像从指定文件中获取像素数据,图像格式可以是 GIF、JPEG 或 PNG。
abstract  Image getImage(URL url)            返回一幅图像,该图像从指定 URL 获取像素数据。
通常我们使用它的默认工具包,通过如下静态方法获取:

static Toolkit getDefaultToolkit()            获取默认工具包。
 
(4)获取图像像素数据的类

java.awt.image
类 PixelGrabber

java.lang.Object
  java.awt.image.PixelGrabber
所有已实现的接口:
ImageConsumer

public class PixelGrabber
extends Object
implements ImageConsumer
 

PixelGrabber 类实现可以附加在 Image 或 ImageProducer 对象上以获得该图像像素子集的 ImageConsumer。下面是一个示例:

public void handlesinglepixel(int x, int y, int pixel) {
          int alpha = (pixel >> 24) & 0xff;
          int red     = (pixel >> 16) & 0xff;
          int green = (pixel >>    8) & 0xff;
          int blue    = (pixel        ) & 0xff;
          // Deal with the pixel as necessary...
 }

 public void handlepixels(Image img, int x, int y, int w, int h) {
          int[] pixels = new int[w * h];
          PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
          try {
              pg.grabPixels();
          } catch (InterruptedException e) {
              System.err.println("interrupted waiting for pixels!");
              return;
          }
          if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
              System.err.println("image fetch aborted or errored");
              return;
          }
          for (int j = 0; j < h; j++) {
              for (int i = 0; i < w; i++) {
                  handlesinglepixel(x+i, y+j, pixels[j * w + i]);
              }
          }
 }
(5)显示图像的类
显示图像的方法有很多种,其中比较常用的方法就是利用ImageIcon类包装一个Image对象,再
通过设置一个Label对象的Icon来显示图像。

javax.swing
类 ImageIcon

java.lang.Object
  javax.swing.ImageIcon
所有已实现的接口:
Serializable, Accessible, Icon
ImageIcon(Image image) 
            根据图像对象创建一个 ImageIcon。
来自: http://hi.baidu.com/xianle/blog/item/7c4d11d5cb3f23c750da4b6a.html

1 楼 keke8614 2011-03-10  
垃圾,直接贴API文档有意思么?
2 楼 muchao_119 2011-03-15  
keke8614 写道
垃圾,直接贴API文档有意思么?

自己学习,做个笔记,你厉害别来javaeye看帖,别跟愤青似的满哪喷