tud*_*lus 2 java bufferedimage file
正如标题所暗示的那样,我试图将一个包含(以及其他变量、字符串等)一些 BufferedImage 的对象保存到文件中。
我发现了这个: 如何序列化一个包含 BufferedImages 的对象
它就像一个魅力,但有一个小小的挫折:如果你的对象只包含一个图像,它会很好地工作。
我一直在努力让他的解决方案处理多个图像(理论上应该可行)但是每次我读入文件时,我都会取回我的对象,我得到正确数量的图像,但只有第一个图像实际上被读入;其他只是空图像,其中没有数据。
这是我的对象的样子:
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我在文件中写入和读取对象的方式:
// 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();}
Run Code Online (Sandbox Code Playgroud)
即使我有一个对象列表,它们也会被正确读取,并且列表中的每个元素都会相应地填充,但 BufferedImages 除外。
有没有人有办法解决这个问题?
问题很可能是ImageIO.read(...)在读取第一个图像后错误地定位了流。
我看到两个选项来解决这个问题:
重写BufferedImages的序列化以写入图像的支持数组、高度、宽度、颜色模型/颜色空间标识符以及重新创建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)));
}
}
Run Code Online (Sandbox Code Playgroud)