如何序列化包含 BufferedImages 的对象 [英] How to serialize an object that includes BufferedImages

查看:41
本文介绍了如何序列化包含 BufferedImages 的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Java 创建一个简单的图像编辑程序.我创建了一个 ImageCanvas 对象,其中包含有关正在编辑的图像的所有信息(一些基本属性、应用的效果列表、BufferedImage 图层列表等.) 并且我想要一种简单的方法将它保存到磁盘,以便以后可以再次打开它.

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.

我认为使用 Java 的默认 Serializable 接口可能正是我正在寻找的,我可以将整个对象写入文件并在稍后再次将其读回内存.但是,ImageCanvas 包含一个 ArrayList,并且 BufferedImage 是不可序列化的(其他都是).

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).

我知道可以覆盖 writeObject()readObject() 方法,但我从未这样做过,我想知道是否有任何简单的方法让 Java 序列化其他所有内容并有一些自定义方式来读取/写入 BufferedImage 到磁盘?或者是否有其他方法可以轻松地将整个 ImageCanvas 对象写入我忽略的磁盘?最终我可能会实现我自己的自定义图像文件类型,但现在我想要一种快速简便的方法来在我测试时临时保存文件(ImageCanvas 类会发生很大变化,所以我没有"我不想在完成之前不断更新我的自定义文件类型).

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).

推荐答案

使您的 ArrayList 成为瞬态,并实现自定义的 writeObject() 方法.在此,为您的 ImageCanvas 写入常规数据,然后使用 PNG 格式手动写出图像的字节数据.

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));
        }
    }
}

这篇关于如何序列化包含 BufferedImages 的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆