Why there's a little delay after I save JPGs using ImageWriter

Z

ZelluX

Hi, all
I want to save the image on a JPanel to a JPG file, here is my code

ImageWriter iw = ImageIO.getImageWritersByFormatName("jpg").next();
BufferedImage bi = new BufferedImage(panel.getWidth(),
panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
panel.paintComponent(g);
ImageOutputStream ios = ImageIO.createImageOutputStream(new
FileOutputStream(path));
iw.setOutput(ios);
iw.write(bi);
g.dispose();
iw.dispose();
ios.close();

After this snippet of code finished, I clicked the created .jpg file,
but it showed nothing. After several seconds, I clicked again, and
this time the image turned up.
Can I diminish such delay?

Many thanks and sorry for my poor English
 
L

Leonard Milcin

ZelluX said:
Hi, all
I want to save the image on a JPanel to a JPG file, here is my code

ImageWriter iw = ImageIO.getImageWritersByFormatName("jpg").next();
BufferedImage bi = new BufferedImage(panel.getWidth(),
panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
panel.paintComponent(g);
ImageOutputStream ios = ImageIO.createImageOutputStream(new
FileOutputStream(path));
iw.setOutput(ios);
iw.write(bi);
g.dispose();
iw.dispose();
ios.close();

After this snippet of code finished, I clicked the created .jpg file,
but it showed nothing. After several seconds, I clicked again, and
this time the image turned up.
Can I diminish such delay?

Many thanks and sorry for my poor English

Have you tried ios.flush() before ios.close()?

Regards,
Leonard
 
A

Andrew Thompson

Hi, all
I want to save the image on a JPanel to a JPG file, here is my code

'code' might mean many things, I would refer to that
as a 'code snippet'. I prefer an SSCCE*.
ImageWriter iw = ImageIO.getImageWritersByFormatName("jpg").next();
BufferedImage bi = new BufferedImage(panel.getWidth(),
panel.getHeight(), BufferedImage.TYPE_INT_RGB);

Note that 'from here' I cannot tell if panel.getWidth()
/getHeight() will return 40x20 or 4000x2000 (the latter
of which might take 10000 times longer to process).

Care to elucidate by providing a *complete* code
example? The best way to express that complete
example is in an SSCCE*.

* hhtp:/sscce.org/
Many thanks and sorry for my poor English

You english is fine (sorry for my entire lack
of understanding your native tongue), it is your
code snippets I am having a problem understanding.
 
Z

ZelluX

'code' might mean many things, I would refer to that
as a 'code snippet'. I prefer an SSCCE*.

Thanks for pointing out that ;-)
Note that 'from here' I cannot tell if panel.getWidth()
/getHeight() will return 40x20 or 4000x2000 (the latter
of which might take 10000 times longer to process).

The image created only takes up 12KB, and panel is a JPanel instance
with size of 500*800

I've tried to add
System.out.println("The file has been closed.");
after the close operation. When the words appeared i opened the jpg
file, but still nothing shown.

But strangely, the jpg file can be opened immediately if i close the
application after the save action straightway.
 
Z

ZelluX

'code' might mean many things, I would refer to that
as a 'code snippet'. I prefer an SSCCE*.

Thanks for pointing out that ;-)
Note that 'from here' I cannot tell if panel.getWidth()
/getHeight() will return 40x20 or 4000x2000 (the latter
of which might take 10000 times longer to process).

The image created only takes up 12KB, and panel is a JPanel instance
with size of 500*800

I've tried to add
System.out.println("The file has been closed.");
after the close operation. When the words appeared i opened the jpg
file, but still nothing shown.

But strangely, the jpg file can be opened immediately if i close the
application after the save action straightway.
 
T

Tom Anderson

I want to save the image on a JPanel to a JPG file, here is my code

ImageWriter iw = ImageIO.getImageWritersByFormatName("jpg").next();
BufferedImage bi = new BufferedImage(panel.getWidth(),
panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
panel.paintComponent(g);
ImageOutputStream ios = ImageIO.createImageOutputStream(new
FileOutputStream(path));
iw.setOutput(ios);
iw.write(bi);
g.dispose();
iw.dispose();
ios.close();

After this snippet of code finished, I clicked the created .jpg file,
but it showed nothing. After several seconds, I clicked again, and
this time the image turned up.
Can I diminish such delay?

Your ImageOutputStream is writing to a FileOutputStream, which is in turn
writing to disk. My guess would be that the ImageOutputStream is not
flushing the FileOutputStream when you close() it, and so data is staying
in a buffer until the FileOutputStream gets garbage collected and its
finalizer runs. Try this:

OutputStream fout = new FileOutputStream(path) ; // keep a reference to the FileOutputStream
ImageOutputStream ios = ImageIO.createImageOutputStream(fout) ;
iw.setOutput(ios) ;
iw.write(bi) ;
g.dispose() ;
iw.dispose() ;
ios.close() ;
fout.close() ; // explicitly close the FileOutputStream

Alternatively, an ios.flush() might do the job.
Many thanks and sorry for my poor English

Your english is fine!

tom
 
R

Roedy Green

After this snippet of code finished, I clicked the created .jpg file,
but it showed nothing. After several seconds, I clicked again, and
this time the image turned up.
Can I diminish such delay?

If you are using windows, the file is there, but the Explorer window
does not necessarily update until you click "refresh".
 
R

Roedy Green

When the words appeared i opened the jpg
file, but still nothing shown.

what do you mean by that?

1. the directory list did not include the file.

2. you did a file open by name in some jpg viewer and the file was not
found.

3. you did a file open by name in some jpg viewer and the file was
found, but when you viewed it, all you saw was a white rectangle?
 
Z

ZelluX

Your ImageOutputStream is writing to a FileOutputStream, which is in turn
writing to disk. My guess would be that the ImageOutputStream is not
flushing the FileOutputStream when you close() it, and so data is staying
in a buffer until the FileOutputStream gets garbage collected and its
finalizer runs. Try this:

OutputStream fout = new FileOutputStream(path) ; // keep a reference to the FileOutputStream
ImageOutputStream ios = ImageIO.createImageOutputStream(fout) ;
iw.setOutput(ios) ;
iw.write(bi) ;
g.dispose() ;
iw.dispose() ;
ios.close() ;
fout.close() ; // explicitly close the FileOutputStream

Alternatively, an ios.flush() might do the job.


Your english is fine!

tom

It works ;-)

Thanks for all the replies ^_^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top