1

我有一个字节数组形式的 jpeg 图像。如何以可以剥离元数据的注释节点的形式获取字节数组?

byte[] myimagedata = ...

ImageWriter writer = ImageIO.getImageWritersBySuffix("jpeg").next();
ImageReader reader = ImageIO.getImageReader(writer);

//Looking for file here but have byte array
reader.setInput(new FileImageInputStream(new File(Byte array cant go here)));

IIOMetadata imageMetadata = reader.getImageMetadata(0);
Element tree = (Element)   imageMetadata.getAsTree("javax_imageio_jpeg_image_1.0");
NodeList comNL = tree.getElementsByTagName("com");
IIOMetadataNode comNode;
if (comNL.getLength() == 0) {
    comNode = new IIOMetadataNode("com");
    Node markerSequenceNode = tree.getElementsByTagName("markerSequence").item(0);
    markerSequenceNode.insertBefore(comNode,markerSequenceNode.getFirstChild());
} else {
    comNode = (IIOMetadataNode) comNL.item(0);          
}
4

1 回答 1

4

您似乎(只是)询问如何创建ImageInputStream从字节数组读取的。通过阅读 javadocs,我认为这应该有效:

new MemoryCacheImageInputStream(new ByteArrayInputStream(myimagedata))

该类FileImageInputStream没有允许您从文件系统中的文件以外的任何内容读取的构造函数。

FileCacheImageInputStream也是一种选择,但它涉及在文件系统中提供一个目录以用于临时缓存......在这种情况下这似乎是不可取的。

于 2013-02-09T09:43:06.737 回答