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

Discussion in 'Java' started by ZelluX, May 19, 2008.

  1. ZelluX

    ZelluX Guest

    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
     
    ZelluX, May 19, 2008
    #1
    1. Advertising

  2. ZelluX wrote:
    > 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

    --
    Simplicity is the ultimate sophistication.
    -- Leonardo da Vinci
     
    Leonard Milcin, May 19, 2008
    #2
    1. Advertising

  3. ZelluX

    ZelluX Guest

    On May 19, 8:07 pm, Leonard Milcin <-spam.pl>
    wrote:
    > ZelluX wrote:
    > > 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
    >
    > --
    > Simplicity is the ultimate sophistication.
    > -- Leonardo da Vinci


    It doesn't help, still takes a little time (about 5~10 seconds)
     
    ZelluX, May 19, 2008
    #3
  4. On May 19, 9:45 pm, ZelluX <> wrote:
    > 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.

    --
    Andrew T.
    PhySci.org
     
    Andrew Thompson, May 19, 2008
    #4
  5. On May 19, 10:25 pm, Andrew Thompson <> wrote:

    >...The best way to express that complete
    > example is in an SSCCE*.
    >
    > * hhtp:/sscce.org/


    Oops! <http://sscce.org/>

    (Note to self - *copy/paste* URLs - especially
    when /intoxicated../)

    --
    A.
     
    Andrew Thompson, May 19, 2008
    #5
  6. ZelluX

    ZelluX Guest

    On May 19, 8:25 pm, Andrew Thompson <> wrote:
    > On May 19, 9:45 pm, ZelluX <> wrote:
    >
    > 'code' might mean many things, I would refer to that
    > as a 'code snippet'. I prefer an SSCCE*.


    Thanks for pointing out that ;-)

    >
    > > 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).


    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.
     
    ZelluX, May 19, 2008
    #6
  7. ZelluX

    ZelluX Guest

    On May 19, 8:25 pm, Andrew Thompson <> wrote:
    > On May 19, 9:45 pm, ZelluX <> wrote:
    >
    > 'code' might mean many things, I would refer to that
    > as a 'code snippet'. I prefer an SSCCE*.


    Thanks for pointing out that ;-)

    >
    > > 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).


    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.
     
    ZelluX, May 19, 2008
    #7
  8. On May 19, 10:54 pm, ZelluX <> wrote:
    > On May 19, 8:25 pm, Andrew Thompson <> wrote:
    >
    > > On May 19, 9:45 pm, ZelluX <> wrote:

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

    >
    > Thanks for pointing out that ;-)


    'Thanks for not providing one.'

    ..NEXT.

    --
    Andrew T.
    PhySci.org
     
    Andrew Thompson, May 19, 2008
    #8
  9. ZelluX

    Tom Anderson Guest

    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
     
    Tom Anderson, May 19, 2008
    #9
  10. ZelluX

    Roedy Green Guest

    On Mon, 19 May 2008 04:45:03 -0700 (PDT), ZelluX <>
    wrote, quoted or indirectly quoted someone who said :

    >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".
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, May 19, 2008
    #10
  11. ZelluX

    Roedy Green Guest

    On Mon, 19 May 2008 05:55:27 -0700 (PDT), ZelluX <>
    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
     
    Roedy Green, May 19, 2008
    #11
  12. ZelluX

    ZelluX Guest

    On May 19, 11:38 pm, Tom Anderson <> 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 ^_^
     
    ZelluX, May 20, 2008
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dorayme

    progressive jpgs

    dorayme, Jun 8, 2005, in forum: HTML
    Replies:
    5
    Views:
    473
    Travis Newbury
    Jun 8, 2005
  2. Kai Berger

    Read out pixels from .jpgs

    Kai Berger, Jul 1, 2004, in forum: C++
    Replies:
    2
    Views:
    400
    John Carson
    Jul 1, 2004
  3. Replies:
    2
    Views:
    299
    red floyd
    Dec 20, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,131
    Smokey Grindel
    Dec 2, 2006
  5. macsw
    Replies:
    0
    Views:
    295
    macsw
    Jun 6, 2008
Loading...

Share This Page