Example usage for org.apache.commons.imaging.formats.tiff.constants ExifTagConstants EXIF_TAG_USER_COMMENT

List of usage examples for org.apache.commons.imaging.formats.tiff.constants ExifTagConstants EXIF_TAG_USER_COMMENT

Introduction

In this page you can find the example usage for org.apache.commons.imaging.formats.tiff.constants ExifTagConstants EXIF_TAG_USER_COMMENT.

Prototype

TagInfoGpsText EXIF_TAG_USER_COMMENT

To view the source code for org.apache.commons.imaging.formats.tiff.constants ExifTagConstants EXIF_TAG_USER_COMMENT.

Click Source Link

Usage

From source file:com.hygenics.imaging.ImageCleanup.java

public byte[] writeMetaData(String data, byte[] ibytes) {
    // write metadata based on the metadata columns list
    TiffOutputSet outset = null;/*from   w  ww  . j  a  v  a 2s.  c om*/
    BufferedImage bi;
    ImageMetadata metadata;
    ByteArrayOutputStream bos = null;
    try {

        // get the buffered image to write to
        bi = Imaging.getBufferedImage(ibytes);
        metadata = Imaging.getMetadata(ibytes);
        JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

        if (null != jpegMetadata) {
            // get the image exif data
            TiffImageMetadata exif = jpegMetadata.getExif();
            outset = exif.getOutputSet();
        }

        if (outset == null) {
            // get a new set (directory structured to write to)
            outset = new TiffOutputSet();
        }

        TiffOutputDirectory exdir = outset.getOrCreateExifDirectory();
        exdir.removeField(ExifTagConstants.EXIF_TAG_USER_COMMENT);

        exdir.add(ExifTagConstants.EXIF_TAG_USER_COMMENT, data.trim());

        bos = new ByteArrayOutputStream();
        ByteArrayInputStream bis = new ByteArrayInputStream(ibytes);

        ExifRewriter exrw = new ExifRewriter();

        // read to a byte stream
        exrw.updateExifMetadataLossy(bis, bos, outset);

        // read the input from the byte buffer
        ibytes = bos.toByteArray();
        bis.close();
        bos.close();

    } catch (ImageReadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ImageWriteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return ibytes;
}

From source file:at.ac.tuwien.qse.sepm.dao.repo.impl.JpegSerializer.java

@Override
public void update(InputStream is, OutputStream os, PhotoMetadata metadata) throws DAOException {
    if (is == null)
        throw new IllegalArgumentException();
    if (os == null)
        throw new IllegalArgumentException();
    if (metadata == null)
        throw new IllegalArgumentException();
    LOGGER.debug("updating photo metadata {}", metadata);

    String tags = "travelimg";

    for (Tag element : metadata.getTags()) {
        tags += "/" + element.getName();
    }//from   w  w w. j a v a2s.co  m

    Rating rating = metadata.getRating();
    tags += "/rating|" + rating;

    Place place = metadata.getPlace();
    if (place != null) {
        tags += "/place|" + place.getCity() + "|" + place.getCountry() + "|" + place.getLatitude() + "|"
                + place.getLongitude();
    }

    Journey journey = metadata.getJourney();
    if (journey != null) {
        tags += "/journey|" + journey.getName() + "|" + journey.getStartDate().format(DATE_FORMATTER) + "|"
                + journey.getEndDate().format(DATE_FORMATTER);
    }

    Photographer photographer = metadata.getPhotographer();
    if (photographer != null) {
        tags += "/photographer|" + photographer.getName();
    }

    try {
        is.mark(Integer.MAX_VALUE);
        ImageMetadata imageData = Imaging.getMetadata(is, null);
        if (imageData == null) {
            LOGGER.debug("could not find image metadata");
            throw new DAOException("No metadata found.");
        }
        if (!(imageData instanceof JpegImageMetadata)) {
            LOGGER.debug("metadata is of unknown type");
            throw new DAOException("Metadata is of unknown type.");
        }

        JpegImageMetadata jpegData = (JpegImageMetadata) imageData;
        TiffOutputSet outputSet = new TiffOutputSet();
        TiffImageMetadata exifData = jpegData.getExif();
        if (exifData != null) {
            outputSet = exifData.getOutputSet();
        }

        TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
        outputSet.setGPSInDegrees(metadata.getLongitude(), metadata.getLatitude());

        exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
        exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL,
                DATE_FORMATTER.format(metadata.getDatetime()));

        exifDirectory.removeField(ExifTagConstants.EXIF_TAG_USER_COMMENT);
        exifDirectory.add(ExifTagConstants.EXIF_TAG_USER_COMMENT, tags);

        is.reset();
        new ExifRewriter().updateExifMetadataLossless(is, os, outputSet);

    } catch (IOException | ImageReadException | ImageWriteException ex) {
        LOGGER.warn("failed updating metadata");
        throw new DAOException(ex);
    }

    LOGGER.debug("updated photo metadata");
}

From source file:at.ac.tuwien.qse.sepm.dao.repo.impl.JpegSerializer.java

private void readMetaData(JpegImageMetadata input, PhotoMetadata result) {
    String tags = "";
    if (input.findEXIFValueWithExactMatch(ExifTagConstants.EXIF_TAG_USER_COMMENT) != null) {
        tags = input.findEXIFValueWithExactMatch(ExifTagConstants.EXIF_TAG_USER_COMMENT).getValueDescription();
        // no tags from our programm
        if (!tags.contains("travelimg"))
            return;
        LOGGER.debug("Tags in exif found: " + tags);
        tags = tags.replace("'", "");
    } else {/* w w  w.  j ava2 s  . co m*/
        return;
    }
    String[] tagArray = tags.split("/");
    for (String element : tagArray) {
        if (element.equals("travelimg"))
            continue;

        // journeys
        if (element.contains("journey")) {
            String[] tempJourney = element.split("\\|");
            LocalDateTime startDate = LocalDateTime.parse(tempJourney[2], DATE_FORMATTER);
            LocalDateTime endDate = LocalDateTime.parse(tempJourney[3], DATE_FORMATTER);
            result.setJourney(new Journey(null, tempJourney[1], startDate, endDate));
            continue;
        }

        // places
        if (element.contains("place")) {
            String[] tempPlace = element.split("\\|");
            result.setPlace(new Place(0, tempPlace[1], tempPlace[2], Double.parseDouble(tempPlace[3]),
                    Double.parseDouble(tempPlace[4])));
            continue;
        }

        // rating
        if (element.contains("rating")) {
            String[] tempRating = element.split("\\|");
            result.setRating(Rating.valueOf(tempRating[1]));
            continue;
        }

        // photographer
        if (element.contains("photographer")) {
            String[] tempPhotographer = element.split("\\|");
            result.setPhotographer(new Photographer(null, tempPhotographer[1]));
            continue;
        }

        // tags
        if (!element.trim().isEmpty()) {
            result.getTags().add(new Tag(null, element));
        }
    }

}