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

查看:1406
本文介绍了如何序列化包含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的defualt Serializable 界面可能正是我想要的,我可以写将整个对象归档并在以后再次将其读回内存。但是, ImageCanvas 包含 ArrayList< BufferedImage> 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).

推荐答案

make您的 ArrayList< BufferedImage> 瞬态,并实现自定义 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天全站免登陆