问题
I'm trying to serialize a BufferedImage
in Java, but running my program I received a NotSerializableException
.
Looking at the BufferedImage
class, I noticed that it does not implements Serializable
.
Why doesn't BufferedImage
implement Serializable
?
回答1:
I think you've just discovered a missing feature.
- Does it make sense to have
BufferedImage implements Serializable
? In my opinion it does. Especially if theBufferedImage
was not loaded from a file, but created and drawn upon. But even if it's from a file, who cares where the stuff comes from if I want to exchange it between VMs via RMI or similar? - Is there anything in
BufferedImage
that provides a strong technical reason againstBufferedImage implements Serializable
? I browsed the source code, and I don't think so.
I checked whether the bug database already contains an entry for that, and I couldn't find anything related. So, this is your chance to make your contribution and suggest a feature request via the bug database. http://bugs.java.com/bugdatabase/
As a workaround, you might want to look at the implementation of readObject()
and writeObject()
in class javax.swing.ImageIcon
. ImageIcon
is Serializable
. Maybe you can wrap the BufferedImage
in an ImageIcon
for your use case, or somehow otherwise provide the logic from ImageIcon.readObject()
/ ImageIcon.writeObject()
.
来源:https://stackoverflow.com/questions/28954091/why-isnt-java-awt-image-bufferedimage-serializable