Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Convert between AWT Image and byte array

 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I have an application which persists all data structures to an XML file. Some of the data objects have AWT Images as properties which should be saved to the XML file, too. I use JAXB for the marshalling and unmarshalling process. The binary data are marshalled to base64 encoded text in the XML file and the base64 data are unmarshalled to byte arrays in Java.

For this I need to convert between Images and byte arrays in both directions. I know there are many links on the internet and there are also discussion on JavaRanch but actually I couldn't find any good, working solution. What do you think would be the best idea to convert between Image and byte[]? Would you store the whole Image as jpg or png to XML or would it be a good plan to store only the raw image data? Perhaps some hints how to do this right?

Any help is welcome!

Marco
 
Sheriff
Posts: 21999
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.imageio.ImageIO has a method called write which takes a RenderedImage, format name and OutputStream. That could be a ByteArrayOutputStream.

Now if your Image isn't an instance of RenderedImage then you can create a BufferedImage, paint the original image on that and use the BufferedImage (since BufferedImage implements RenderedImage):
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, Rob!

Your solution works perfectly. I just didn't know how to get a BufferedImage from my Image object.

Marco
 
Rob Spoor
Sheriff
Posts: 21999
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first check would be of course whether or not your Image may already be a RenderedImage. If so, why create the BufferedImage?
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point! I'll include this check in my code!

Marco
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic