Example usage for org.apache.commons.imaging.formats.tiff.write TiffOutputSet getOrCreateRootDirectory

List of usage examples for org.apache.commons.imaging.formats.tiff.write TiffOutputSet getOrCreateRootDirectory

Introduction

In this page you can find the example usage for org.apache.commons.imaging.formats.tiff.write TiffOutputSet getOrCreateRootDirectory.

Prototype

public TiffOutputDirectory getOrCreateRootDirectory() throws ImageWriteException 

Source Link

Usage

From source file:org.openstreetmap.josm.plugins.mapillary.oauth.UploadUtils.java

/**
 * Returns a file containing the picture and an updated version of the EXIF
 * tags.//from w  ww  .  j a v a2 s  .c om
 *
 * @param image The image to be uploaded
 * @return A File object containing the picture and an updated version of the
 * EXIF tags.
 * @throws ImageReadException  if there are errors reading the image from the file.
 * @throws IOException         if there are errors getting the metadata from the file or writing
 *                             the output.
 * @throws ImageWriteException if there are errors writing the image in the file.
 */
static File updateFile(MapillaryImportedImage image)
        throws ImageReadException, IOException, ImageWriteException {
    TiffOutputSet outputSet = null;
    TiffOutputDirectory exifDirectory;
    TiffOutputDirectory gpsDirectory;
    TiffOutputDirectory rootDirectory;

    // If the image is imported, loads the rest of the EXIF data.
    JpegImageMetadata jpegMetadata = null;
    try {
        ImageMetadata metadata = Imaging.getMetadata(image.getFile());
        jpegMetadata = (JpegImageMetadata) metadata;
    } catch (Exception e) {
        Main.warn(e);
    }

    if (null != jpegMetadata) {
        final TiffImageMetadata exif = jpegMetadata.getExif();
        if (null != exif) {
            outputSet = exif.getOutputSet();
        }
    }
    if (null == outputSet) {
        outputSet = new TiffOutputSet();
    }
    gpsDirectory = outputSet.getOrCreateGPSDirectory();
    exifDirectory = outputSet.getOrCreateExifDirectory();
    rootDirectory = outputSet.getOrCreateRootDirectory();

    gpsDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF);
    gpsDirectory.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF,
            GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF_VALUE_TRUE_NORTH);

    gpsDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION);
    gpsDirectory.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, RationalNumber.valueOf(image.getMovingCa()));

    exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
    exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, image.getDate("yyyy/MM/dd HH:mm:ss"));

    // Removes the ImageDescription tag, that causes problems in the upload.
    rootDirectory.removeField(TiffTagConstants.TIFF_TAG_IMAGE_DESCRIPTION);

    outputSet.setGPSInDegrees(image.getMovingLatLon().lon(), image.getMovingLatLon().lat());
    File tempFile = File.createTempFile("imagetoupload_" + c, ".tmp");
    c++;
    OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile));

    // Transforms the image into a byte array.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ImageIO.write(image.getImage(), "jpg", outputStream);
    byte[] imageBytes = outputStream.toByteArray();
    new ExifRewriter().updateExifMetadataLossless(imageBytes, os, outputSet);
    os.close();
    return tempFile;
}

From source file:slash.navigation.photo.PhotoFormat.java

public void write(PhotoPosition position, File source, OutputStream target) throws IOException {
    try {// ww w.  j  av  a2  s. c o  m
        ImageMetadata metadata = Imaging.getMetadata(source);
        TiffImageMetadata tiffImageMetadata = extractTiffImageMetadata(metadata);

        TiffOutputSet outputSet = null;
        if (tiffImageMetadata != null)
            outputSet = tiffImageMetadata.getOutputSet();
        if (outputSet == null)
            outputSet = new TiffOutputSet();

        TiffOutputDirectory rootDirectory = outputSet.getOrCreateRootDirectory();
        rootDirectory.removeField(TIFF_TAG_MAKE);
        rootDirectory.removeField(TIFF_TAG_MODEL);

        rootDirectory.add(TIFF_TAG_MAKE, position.getMake());
        rootDirectory.add(TIFF_TAG_MODEL, position.getModel());

        TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
        exifDirectory.removeField(EXIF_TAG_USER_COMMENT);
        exifDirectory.removeField(EXIF_TAG_EXIF_IMAGE_WIDTH);
        exifDirectory.removeField(EXIF_TAG_EXIF_IMAGE_LENGTH);
        exifDirectory.removeField(EXIF_TAG_FNUMBER);
        exifDirectory.removeField(EXIF_TAG_EXPOSURE_TIME);
        exifDirectory.removeField(EXIF_TAG_FLASH);
        exifDirectory.removeField(EXIF_TAG_FOCAL_LENGTH);
        exifDirectory.removeField(EXIF_TAG_ISO);

        exifDirectory.add(EXIF_TAG_USER_COMMENT, position.getDescription());
        if (position.getWidth() != null)
            exifDirectory.add(EXIF_TAG_EXIF_IMAGE_WIDTH, position.getWidth().shortValue());
        if (position.getHeight() != null)
            exifDirectory.add(EXIF_TAG_EXIF_IMAGE_LENGTH, position.getHeight().shortValue());
        exifDirectory.add(EXIF_TAG_FNUMBER, position.getfNumber());
        exifDirectory.add(EXIF_TAG_EXPOSURE_TIME, position.getExposure());
        if (position.getFlash() != null)
            exifDirectory.add(EXIF_TAG_FLASH, position.getFlash().shortValue());
        exifDirectory.add(EXIF_TAG_FOCAL_LENGTH, position.getFocal());
        if (position.getPhotographicSensitivity() != null)
            exifDirectory.add(EXIF_TAG_ISO, position.getPhotographicSensitivity().shortValue());

        TiffOutputDirectory gpsDirectory = outputSet.getOrCreateGPSDirectory();
        gpsDirectory.removeField(GPS_TAG_GPS_VERSION_ID);
        gpsDirectory.add(GPS_TAG_GPS_VERSION_ID, (byte) 2, (byte) 3, (byte) 0, (byte) 0);

        gpsDirectory.removeField(GPS_TAG_GPS_LONGITUDE);
        gpsDirectory.removeField(GPS_TAG_GPS_LONGITUDE_REF);
        gpsDirectory.removeField(GPS_TAG_GPS_LATITUDE);
        gpsDirectory.removeField(GPS_TAG_GPS_LATITUDE_REF);
        gpsDirectory.removeField(GPS_TAG_GPS_ALTITUDE);
        gpsDirectory.removeField(GPS_TAG_GPS_ALTITUDE_REF);
        gpsDirectory.removeField(GPS_TAG_GPS_SPEED);
        gpsDirectory.removeField(GPS_TAG_GPS_SPEED_REF);
        gpsDirectory.removeField(GPS_TAG_GPS_DATE_STAMP);
        gpsDirectory.removeField(GPS_TAG_GPS_TIME_STAMP);
        gpsDirectory.removeField(GPS_TAG_GPS_IMG_DIRECTION);
        gpsDirectory.removeField(GPS_TAG_GPS_SATELLITES);
        gpsDirectory.removeField(GPS_TAG_GPS_MEASURE_MODE);
        gpsDirectory.removeField(GPS_TAG_GPS_DOP);

        if (position.getLongitude() != null && position.getLatitude() != null)
            outputSet.setGPSInDegrees(position.getLongitude(), position.getLatitude());

        if (position.getElevation() != null) {
            gpsDirectory.add(GPS_TAG_GPS_ALTITUDE, RationalNumber.valueOf(abs(position.getElevation())));
            gpsDirectory.add(GPS_TAG_GPS_ALTITUDE_REF, (byte) (position.getElevation() > 0 ? 0 : 1));
        }

        if (position.getSpeed() != null) {
            gpsDirectory.add(GPS_TAG_GPS_SPEED, RationalNumber.valueOf(position.getSpeed()));
            gpsDirectory.add(GPS_TAG_GPS_SPEED_REF, GPS_TAG_GPS_SPEED_REF_VALUE_KMPH);
        }

        if (position.getTime() != null) {
            Calendar calendar = position.getTime().getCalendar();

            gpsDirectory.add(GPS_TAG_GPS_TIME_STAMP, RationalNumber.valueOf(calendar.get(HOUR_OF_DAY)),
                    RationalNumber.valueOf(calendar.get(MINUTE)), RationalNumber.valueOf(calendar.get(SECOND)));
            String dateStamp = XXXX_FORMAT.format(calendar.get(YEAR)) + ":"
                    + XX_FORMAT.format(calendar.get(MONTH) + 1) + ":"
                    + XX_FORMAT.format(calendar.get(DAY_OF_MONTH));
            gpsDirectory.add(GPS_TAG_GPS_DATE_STAMP, dateStamp);
        }

        if (position.getHeading() != null)
            gpsDirectory.add(GPS_TAG_GPS_IMG_DIRECTION, RationalNumber.valueOf(position.getHeading()));

        if (position.getSatellites() != null)
            gpsDirectory.add(GPS_TAG_GPS_SATELLITES, position.getSatellites().toString());

        if (position.getPdop() != null) {
            gpsDirectory.add(GPS_TAG_GPS_MEASURE_MODE,
                    Integer.toString(GPS_TAG_GPS_MEASURE_MODE_VALUE_3_DIMENSIONAL_MEASUREMENT));
            gpsDirectory.add(GPS_TAG_GPS_DOP, RationalNumber.valueOf(position.getPdop()));
        } else if (position.getHdop() != null) {
            gpsDirectory.add(GPS_TAG_GPS_MEASURE_MODE,
                    Integer.toString(GPS_TAG_GPS_MEASURE_MODE_VALUE_2_DIMENSIONAL_MEASUREMENT));
            gpsDirectory.add(GPS_TAG_GPS_DOP, RationalNumber.valueOf(position.getHdop()));
        }

        new ExifRewriter().updateExifMetadataLossless(source, target, outputSet);
    } catch (ImageReadException | ImageWriteException e) {
        throw new IOException(e);
    }
}