This discussion is archived
4 Replies Latest reply: Nov 10, 2011 11:10 PM by 896874 RSS

IIOMetadataNode Is there an easy way to obtain all elements?

896874 Newbie
Currently Being Moderated
Is there an easy way to obtain all elements with no recursion to one level depth in the IIOMetadataNode XML document from getAsTree("formatname") and do the jpeg type images that have EXIF meta data(when its present on the image) return that in the IIOMetadataNode ?
  • 1. Re: IIOMetadataNode Is there an easy way to obtain all elements?
    Maxideon Explorer
    Currently Being Moderated
    You can call getChildNodes() on the Node returned from getAsTree(). The returned NodeList will contain the one level depth children.

    The EXIF data will be lumped together as a byte[] array under an "unkown" marker sequence with a marker value of 225.
    /**Returns the EXIF information from the given metadata if present.  The
     * metadata is assumed to be in <pre>javax_imageio_jpeg_image_1.0</pre> 
     * format. If EXIF information was not present then null is returned.*/
    public static byte[] getEXIF(IIOMetadata meta) {
        //javax_imageio_jpeg_image_1.0
        //-->markerSequence
        //---->unknown (attribute: "MarkerTag" val: 225 (for exif))
    
        IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree(
                "javax_imageio_jpeg_image_1.0");
    
        IIOMetadataNode markerSeq = (IIOMetadataNode)
                root.getElementsByTagName("markerSequence").item(0);
    
        NodeList unkowns = markerSeq.getElementsByTagName("unknown");
        for(int i = 0; i < unkowns.getLength(); i++) {
            IIOMetadataNode marker = (IIOMetadataNode) unkowns.item(i);
            if("225".equals(marker.getAttribute("MarkerTag"))) {
                return (byte[]) marker.getUserObject();
            }
        }
        return null;
    }
    You need to use a TIFFImageReader to parse the byte[] array since EXIF metadata is TIFF metadata.
  • 2. Re: IIOMetadataNode Is there an easy way to obtain all elements?
    896874 Newbie
    Currently Being Moderated
    Thanks for that , i will see how looking through that is , i have a process i used in a PERL script to get all folders and files from an allocated top folder that i have been chunking away on to adapt(it's nowhere near as easy in Java), "i need to know if the Tiff reader commits un-compression upon the EXIF meta data"(seems like it) , i have a PDF on EXIF specification but it had no reference to whether the data was compressed or not only start block int and end block int where to find it. I've seen the API with the Tiff reader and the constant field values for each header allocated under the specification, its possible to get the start and end block but is it compressed?
  • 3. Re: IIOMetadataNode Is there an easy way to obtain all elements?
    Maxideon Explorer
    Currently Being Moderated
    The block is probably not compressed. There wouldn't be much gain in compressing metada in general.

    Try using the following.
    /**Uses a TIFFImageReader plugin to parse the given exif data into tiff
     * tags.  The returned IIOMetadata is in whatever format the tiff ImageIO
     * plugin uses.  If there is no tiff plugin, then this method returns null.*/
    public static IIOMetadata getTiffMetaFromEXIF(byte[] exif) {
        java.util.Iterator<ImageReader> readers =
                ImageIO.getImageReadersByFormatName("tif");
    
        ImageReader reader;
        if(!readers.hasNext()) {
            return null;
        }else {
            reader = readers.next();
        }
    
        //skip the 6 byte exif header
        ImageInputStream wrapper = new MemoryCacheImageInputStream(
                new java.io.ByteArrayInputStream(exif,6,exif.length-6));
        reader.setInput(wrapper,true,false);
    
        IIOMetadata exifMeta;
        try {
            exifMeta = reader.getImageMetadata(0);
        }catch(Exception e) {
            //shouldn't happen
            throw new Error(e);
        }
    
        reader.dispose();
        return exifMeta;
    }
  • 4. Re: IIOMetadataNode Is there an easy way to obtain all elements?
    896874 Newbie
    Currently Being Moderated
    I was more referring to using the EXIF spec (around version 3 i think it is now) to simply be able to remove the bytes required from the head of the file on disk by the spec byte markers values, but if its supposed to be there i can remove it with the java metadata objects but just as much to detect the camera or device info in EXIF the specs appear ok as much if i the data is not compressed.


    Heres how i've fixed something:
    The writer jams horribly if i don't have exactly the correct data but "image metadata is ambiguous to null" so its immensely difficult to get the writer to use null,null because there is no such thing as null image meta data unless all of it is explicitly ignored.
    IIOMetadataNode root = (IIOMetadataNode) imgIIOmtda.getAsTree("javax_imageio_jpeg_image_1.0");
    org.w3c.dom.NodeList markerSeq = root.getElementsByTagName("markerSequence");
    org.w3c.dom.NodeList unkowns = ((IIOMetadataNode)markerSeq.item(0)).getElementsByTagName("unknown");
    if(unkowns.getLength()>0){
    for(int i=0;i<unkowns.getLength();i++){
    IIOMetadataNode marker = (IIOMetadataNode) unkowns.item(i);
    if(("225".equals(marker.getAttribute("MarkerTag")))==false){
    //return (byte[]) marker.getUserObject();
    i=unkowns.getLength();
    //JOptionPane.showMessageDialog(new JFrame(),""+imgIIOmtda.toString());
    setMkImg(new javax.imageio.IIOImage(((RenderedImage)bumi),null,null));
    }else{
    //
    if(imgThmbNl!=null){
    //JOptionPane.showMessageDialog(new JFrame(),":this is the thumbs and,imgmetadata version");
    i=unkowns.getLength();
    setMkImg(new javax.imageio.IIOImage(((RenderedImage)bumi),imgThmbNl,imgIIOmtda));
    }else if((imgThmbNl==null) && (imgIIOmtda!=null)){
    //JOptionPane.showMessageDialog(new JFrame(),":this is the null,imgmetadata version");
    i=unkowns.getLength();
    setMkImg(new javax.imageio.IIOImage(((RenderedImage)bumi),null,imgIIOmtda));
    }else{}
    //
    }
    }//enfr
    }else{
    //imgIIOmtda=null;
    //JOptionPane.showMessageDialog(new JFrame(),""+imgIIOmtda.toString());
    setMkImg(new javax.imageio.IIOImage(((RenderedImage)bumi),null,null));
    }
    
    /*
    //JOptionPane.showMessageDialog(new JFrame(),""+imgIIOmtda.toString());
    //iowrtImg=new javax.imageio.IIOImage(((RenderedImage)bumi),imgThmbNl,imgIIOmtda);
    if(imgThmbNl!=null){
    //JOptionPane.showMessageDialog(new JFrame(),":this is the thumbs and,imgmetadata version");
    setMkImg(new javax.imageio.IIOImage(((RenderedImage)bumi),imgThmbNl,imgIIOmtda));
    }else if((imgThmbNl==null) && (imgIIOmtda!=null)){
    //JOptionPane.showMessageDialog(new JFrame(),":this is the null,imgmetadata version");
    setMkImg(new javax.imageio.IIOImage(((RenderedImage)bumi),null,imgIIOmtda));
    }else{
    //JOptionPane.showMessageDialog(new JFrame(),":this is the null,null version");
    JOptionPane.showMessageDialog(new JFrame(),""+imgIIOmtda.toString());
    setMkImg(new javax.imageio.IIOImage(((RenderedImage)bumi),null,null));
    }
    //iowrtImg FOR JPEG
    */
    Edited by: 893871 on Nov 10, 2011 11:08 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points