以字节数组形式从JPEG获取元数据

时间:2013-02-09 07:39:10

标签: java image metadata byte jpeg

我有一个字节数组形式的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);          
}

1 个答案:

答案 0 :(得分:4)

您似乎(正好)询问如何创建从字节数组中读取的ImageInputStream。从阅读javadocs,我认为这应该工作:

new MemoryCacheImageInputStream(new ByteArrayInputStream(myimagedata))

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

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