Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

directly saving an image to a file?

 
Kyle Rayner
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is one way of doing it.



You can try it with

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic