是否可以使用Java ImageIO从InputStream读取多个图像?
作者:互联网
我正在尝试一个Kotlin线程,该线程仅从单个InputStream读取多个图像.
为了进行测试,我有一个输入流,该输入流在单独的线程中接收两个小图像文件的内容.这似乎工作正常,就像我将输入流的内容写入磁盘一样,生成的文件与两个源映像文件的串联相同.
使用ImageIO从输入流中读取图像时,会发生此问题:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import javax.imageio.ImageIO;
class ImgReader {
InputStream input;
ImgReader(InputStream input) {
this.input = input;
}
public void run() {
ImageIO.setUseCache(false);
System.out.println("read start");
int counter = 1;
try {
BufferedImage im = ImageIO.read(input);
System.out.println("read: " + counter + " " + (im != null));
if (im != null)
ImageIO.write(im, "jpg", new File("pics/out/" + (counter++) +".jpeg"));
} catch (Exception e){
System.out.println("error while reading stream");
e.printStackTrace(System.out);
}
System.out.println("read done");
}
}
这适用于第一个图像,该图像已正确接收并保存到文件中.但是,不会读取第二个图像:ImageIO.read(input)返回null.
是否可以从InputStream读取多个图像?我究竟做错了什么?
-编辑-
我尝试了一种变体,其中仅从流中解码了一个图像(此操作正确完成).此后,我尝试将其余的流内容保存到二进制文件中,而没有尝试将其解码为图像.第二个二进制文件为空,这意味着第一个ImageIO.read似乎消耗了整个流.
解决方法:
是的,可以从(单个)InputStream读取多个图像.
我认为最明显的解决方案是使用一种文件格式,该文件格式已广泛支持多种图像,例如TIFF.即使ImageIO类没有任何便利的方法,如ImageIO.read(…)/ ImageIO.write(…),javax.imageio API也对读取和写入多图像文件提供了良好的支持. )读取/写入单个图像的方法.这意味着您需要编写更多代码(下面的代码示例).
但是,如果输入是由控件之外的第三方创建的,则不能选择使用其他格式.从注释中可以看出,您的输入实际上是串联的Exif JPEG流.好消息是,Java的JPEGImageReader / Writer确实允许在同一流中包含多个JPEG,即使这种格式不是很常见.
要从同一流中读取多个JPEG,可以使用以下示例(请注意,该代码是完全通用的,并且可以读取其他多图像文件,例如TIFF):
File file = ...; // May also use InputStream here
List<BufferedImage> images = new ArrayList<>();
try (ImageInputStream in = ImageIO.createImageInputStream(file)) {
Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
if (!readers.hasNext()) {
throw new AssertionError("No reader for file " + file);
}
ImageReader reader = readers.next();
reader.setInput(in);
// It's possible to use reader.getNumImages(true) and a for-loop here.
// However, for many formats, it is more efficient to just read until there's no more images in the stream.
try {
int i = 0;
while (true) {
images.add(reader.read(i++));
}
}
catch (IndexOutOfBoundsException expected) {
// We're done
}
reader.dispose();
}
此行以下的任何内容都只是额外的信息.
这是使用ImageIO API编写多图像文件的方法(代码示例使用TIFF,但是它非常通用,并且理论上也应适用于其他格式,但压缩类型参数除外).
File file = ...; // May also use OutputStream/InputStream here
List<BufferedImage> images = new ArrayList<>(); // Just add images...
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("TIFF");
if (!writers.hasNext()) {
throw new AssertionError("Missing plugin");
}
ImageWriter writer = writers.next();
if (!writer.canWriteSequence()) {
throw new AssertionError("Plugin doesn't support multi page file");
}
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("JPEG"); // The allowed compression types may vary from plugin to plugin
// The most common values for TIFF, are NONE, LZW, Deflate or Zip, or JPEG
try (ImageOutputStream out = ImageIO.createImageOutputStream(file)) {
writer.setOutput(out);
writer.prepareWriteSequence(null); // No stream metadata needed for TIFF
for (BufferedImage image : images) {
writer.writeToSequence(new IIOImage(image, null, null), param);
}
writer.endWriteSequence();
}
writer.dispose();
请注意,在Java 9之前,您还需要第三方TIFF插件(例如JAI或我自己的TwelveMonkeys ImageIO)才能使用ImageIO读取/写入TIFF.
如果您真的不喜欢编写此冗长的代码,另一种选择是将图像包装成您自己的最小容器格式,该格式至少(包括)每个图像的长度.然后,您可以使用ImageIO.write(…)进行写入,也可以使用ImageIO.read(…)进行读取,但是您需要围绕它实现一些简单的流逻辑.当然,反对它的主要论据是它将是完全专有的.
但是,如果您要在类似客户端/服务器的设置中异步读取/写入(就像我怀疑的那样,从我的问题来看),这可能是很合理的,并且可能是可以接受的折衷方案.
就像是:
File file = new File(args[0]);
List<BufferedImage> images = new ArrayList<>();
try (DataOutputStream out = new DataOutputStream(new FileOutputStream(file))) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024 * 1024); // Use larger buffer for large images
for (BufferedImage image : images) {
buffer.reset();
ImageIO.write(image, "JPEG", buffer); // Or PNG or any other format you like, really
out.writeInt(buffer.size());
buffer.writeTo(out);
out.flush();
}
out.writeInt(-1); // EOF marker (alternatively, catch EOFException while reading)
}
// And, reading back:
try (DataInputStream in = new DataInputStream(new FileInputStream(file))) {
int size;
while ((size = in.readInt()) != -1) {
byte[] buffer = new byte[size];
in.readFully(buffer); // May be more efficient to create a FilterInputStream that counts bytes read, with local EOF after size
images.add(ImageIO.read(new ByteArrayInputStream(buffer)));
}
}
PS:如果您要做的只是将收到的图像写入磁盘,则不应使用ImageIO.而是使用普通的I / O(假定来自上一个示例的格式):
try (DataInputStream in = new DataInputStream(new FileInputStream(file))) {
int counter = 0;
int size;
while ((size = in.readInt()) != -1) {
byte[] buffer = new byte[size];
in.readFully(buffer);
try (FileOutputStream out = new FileOutputStream(new File("pics/out/" + (counter++) +".jpeg"))) {
out.write(buffer);
out.flush();
}
}
}
标签:javax-imageio,kotlin,java 来源: https://codeday.me/bug/20191024/1924113.html