adams.flow.transformer.exiftagoperation.ApacheCommonsExifTagExists.java Source code

Java tutorial

Introduction

Here is the source code for adams.flow.transformer.exiftagoperation.ApacheCommonsExifTagExists.java

Source

/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * ApacheCommonsExifTagRead.java
 * Copyright (C) 2019 University of Waikato, Hamilton, NZ
 */

package adams.flow.transformer.exiftagoperation;

import adams.core.License;
import adams.core.MessageCollection;
import adams.core.Utils;
import adams.core.annotation.MixedCopyright;
import adams.core.io.PlaceholderFile;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoAscii;
import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoByte;
import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoDouble;
import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoFloat;
import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoRational;
import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoShort;

import java.io.File;

/**
 * Checks whether the specified tag is present in the the file.
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 */
@MixedCopyright(author = "yurko - https://stackoverflow.com/users/418516/yurko", url = "https://stackoverflow.com/a/36873897/4698227", license = License.CC_BY_SA_3, note = "general usage of Apache Commons Imaging for EXIF operations")
public class ApacheCommonsExifTagExists extends AbstractApacheCommonsExifTagOperation<Object, Boolean>
        implements ExifTagExistsOperation<Object> {

    private static final long serialVersionUID = -4257460091938302125L;

    /**
     * Returns a string describing the object.
     *
     * @return          a description suitable for displaying in the gui
     */
    @Override
    public String globalInfo() {
        return "Checks whether the specified tag is present in the the file.";
    }

    /**
     * Returns the type of data that we can process.
     *
     * @return      the type of data
     */
    @Override
    public Class[] accepts() {
        return new Class[] { String.class, File.class };
    }

    /**
     * Returns the type of data that we generate.
     *
     * @return      the type of data
     */
    @Override
    public Class[] generates() {
        return new Class[] { Boolean.class };
    }

    /**
     * Hook method for performing checks before processing the data.
     *
     * @param input   the input to process
     * @return      null if successful, otherwise error message
     */
    @Override
    protected String check(Object input) {
        String result;

        result = super.check(input);

        if (result == null) {
            if (!(m_Tag.getTagInfo() instanceof TagInfoAscii) && !(m_Tag.getTagInfo() instanceof TagInfoByte)
                    && !(m_Tag.getTagInfo() instanceof TagInfoShort)
                    && !(m_Tag.getTagInfo() instanceof TagInfoDouble)
                    && !(m_Tag.getTagInfo() instanceof TagInfoFloat)
                    && !(m_Tag.getTagInfo() instanceof TagInfoRational))
                result = "Unhandled tag info type: " + Utils.classToString(m_Tag.getTagInfo());
        }

        return result;
    }

    /**
     * Processes the incoming data.
     *
     * @param input   the input to process
     * @param errors   for storing errors
     * @return      the generated output
     */
    @Override
    protected Boolean doProcess(Object input, MessageCollection errors) {
        Boolean result;
        File inputFile;
        JpegImageMetadata meta;
        TiffImageMetadata exif;

        result = false;

        if (input instanceof String)
            inputFile = new PlaceholderFile((String) input).getAbsoluteFile();
        else
            inputFile = ((File) input).getAbsoluteFile();

        try {
            meta = (JpegImageMetadata) Imaging.getMetadata(inputFile);
            if (meta != null) {
                exif = meta.getExif();
                if (exif != null) {
                    result = (exif.getFieldValue(m_Tag.getTagInfo()) != null);
                } else {
                    errors.add("No EXIF meta-data available: " + input);
                }
            } else {
                errors.add("No meta-data available: " + input);
            }
        } catch (Exception e) {
            errors.add("Failed to read EXIF tag " + m_Tag + " from: " + input, e);
        }

        return result;
    }
}