I displayed the image, how to save it

I was able to display an image after acquiring it from scanner, now the question is how to save it? Is there any simple code, tutorial etc...
[148 byte] By [tleis1a] at [2007-10-1 2:05:14]
# 1
What are you displaying the image in?
james91a at 2007-7-8 10:35:13 > top of Java-index,Administration Tools,Sun Connection...
# 2

Thank you for replying

after acquiring the image I simply display it this way.

public void paint(Graphics g)

{ if (null!=image)

g.drawImage(image, 20, 40, this);

}

}

I found out some code that saves my image as "SaveTest.jpg". but I want to save many images using counter.

public void save()

{ try

{ File f=new File("SaveTest.jpg");

BufferedImage bimg=new BufferedImage(image.getWidth(null),

image.getHeight(null), BufferedImage.TYPE_INT_RGB);

bimg.createGraphics().drawImage(image, 0, 0, null);

FileOutputStream out=new FileOutputStream(f);

JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(bimg);

param.setQuality(1.0f, false);

encoder.setJPEGEncodeParam(param);

encoder.encode(bimg);

out.close();

}

catch (IOException e)

{ e.printStackTrace();

}

}

by the way, isn't there any dialog boxes allowing me to save my image when the user request?

tleis1a at 2007-7-8 10:35:13 > top of Java-index,Administration Tools,Sun Connection...
# 3

You can use the saveFileDialog here some code I have for it:

JFileChooser chooser = new JFileChooser();

int retval;

//Then you do this to display it

returnVal = chooser.showSaveDialog(this);

//after that you can use if statements on the options chosen

if (returnVal == JFileChooser.APPROVE_OPTION)

{

As for the saving problem your having, I'll have to look that up a little later..I'm getting ready for schoo right now :)

james91a at 2007-7-8 10:35:13 > top of Java-index,Administration Tools,Sun Connection...
# 4

See, I just don't know how to get a reference to where your drawing it using the graphics object.

Now here's how I saved contents of a what was in my textpane into a file

textPane.write( new FileWriter(getFile));

getFile is a string holding the file chosen in the JFielChooser.

Now, if we knew how to reference the drawing area, we could probably just replace textPane with the reference

james91a at 2007-7-8 10:35:13 > top of Java-index,Administration Tools,Sun Connection...
# 5

thank you james

I am not good enough in java programming. I still don't know how to hold the file Chosen in the getFile string. I wish you illustrate it in more code.

Moreover, how can I save a sequence of files using a counter.

public ImageFrame(TwainSource source)

{ super("Image: "+frameCount);

...

...

frameCount++;

save();

}

}

but the save(); will replace the old one, i.e. in every loop the new image is saved in the same file SaveTest.jpg. as illustrated in my first post.

tleis1a at 2007-7-8 10:35:13 > top of Java-index,Administration Tools,Sun Connection...
# 6
Im not sure you can even save something like this but you might. Why don't you try looking at something to hold the image like a Canvas. And then it would be easier for me to help you.
james91a at 2007-7-8 10:35:13 > top of Java-index,Administration Tools,Sun Connection...
# 7
ImageIO.write(image, "jpg", new File(outputDirectory, "temp"+counter+".jpg"));should work
Jochema at 2007-7-8 10:35:13 > top of Java-index,Administration Tools,Sun Connection...