• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

directly saving an image to a file?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings. I already managed to acquire an image by using the BufferedImage class - and with some fiddling I found out how I can save it to a file, BUT...

...the thing is, I'd like the image to be saved without being recompressed so that the resulting image file is a 1:1 copy of what I started with.

If someone says "why not just copy it then" - I'm retrieving the image from the web, so I can't just copy it. (erh... or can I?)

Kyle
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is one way of doing it.



You can try it with

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Kyle", please take a look at our display name policy and then edit your display name to one less obviously fictitious (to some of us, at least.). Thank you.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't agree with all that Keith wrote here -- buffering both input and output, then providing additional buffers and copying over and over again, but take a look at the code for copying from one file to another, from the Java Almanac::(I think you could do better by closing the streams in a finally block.) Given that url.openStream() returns an InputStream, all that needs to be added is q.e.d.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you already have an instance of BufferedImage, you can save it directly with the static write() method of javax.imageio.ImageIO
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Ess:
If you already have an instance of BufferedImage, you can save it directly with the static write() method of javax.imageio.ImageIO



I think what the OP really wanted to do was copy from www.acme.com/coyote.gif to a local file xxx.gif. Converting the file back and forth to a BufferedImage is asking for trouble (apart from efficiency questions) -- what if you want to preserve the file's image format (woops, where's my gif writer?) and compression level, if any? Did you copy the file's metadata? Etc, etc...
reply
    Bookmark Topic Watch Topic
  • New Topic