All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgrasstools.gears.io.exif.ExifReader Maven / Gradle / Ivy

/*
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.jgrasstools.gears.io.exif;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.FileImageInputStream;

import oms3.annotations.Author;
import oms3.annotations.Label;
import oms3.annotations.Description;
import oms3.annotations.Execute;
import oms3.annotations.In;
import oms3.annotations.Keywords;
import oms3.annotations.License;
import oms3.annotations.Out;
import oms3.annotations.Role;
import oms3.annotations.Status;

import org.jgrasstools.gears.libs.modules.JGTConstants;
import org.jgrasstools.gears.libs.modules.JGTModel;
import org.jgrasstools.gears.libs.monitor.LogProgressMonitor;
import org.jgrasstools.gears.libs.monitor.IJGTProgressMonitor;
import org.w3c.dom.NodeList;

@Description("Utility class for reading exif tags in jpegs.")
@Author(name = "Andrea Antonello", contact = "www.hydrologis.com")
@Keywords("IO, Jpeg, Exif, Reading")
@Label(JGTConstants.GENERICREADER)
@Status(Status.DRAFT)
@License("http://www.gnu.org/licenses/gpl-3.0.html")
public class ExifReader extends JGTModel {
    @Role(Role.PARAMETER)
    @Description("The jpeg file.")
    @In
    public String file = null;

    @Role(Role.PARAMETER)
    @Description("The progress monitor.")
    @In
    public IJGTProgressMonitor pm = new LogProgressMonitor();

    @Role(Role.PARAMETER)
    @Description("The read exif tags.")
    @Out
    public HashMap outTags = null;

    @Execute
    public void readExif() throws IOException {
        ImageReader reader = ExifUtil.findReader();
        reader.setInput(new FileImageInputStream(new File(file)));
        IIOMetadata imageMetadata = reader.getImageMetadata(0);

        parseExifMeta(imageMetadata);

    }

    @SuppressWarnings("nls")
    private void parseExifMeta( IIOMetadata exifMeta ) {
        outTags = new HashMap();

        IIOMetadataNode root = (IIOMetadataNode) exifMeta.getAsTree("com_sun_media_imageio_plugins_tiff_image_1.0");

        NodeList imageDirectories = root.getElementsByTagName("TIFFIFD");
        for( int i = 0; i < imageDirectories.getLength(); i++ ) {
            IIOMetadataNode directory = (IIOMetadataNode) imageDirectories.item(i);

            NodeList tiffTags = directory.getElementsByTagName("TIFFField");
            for( int j = 0; j < tiffTags.getLength(); j++ ) {
                IIOMetadataNode tag = (IIOMetadataNode) tiffTags.item(j);

                String tagNumber = tag.getAttribute("number");
                String tagName = tag.getAttribute("name");
                String tagValue;

                StringBuilder tmp = new StringBuilder();
                IIOMetadataNode values = (IIOMetadataNode) tag.getFirstChild();

                if ("TIFFUndefined".equals(values.getNodeName())) {
                    tmp.append(values.getAttribute("value"));
                } else {
                    NodeList tiffNumbers = values.getChildNodes();
                    for( int k = 0; k < tiffNumbers.getLength(); k++ ) {
                        tmp.append(((IIOMetadataNode) tiffNumbers.item(k)).getAttribute("value"));
                        tmp.append(",");
                    }
                    tmp.deleteCharAt(tmp.length() - 1);
                }

                tagValue = tmp.toString();

                ExifTag exifTag = new ExifTag(tagName, tagNumber, tagValue);
                outTags.put(tagName, exifTag);
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy