Java - 无法查看已修改的元数据

时间:2014-03-04 21:10:34

标签: java metadata

过去几个小时,我一直试图在Java中访问已修改的图像元数据。我知道我距离获得正确的输出还有一两步之遥。如果有人可以帮助我,我真的很感激。

我想在元数据中添加一个额外的字段,类似于以下文本: Writing image metadata in Java, preferably PNG

我的问题是,当我将自定义数据添加到我的图像中并且当我读取图像时,当我调用方法readAndDisplay时,我看不到修改后的元数据中的更改。我认为原因是我没有将图像正确保存到具有修改后的元数据的新文件中。有人可以看看我的代码中缺少的内容:

public class Metadata {

public static void main(String[] args) throws Exception {
     String imageFile = "134.png";
     BufferedImage img = null;

     try {
            img = ImageIO.read(new File(imageFile));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    Metadata meta = new Metadata();
    byte[] result = meta.writeCustomData(img, "decimalID", "211");
    BufferedImage output = ImageIO.read(new ByteArrayInputStream(result));
ImageIO.write(output, "png", new File("output.png"));
meta.readAndDisplayMetadata("output.png");
}

void readAndDisplayMetadata( String fileName ) {
    try {

        File file = new File( fileName );
        ImageInputStream iis = ImageIO.createImageInputStream(file);
        Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
        if (readers.hasNext()) {

            // pick the first available ImageReader
            ImageReader reader = readers.next();

            // attach source to the reader
            reader.setInput(iis, true);

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

            String[] names = metadata.getMetadataFormatNames();
            int length = names.length;
            System.out.println(length);
            for (int i = 0; i < length; i++) {
                System.out.println( "Format name: " + names[ i ] );
                displayMetadata(metadata.getAsTree(names[i]));
            }
        }
    }
    catch (Exception e) {

        e.printStackTrace();
    }
}

void readAndDisplayMetadata( byte[] image ) {
    try {

       // File file = new File( fileName );

        ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(image));
        Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
        if (readers.hasNext()) {

            // pick the first available ImageReader
            ImageReader reader = readers.next();

            // attach source to the reader
            reader.setInput(iis, true);

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

            String[] names = metadata.getMetadataFormatNames();
            int length = names.length;
            System.out.println(length);
            for (int i = 0; i < length; i++) {
                System.out.println( "Format name: " + names[ i ] );
                displayMetadata(metadata.getAsTree(names[i]));
            }
        }
    }
    catch (Exception e) {

        e.printStackTrace();
    }
}


void displayMetadata(Node root) {
    displayMetadata(root, 0);
}

void indent(int level) {
    for (int i = 0; i < level; i++)
        System.out.print("    ");
}

void displayMetadata(Node node, int level) {
    // print open tag of element
    indent(level);
    //System.out.println("Attributes: " + node.getAttributes().getLength());
    System.out.print("<" + node.getNodeName());
    NamedNodeMap map = node.getAttributes();
    if (map != null) {

        // print attribute values
        int length = map.getLength();

        for (int i = 0; i < length; i++) {
            Node attr = map.item(i);
            System.out.print(" " + attr.getNodeName() +
                             "=\"" + attr.getNodeValue() + "\"");
        }
    }

    Node child = node.getFirstChild();
    if (child == null) {
        // no children, so close element and return
        System.out.println("/>");
        return;
    }

    // children, so close current tag
    System.out.println(">");
    while (child != null) {
        // print children recursively
        displayMetadata(child, level + 1);
        child = child.getNextSibling();
    }

    // print close tag of element
    indent(level);
    System.out.println("</" + node.getNodeName() + ">");
}

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();
}

0 个答案:

没有答案