Java 序列化包含BuffereImage的对象
正如标题所示,我正试图保存一个对象,该对象包含(除其他变量、字符串等外)几个BuffereImage 我发现: 它就像一个符咒,但有一个小小的挫折:如果你的物体只包含一个图像,它会很好地工作 我一直在努力使他的解决方案能够处理多个图像(理论上应该可以),但每次我读入文件时,我都会取回我的对象,得到正确数量的图像,但实际上只有第一个图像被读入;其他的只是空图像,其中没有数据 这是我的对象的外观:Java 序列化包含BuffereImage的对象,java,file,bufferedimage,Java,File,Bufferedimage,正如标题所示,我正试图保存一个对象,该对象包含(除其他变量、字符串等外)几个BuffereImage 我发现: 它就像一个符咒,但有一个小小的挫折:如果你的物体只包含一个图像,它会很好地工作 我一直在努力使他的解决方案能够处理多个图像(理论上应该可以),但每次我读入文件时,我都会取回我的对象,得到正确数量的图像,但实际上只有第一个图像被读入;其他的只是空图像,其中没有数据 这是我的对象的外观: class Obj implements Serializable { transient
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));
}
}
}
类Obj实现可序列化
{
瞬态列表imageSelection=新建ArrayList();
//…其他变量和函数
私有void writeObject(ObjectOutputStream out)引发IOException{
out.defaultWriteObject();
out.writeInt(imageSelection.size());//序列化了多少个图像?
用于(BuffereImage每个图像:图像选择){
write(每个图像,“jpg”,out);//png是无损的
}
}
私有void readObject(ObjectInputStream in)引发IOException、ClassNotFoundException{
in.defaultReadObject();
final int imageCount=in.readInt();
imageSelection=新阵列列表(imageCount);
对于(int i=0;i而言,问题可能是ImageIO.read(…)
在第一次读取图像后错误地定位流
我认为有两种方法可以解决这个问题:
- 重写
buffereImage
s的序列化以写入备份数组重新创建BuffereImage
所需的图像、高度、宽度、颜色模型/颜色空间标识符和其他数据。这需要一些代码来正确处理各种图像,因此我现在跳过细节。可能更快更准确(但可能发送更多数据)
- 继续使用
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)));
}
}
private void writeObject(ObjectOutputStream out)抛出IOException{
out.defaultWriteObject();
out.writeInt(imageSelection.size());//序列化了多少个图像?
用于(BuffereImage每个图像:图像选择){
ByteArrayOutputStream缓冲区=新建ByteArrayOutputStream();
写入(每个图像,“jpg”,缓冲区);
out.writeInt(buffer.size());//在图像前面加上字节计数
buffer.writeTo(out);//写入映像
}
}
私有void readObject(ObjectInputStream in)引发IOException、ClassNotFoundException{
in.defaultReadObject();
int imageCount=in.readInt();
imageSelection=新阵列列表(imageCount);
对于(int i=0;i
你能调查一下吗?这可能是缓冲/刷新问题,刷新并关闭writeObject()中的“out”,只要检查是否是这样的情况你…是个天才!你立即解决了我的问题!非常感谢!我能为你做些什么吗?
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)));
}
}