Java图像处理所用到的主要类
此类是 ImageProducer 接口的一个实现,该接口使用一个数组为 Image 生成像素值。下面的示例计算了一幅 100x100 的图像,表示沿 X 轴从黑色渐变到蓝色,沿 Y 轴从黑色渐变到红色: MemoryImageSource 还能够管理随时间的推移而变化的内存图像,以实现动画或自定义呈现。下面的示例显示了如何设置动画源并通知数据的改变(改编自 Garth Dickie 的 MemoryAnimationSourceDemo): 一般的情况下,我们不会直接使用这个类,而是使用它的子类,如Frame等 而在它的成员方法中,对于图像处理有一个很有用的方法就是 PixelGrabber 类实现可以附加在 Image 或 ImageProducer 对象上以获得该图像像素子集的 ImageConsumer。下面是一个示例:
java.awt.image
类 MemoryImageSourcejava.lang.Object
java.awt.image.MemoryImageSource
public class MemoryImageSource
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));
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
类 Componentjava.lang.Object
java.awt.Component
createImage
public Image createImage(ImageProducer producer)
producer
- 图像生成器当我们对图像像素数据处理完以后,我们就可以利用这个像素矩阵生成一个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
类 PixelGrabberjava.lang.Object
java.awt.image.PixelGrabber
public class PixelGrabber
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
类 ImageIconjava.lang.Object
javax.swing.ImageIcon
ImageIcon(Image image)
根据图像对象创建一个 ImageIcon。
自己学习,做个笔记,你厉害别来javaeye看帖,别跟愤青似的满哪喷