Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize an object that includes BufferedImages

I'm trying to create a simple image editing program in java. I made an ImageCanvas object that has all the information about the image that is being edited (some basic properties, list of effects being applied, a list of BufferedImage layers, etc.) and I wanted a simple way to save it to disk so it could be opened again later.

I figured that using Java's defualt Serializable interface might be exactly what I was looking for and I could just write the entire object to file and read it back into memory again at a later time. However, ImageCanvas includes an ArrayList<BufferedImage>, and BufferedImage's are not serializable (everything else is).

I know it is possible to override the writeObject() and readObject() methods, but I have never done so and I was wondering if there is any easy way to have Java serialize everything else and have some custom way to read/write the BufferedImage's to disk? Or is there some other way to easily write the entire ImageCanvas object to disk that I'm overlooking? Eventually I might implement my own custom image file type, but for right now I wanted a quick and easy way to save files temporarily while I am testing (the ImageCanvas class will change a lot, so I didn't want to have to keep updating my custom file type before I have it finalized).

like image 356
scaevity Avatar asked Feb 25 '13 01:02

scaevity


People also ask

Which class contains method to serializing an object?

The ObjectOutputStream class contains writeObject() method for serializing an Object.

How do you serialize an object class in Java?

For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. We must have to implement the Serializable interface for serializing the object.

How do you serialize and deserialize an object?

The Serialization is a process of changing the state of an object into a byte stream, an object is said to be serializable if its class or parent classes implement either the Serializable or Externalizable interface and the Deserialization is a process of converting the serialized object back into a copy of an object.


1 Answers

make your ArrayList<BufferedImage> transient, and implement a custom writeObject() method. In this, write the regular data for your ImageCanvas, then manually write out the byte data for the images, using PNG format.

class ImageCanvas implements Serializable {
    transient List<BufferedImage> images;

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
        out.writeInt(images.size()); // how many images are serialized?
        for (BufferedImage eachImage : images) {
            ImageIO.write(eachImage, "png", out); // png is lossless
        }
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        final int imageCount = in.readInt();
        images = new ArrayList<BufferedImage>(imageCount);
        for (int i=0; i<imageCount; i++) {
            images.add(ImageIO.read(in));
        }
    }
}
like image 119
Sam Barnum Avatar answered Sep 19 '22 06:09

Sam Barnum