java读取图像的rpg以及灰度值,可以对rgb进行修改后输出新图像
/**
*java读取图像的rpg以及灰度值,可以对rgb进行修改后输出新图像
*/
File file = new File(filename);
BufferedImage BI = null;
try{
BI = ImageIO.read(file);
}catch(Exception e){
e.printStackTrace();
}
int width = BI.getWidth();
int height = BI.getHeight();
int minx = BI.getMinX();
int miny = BI.getMinY();
int[] rgb = new int[3];
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = BI.getRGB(i, j);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
list[i][j] = (rgb[0]*150+rgb[1]*59+rgb[2]*11+150)/150;
/**
*利用BI.setRGB对图像的像素值进行修改
*/
BI.setRGB(j,i,img[j][r]);
Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = it.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("c:/"+p+".png"));
writer.setOutput(ios);
writer.write(BI);
BI.flush();
ios.flush();
/**
*总结:java的图像处理主要基于BufferedImage类,如果要实现的剪切缩放等则需与其他图像类使用,用户可自己实现一个图像处理的工具类,csdn中有人已经贴出具体实现:参照
http://blog.csdn.net/zhangzhikaixinya/article/details/8459400
*/