用Java编写图像元数据,最好是PNG [英] Writing image metadata in Java, preferably PNG

查看:98
本文介绍了用Java编写图像元数据,最好是PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自stackoverflow的人们,

Hello people from stackoverflow,

我花了一些时间在谷歌搜索我的问题的解决方案,但没有什么好出来。

I've spent some time googling for a solution to my problem, but nothing great came out.

我想将元数据写入我从头开始创建的图像。

I would like to write metadata to an image that I create from scratch.

我对Java Advanced Image API的理解是我应该使用的IIOMetadata但我发现的代码片段似乎过于复杂。

My understanding of Java Advanced Image API is that I should use IIOMetadata but code snippets I found seemed to be overly complicated.

然后我搜索了一个库并找到了Sanselan,但它看起来有点旧,而且对于来说并不是很方便写元数据。

Then I searched a library and found Sanselan but it seems a bit old, and not very handy for writing metadata.

我实际使用
ImageIO.write(图片,png,baos)创建图像;

I actually create the image using ImageIO.write(image, "png", baos);

我理解图像元数据由于其类似XML的结构而有点复杂。

I understand image metadatas are a bit complex to handle due to its XML-like structure.

有人可以指点我一个教程,解决方案,图书馆......?

Could anybody point me to a tutorial, a solution, a library... ?

谢谢,
Alexis。

Thanks, Alexis.

推荐答案

前几天我不得不做同样的事情..我还没有找到互联网上的确切解决方案e但是看看 com.sun.imageio.plugins.png.PNGMetadata 类,我可以取得一些成果..

I had to do the the same thing some days ago.. I have not found the exact solution on the internet either but looking at the com.sun.imageio.plugins.png.PNGMetadata class I could achieve some results..

将自定义元数据写入PNG文件:

To write a custom metadata to a PNG file:

public byte[] writeCustomData(BufferedImage buffImg, String key, String value) throws Exception {
    ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();

    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    //adding metadata
    IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);

    IIOMetadataNode textEntry = new IIOMetadataNode("tEXtEntry");
    textEntry.setAttribute("keyword", key);
    textEntry.setAttribute("value", value);

    IIOMetadataNode text = new IIOMetadataNode("tEXt");
    text.appendChild(textEntry);

    IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");
    root.appendChild(text);

    metadata.mergeTree("javax_imageio_png_1.0", root);

    //writing the data
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream stream = ImageIO.createImageOutputStream(baos);
    writer.setOutput(stream);
    writer.write(metadata, new IIOImage(buffImg, null, metadata), writeParam);
    stream.close();

    return baos.toByteArray();
}

然后,要读取数据:

public String readCustomData(byte[] imageData, String key) throws IOException{
    ImageReader imageReader = ImageIO.getImageReadersByFormatName("png").next();

    imageReader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(imageData)), true);

    // read metadata of first image
    IIOMetadata metadata = imageReader.getImageMetadata(0);

    //this cast helps getting the contents
    PNGMetadata pngmeta = (PNGMetadata) metadata; 
    NodeList childNodes = pngmeta.getStandardTextNode().getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        String keyword = node.getAttributes().getNamedItem("keyword").getNodeValue();
        String value = node.getAttributes().getNamedItem("value").getNodeValue();
        if(key.equals(keyword)){
            return value;
        }
    }
    return null;
}

这篇关于用Java编写图像元数据,最好是PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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