Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why there's a little delay after I save JPGs using ImageWriter

Reply
Thread Tools

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

 
 
Roedy Green
Guest
Posts: n/a
 
      05-19-2008
On Mon, 19 May 2008 05:55:27 -0700 (PDT), ZelluX <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>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?
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
ZelluX
Guest
Posts: n/a
 
      05-20-2008
On May 19, 11:38 pm, Tom Anderson <(E-Mail Removed)> wrote:
> On Mon, 19 May 2008, ZelluX wrote:
> > 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
>
> --
> It is better to create badly than to appreciate well. -- Gareth Jones


It works

Thanks for all the replies ^_^
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plug-in ImageWriter Jpeg with JMagick macsw Java 0 06-06-2008 04:24 PM
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Calculating propagation delay & transmission delay Stone Cisco 1 09-27-2006 06:26 PM
Why getting Corrupted jpgs on my Compact flash CARD? lbbss Digital Photography 10 09-16-2004 11:01 PM



Advertisments