Java:如何以通用方式将元信息添加到多个图像类型

时间:2012-09-29 15:28:43

标签: java image-processing metadata javax.imageio jai

需要做什么

我需要将自定义元信息添加到图像文件
支持的类型需要是:JPEG,GIF,BMP,TIF,PNG

如何完成

我决定使用imageio Standard metadata来为所有文件类型提供通用解决方案。
我也使用Java高级成像库,因为并非所有文件类型都支持普通的api。

以下是添加元数据的核心功能的代码片段:

    public static byte[] writeCustomMetaInformation(byte[] imageByteArray, String contentType) throws IOException {
    ImageWriter writer = null;

    String formatName = "javax_imageio_1.0";

    ImageOutputStream stream = null;
    if (contentType.toLowerCase().contains("gif")) {
        IIORegistry registry = IIORegistry.getDefaultInstance();
        registry.registerServiceProvider(new GIFImageWriterSpi());
        writer = ImageIO.getImageWritersByFormatName("gif").next();

    } else if (contentType.toLowerCase().contains("png")) {
        writer = ImageIO.getImageWritersByFormatName("png").next();

    } else if (contentType.toLowerCase().contains("bmp")) {
        IIORegistry registry = IIORegistry.getDefaultInstance();
        registry.registerServiceProvider(new BMPImageWriterSpi());
        writer = ImageIO.getImageWritersByFormatName("bmp").next();

    } else if (contentType.toLowerCase().contains("tif")) {
        IIORegistry registry = IIORegistry.getDefaultInstance();
        registry.registerServiceProvider(new TIFFImageWriterSpi());
        writer = ImageIO.getImageWritersByFormatName("tif").next();

    } else { //by default jpeg
        writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    }

    BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageByteArray));

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

    /*
     * Add metadata to the file
     * See http://docs.oracle.com/javase/6/docs/api/javax/imageio/metadata/doc-files/standard_metadata.html
     * for reference on default image metadata DTD
     */
    IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);

    IIOMetadataNode root = new IIOMetadataNode(formatName);

    //Add RGB tag, otherwise BMP writing throws NPE
    IIOMetadataNode chroma = new IIOMetadataNode("Chroma");
    IIOMetadataNode colorSpaceType = new IIOMetadataNode("ColorSpaceType");
    colorSpaceType.setAttribute("name", "RGB");
    chroma.appendChild(colorSpaceType);
    root.appendChild(chroma);

    IIOMetadataNode text = new IIOMetadataNode("Text");
    IIOMetadataNode textEntry = new IIOMetadataNode("TextEntry");
    textEntry.setAttribute("keyword", "metainformation");
    textEntry.setAttribute("value", "This is some custom meta information!");
    textEntry.setAttribute("encoding", "UTF-8");
    textEntry.setAttribute("language", "EN");
    textEntry.setAttribute("compression", "none");
    text.appendChild(textEntry);

    root.appendChild(text);

    metadata.mergeTree(formatName, root);

    // writing the data
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    stream = ImageIO.createImageOutputStream(baos);

    writer.setOutput(stream);

    IIOImage img = new IIOImage(bufferedImage, null, metadata);
    writer.write(metadata, img, writeParam);
    stream.close();

    return baos.toByteArray();
}

结果是什么

我使用ImageMagick标识-verbose 来查看添加元信息的结果。

  1. JPG,PNG和GIF文件添加了元信息
  2. BMP和TIF要么没有添加元信息,要么ImageMagick无法读取它。
  3. 那么,有谁知道为什么添加到BMP和TIF的元信息似乎没有生效?标准元数据DTD不适用于它们吗?我应该使用BMP / TIF元数据吗?

    如果您想测试行为,请参阅completely set-up project链接,以演示转换及其问题。

0 个答案:

没有答案