PixelGrabber获得图片的像素点值的数组变换后如何还原成图片
代码和输出的结果数组如下
我要改变数组元素的值后把它在还原成图片 该怎么做 求高手帮忙 小弟初学java
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;
public class GetImg {
public static void main(String args[]){
GetImg gi = new GetImg();
File _file = new File("D:\\毕业论文资料\\图片\\MyZone.jpg"); //读入文件
//Picture p1 = new Picture("D:\\毕业论文资料\\图片\\MyZone.jpg");
Image img;
try {
img = javax.imageio.ImageIO.read(_file);//构造Image对象
int wideth=img.getWidth(null); //得到源图宽
int height=img.getHeight(null); //得到源图长
System.out.println("宽和长"+wideth+height);
int buffer[] = new int[wideth*height];
buffer = gi.pixelGrabber(buffer,img);
for(int i =0;i<wideth*height;i++){
if(i%wideth==0)System.out.println();
System.out.printf("%-10d",buffer[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private int[] pixelGrabber(int buffer[],Image img) {
try {
int wideth=img.getWidth(null); //得到源图宽
int height=img.getHeight(null); //得到源图长
/* MediaTracker tracker = new MediaTracker(null);
tracker.addImage(img, 0);
tracker.waitForAll(); */
PixelGrabber grabber = new PixelGrabber(img, 0, 0, wideth, height, buffer, 0, wideth);
try {
grabber.grabPixels();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer;
}
}
这是部分值{ -1 -1 -1 -1 -3750202 -3750202 -3750202
-3750202 -3750202 -3750202 -3815995 -3815995 -3552823 -197380 -1 }
------解决方案--------------------
- Java code
// 将数组中的象素产生一个图像 Image tempImg=Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(iw,ih, pixels, 0, iw));