How to best serialize a java.awt.Image?

前端 未结 3 1286
梦谈多话
梦谈多话 2020-12-18 23:43

I have a Serializable object which is supposed to hold a java.awt.Image as its member. How should I go about serializing it? (Edited from a not so clear first version, s

相关标签:
3条回答
  • 2020-12-19 00:23

    None that I know of. I believe you need to write your own serializer for it to basically save out the width, height and pixel values... Or write it out to the stream as a PNG or something

    0 讨论(0)
  • 2020-12-19 00:26

    javax.swing.ImageIcon, as a part of Swing, does not guarantee to have compatible serialised form between versions. However, you can cheat and look at its readObject and writeObject code - find width and height, grab the pixels with PixelGrabber. I'm not entirely sure that covers the colour model correctly. The obvious alternative is to write a real image format with javax.imageio.

    0 讨论(0)
  • 2020-12-19 00:40

    ImageIcon implements Serializable and it can be used to wrap an Image class

    http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/ImageIcon.html

    0 讨论(0)
提交回复
热议问题