我的问题是我拍摄了一张图像,然后我更改了一些像素值,然后将它们设置到所需的位置,然后我写入图像,然后我使用 ImageIO.read() 通过使用图像对象读取图像我没有得到以前的值.. .那么我怎样才能得到以前的值
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class LoadImageApp extends JFrame implements ActionListener
{
BufferedImage img=null;
BufferedImage img1=null;
BufferedImage mask=null;
BufferedImage rao=null;
JButton b1;
int pixel[][];
int w,h;
JFileChooser chooser;
File file;
public void paint(Graphics g)
{
g.drawImage(img,50,50,150,200,this);
g.drawImage(mask,600,100,150,200,this);
}
public LoadImageApp()
{
super("Image...");
setSize(800,800);
setLayout(new FlowLayout());
b1=new JButton("Select Image");
b1.setSize(250,20);
add(b1);
b1.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
try
{
chooser = new JFileChooser("C:/Users/Ravi/Desktop");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
file=chooser.getSelectedFile();
img=ImageIO.read(file);
mask=new BufferedImage(img.getWidth(),img.getHeight(),img.getType());
repaint();
img1=img.getSubimage(0,0,100,150);
}
repaint();
pixel=getPixels(img);
int i=80,j=50;
int temp[]={1,1,1,1};
int temp1=setPixel(temp);
for(j=0;j<w;j++)
pixel[i][j]=temp1;
System.out.println("pixels before.."+pixel[80][3]);
this.setPixels();//setting pixels....
ImageIO.write(mask,"jpeg",new File("aft-con.jpeg"));//write the image
BufferedImage next=ImageIO.read(new File("aft-con.jpeg"));
ord=getPixels(next);//getting the previous values...
org=getARGB(ord[80][3]);
System.out.println("pixels before.."+pixel[80][3]);//but it is not same...
}
catch(Exception e)
{
System.out.println("error in action");
}
}
public int setPixel(int rgb[])
{
int pixel = (((rgb[0] & 0x0ff)<<24)|((rgb[1] & 0x0ff)<<16)|((rgb[2] & 0x0ff)<<8)|(rgb[3] & 0x0ff));
return pixel;
}
public int[] getARGB(int pixel)
{
int rgb[]=new int[4];
rgb[0] =((pixel >> 24) & 0xff);
rgb[1] = ((pixel >> 16) & 0xff);
rgb[2] =((pixel >> 8) & 0xff);
rgb[3] =((pixel ) & 0xff);
return rgb;
}
public int[][] getPixels(BufferedImage img)
{
w=img.getWidth();
h=img.getHeight();
int pixels[][]=new int[h][w];
int i=0,j=0;
try
{
for(i=0;i<h;i++)
for(j=0;j<w;j++)
pixels[i][j]=img.getRGB(j,i);
}
catch(Exception e)
{
System.out.println("i:"+i+" j:"+j);
}
return pixels;
}
public void setPixels()
{
w=img.getWidth();
h=img.getHeight();
int i=0,j=0;
try
{
for(i=0;i<h;i++)
for(j=0;j<w;j++)
mask.setRGB(j,i,pixel[i][j]);
}
catch(Exception e)
{
System.out.println("error at setting pixel");
}
}
public static void main(String[] args)
{
LoadImageApp f = new LoadImageApp();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}