java数字图像处理基础使用imageio写图像文件示例
一个bufferedimage的像素数据储存在raster中,colormodel里面储存颜色空间,类型等信息,当前java只支持一下三种图像格式- jpg,png,gif,如何向让java支持其它格式,首先要 完成java中的图像读写接口,然后打成jar,加上启动参数- xbootclasspath/pnewimageformatio.jar即可。
java中如何读写一个图像文件,使用imageio对象即可。读图像文件的代码如下:
file file = new file("d:\\test\\blue_flower.jpg");
bufferedimage image = imageio.read(file);
写图像文件的代码如下:
file outputfile = new file("saved.png");
imageio.write(bufferedimage, "png",outputfile);
从bufferedimage对象中读取像素数据的代码如下:
int type= image.gettype();
if ( type ==bufferedimage.type_int_argb || type == bufferedimage.type_int_rgb )
return (int [])image.getraster().getdataelements(x, y, width, height, pixels );
else
return image.getrgb( x, y, width, height, pixels, 0, width );
首先获取图像类型,如果不是32位的int型数据,直接读写rgb值即可,否则需要从raster
对象中读取。
往bufferedimage对象中写入像素数据同样遵守上面的规则。代码如下:
int type= image.gettype();
if ( type ==bufferedimage.type_int_argb || type == bufferedimage.type_int_rgb )
image.getraster().setdataelements(x, y, width, height, pixels );
else
image.setrgb(x, y, width, height, pixels, 0, width );
读取图像可能因为图像文件比较大,需要一定时间的等待才可以,java advance image
processor api提供了mediatracker对象来跟踪图像的加载,同步其它操作,使用方法如下:
mediatracker tracker = new mediatracker(this); //初始化对象
tracker.addimage(image_01, 1); // 加入要跟踪的bufferedimage对象image_001
tracker.waitforid(1, 10000) // 等待10秒,让iamge_01图像加载
从一个32位int型数据cargb中读取图像rgb颜色值的代码如下:
1 int alpha = (cargb >> 24)& 0xff; //透明度通道
2 int red = (cargb >> 16) &0xff;
3 int green = (cargb >> 8) &0xff;
4 int blue = cargb & 0xff;
将rgb颜色值写入成一个int型数据crgb的代码如下:
crgb = (alpha << 24) | (red<< 16) | (green << 8) | blue;
创建一个bufferedimage对象的代码如下:
bufferedimage image = newbufferedimage(256, 256, bufferedimage.type_int_argb);
一个完整的源代码demo如下:
package com.gloomyfish.swing;
import java.awt.borderlayout;
import java.awt.dimension;
import java.awt.graphics;
import java.awt.graphics2d;
import java.awt.renderinghints;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import javax.imageio.imageio;
import javax.swing.jcomponent;
import javax.swing.jframe;
public class plasmademo extends jcomponent {
/**
*
*/
private static final long serialversionuid = -2236160343614397287l;
private bufferedimage image = null;
private int size = 256;
public plasmademo() {
super();
this.setopaque(false);
}
protected void paintcomponent(graphics g) {
graphics2d g2 = (graphics2d)g;
g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);
g2.drawimage(getimage(), 5, 5, image.getwidth(), image.getheight(), null);
}
private bufferedimage getimage() {
if(image == null) {
image = new bufferedimage(size, size, bufferedimage.type_int_argb);
int[] rgbdata = new int[size*size];
generatenoiseimage(rgbdata);
setrgb(image, 0, 0, size, size, rgbdata);
file outfile = new file("plasma.jpg");
try {
imageio.write(image, "jpg", outfile);
} catch (ioexception e) {
e.printstacktrace();
}
}
return image;
}
public void generatenoiseimage(int[] rgbdata) {
int index = 0;
int a = 255;
int r = 0;
int g = 0;
int b = 0;
for(int row=0; row<size; row++) {
for(int col=0; col<size; col++) {
// set random color value for each pixel
r = (int)(128.0 + (128.0 * math.sin((row + col) / 8.0)));
g = (int)(128.0 + (128.0 * math.sin((row + col) / 8.0)));
b = (int)(128.0 + (128.0 * math.sin((row + col) / 8.0)));
rgbdata[index] = ((clamp(a) & 0xff) << 24) |
((clamp(r) & 0xff) << 16) |
((clamp(g) & 0xff) << 8) |
((clamp(b) & 0xff));
index++;
}
}
}
private int clamp(int rgb) {
if(rgb > 255)
return 255;
if(rgb < 0)
return 0;
return rgb;
}
public void setrgb( bufferedimage image, int x, int y, int width, int height, int[] pixels ) {
int type = image.gettype();
if ( type == bufferedimage.type_int_argb || type == bufferedimage.type_int_rgb )
image.getraster().setdataelements( x, y, width, height, pixels );
else
image.setrgb( x, y, width, height, pixels, 0, width );
}
public static void main(string[] args) {
jframe frame = new jframe("noise art panel");
frame.setdefaultcloseoperation(jframe.exit_on_close);
frame.getcontentpane().setlayout(new borderlayout());
frame.getcontentpane().add(new plasmademo(), borderlayout.center);
frame.setpreferredsize(new dimension(400 + 25,450));
frame.pack();
frame.setvisible(true);
}
}