如何将流式数据直接加载到BufferedImage中 [英] How to load streamed data directly into a BufferedImage

查看:130
本文介绍了如何将流式数据直接加载到BufferedImage中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此已接受的答案,用于通过Java中的套接字发送文件列表。我的目标是接收图像列表。我想做的就是将这些图像作为 BufferedImages 读入内存,然后再将其写入磁盘。但是,我的第一个尝试是使用 ImageIO.read(bis)(同样,请参见附件的问题)失败,因为它尝试继续读取末尾的内容。第一个图像文件。

I am using the code provided by this accepted answer to send a list of files over a socket in Java. My goal is to be receiving a list of images. What I would like to do is read these images directly into memory as BufferedImages before writing them to disk. However, my first attempts, which was to use ImageIO.read(bis) (again, see the attached question) failed, as it attempted to continue reading beyond the end of the first image file.

我目前的想法是将套接字中的数据写入新的输出流,然后从传递给<$的输入流中读取该流。 c $ c> ImageIO.read()。这样,我可以像程序当前所做的那样逐字节写入它,但是将其发送到 BufferedImage 而不是文件。但是我不确定如何将输出流链接到输入流。

My current idea is to write the data from the socket to a new output stream, then read that stream from an intput stream that is passed to ImageIO.read(). This way, I can write it byte by byte as the program is currently doing, but send it to the BufferedImage rather than the file. however I'm not sure how to link the output stream to an input stream.

有人可以建议对上面的代码进行简单的编辑,或者提供另一种方法吗?

Can anyone recommend simple edits to the code above, or provide another method of doing this?

推荐答案

为了在将图像写入磁盘之前先读取图像,您需要使用ByteArrayInputStream。 http://docs.oracle.com/javase/6 /docs/api/java/io/ByteArrayInputStream.html

In order to read the image before writing it to disk, you'll need to use a ByteArrayInputStream. http://docs.oracle.com/javase/6/docs/api/java/io/ByteArrayInputStream.html

基本上,它会创建一个从指定字节数组读取的输入流。因此,您将读取图像的长度,然后是名称,然后是字节的长度,创建ByteArrayInputStream,并将其传递给ImageIO。read

Basically, it creates a inputstream that reads from a specified byte array. So, you'd read the image length, then it's name, then the length-amount of bytes, create the ByteArrayInputStream, and pass it to ImageIO.read

示例代码段:

long fileLength = dis.readLong();
String fileName = dis.readUTF();
byte[] bytes = new byte[fileLength];
dis.readFully(bytes);
BufferedImage bimage = ImageIO.read(new ByteArrayInputStream(bytes));

或使用您引用的其他答案中的代码:

Or using the code from the other answer you cited:

String dirPath = ...;

ServerSocket serverSocket = ...;
Socket socket = serverSocket.accept();

BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
DataInputStream dis = new DataInputStream(bis);

int filesCount = dis.readInt();
File[] files = new File[filesCount];

for(int i = 0; i < filesCount; i++)
{
    long fileLength = dis.readLong();
    String fileName = dis.readUTF();
    byte[] bytes = new byte[fileLength];
    dis.readFully(bytes);
    BufferedImage bimage = ImageIO.read(new ByteArrayInputStream(bytes));

    //do some shit with your bufferedimage or whatever

    files[i] = new File(dirPath + "/" + fileName);

    FileOutputStream fos = new FileOutputStream(files[i]);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    bos.write(bytes, 0, fileLength);

    bos.close();
}

dis.close();

这篇关于如何将流式数据直接加载到BufferedImage中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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