正如标题所示,我试图将一个包含(以及其他变量、字符串等)一些 BufferedImage 的对象保存到文件中。
我找到了这个:如何序列化包含 BufferedImages 的对象 https://stackoverflow.com/questions/15058663/how-to-serialize-an-object-that-includes-bufferedimages/32088694#32088694
它的工作方式就像一个魅力,但有一个小挫折:如果你的对象只包含一个图像,它会很好地工作。
我一直在努力让他的解决方案能够处理多个图像(理论上应该有效),但是每次我读入文件时,我都会找回我的对象,我会得到正确数量的图像,但只有第一个图像实际上被读入;其他的只是空图像,其中没有数据。
这就是我的对象的样子:
class Obj implements Serializable
{
transient List<BufferedImage> imageSelection= new ArrayList<BufferedImage>();
// ... other vars and functions
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(imageSelection.size()); // how many images are serialized?
for (BufferedImage eachImage : imageSelection) {
ImageIO.write(eachImage, "jpg", out); // png is lossless
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
final int imageCount = in.readInt();
imageSelection = new ArrayList<BufferedImage>(imageCount);
for (int i=0; i<imageCount; i++) {
imageSelection.add(ImageIO.read(in));
}
}
}
这就是我在文件中写入和读取对象的方式:
// writing
try (
FileOutputStream file = new FileOutputStream(objName+".ser");
ObjectOutputStream output = new ObjectOutputStream(file);
){
output.writeObject(myObjs);
}
catch(IOException ex){
ex.printStackTrace();
}
// reading
try(
FileInputStream inputStr = new FileInputStream(file.getAbsolutePath());
ObjectInputStream input = new ObjectInputStream (inputStr);
)
{myObjs = (List<Obj>)input.readObject();}
catch(Exception ex)
{ex.printStackTrace();}
即使我有一个对象列表,它们也会被正确读取,并且列表中的每个元素都会相应填充,BufferedImage 除外。
有人有办法解决这个问题吗?
问题很可能是ImageIO.read(...)
在读取第一个图像后错误地定位流。
我看到有两个选项可以解决这个问题:
重写序列化BufferedImage
写入图像的支持数组、高度、宽度、颜色模型/颜色空间标识符以及重新创建图像所需的其他数据BufferedImage
。这需要一些代码来正确处理各种图像,因此我现在将跳过详细信息。可能更快、更准确(但可能会发送更多数据)。
-
继续序列化使用ImageIO
,但使用缓冲每个写入ByteArrayOutputStream
,并在每个图像前面添加其字节数。回读时,首先读取字节数,并确保完全读取每个图像。这实现起来很简单,但由于文件格式的限制,某些图像可能会被转换或丢失细节(即 JPEG 压缩)。就像是:
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(imageSelection.size()); // how many images are serialized?
for (BufferedImage eachImage : imageSelection) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ImageIO.write(eachImage, "jpg", buffer);
out.writeInt(buffer.size()); // Prepend image with byte count
buffer.writeTo(out); // Write image
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
int imageCount = in.readInt();
imageSelection = new ArrayList<BufferedImage>(imageCount);
for (int i = 0; i < imageCount; i++) {
int size = in.readInt(); // Read byte count
byte[] buffer = new byte[size];
in.readFully(buffer); // Make sure you read all bytes of the image
imageSelection.add(ImageIO.read(new ByteArrayInputStream(buffer)));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)