I am using the following code to serialize and deserialize my object, that contains BufferedImage
field.
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
if (bufferedImageField != null) {
out.writeBoolean(true);
ImageIO.write(bufferedImageField, "png", out);
}
else
out.writeBoolean(false);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if (in.readBoolean())
bufferedImageField= ImageIO.read(in);
else
bufferedImageField = null;
}
But the problem is that image is being compressed and decompressed, so hashCode()
of the object changes after deserialization. Also compression and decompression takes a lot of time.
How can I serialize the image without compression and deserialize it precisely?