如何最好地序列化 java.awt.Image?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/139996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to best serialize a java.awt.Image?
提问by Epaga
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, sorry.)
我有一个 Serializable 对象,它应该将 java.awt.Image 作为其成员。我应该如何序列化它? (从不太清楚的第一个版本中编辑,抱歉。)
回答by pfranza
ImageIcon implements Serializable and it can be used to wrap an Image class
ImageIcon 实现了 Serializable,它可以用来包装一个 Image 类
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/ImageIcon.html
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/ImageIcon.html
回答by Tom Hawtin - tackline
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.
javax.swing.ImageIcon 作为 Swing 的一部分,不保证版本之间具有兼容的序列化形式。但是,您可以欺骗并查看其 readObject 和 writeObject 代码 - 找到宽度和高度,使用 PixelGrabber 抓取像素。我不完全确定是否正确涵盖了颜色模型。显而易见的替代方法是使用 javax.imageio 编写真实的图像格式。
回答by tim_yates
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
我所知道的都没有。我相信您需要为它编写自己的序列化程序以基本上保存宽度、高度和像素值......或者将它作为 PNG 或其他东西写到流中

