Example usage for org.apache.commons.imaging.common RationalNumber RationalNumber

List of usage examples for org.apache.commons.imaging.common RationalNumber RationalNumber

Introduction

In this page you can find the example usage for org.apache.commons.imaging.common RationalNumber RationalNumber.

Prototype

public RationalNumber(final int numerator, final int divisor) 

Source Link

Usage

From source file:org.apache.commons.imaging.examples.WriteExifMetadataExample.java

/**
 * This example illustrates how to add/update EXIF metadata in a JPEG file.
 * //from   w  ww. ja va2s .  c  om
 * @param jpegImageFile
 *            A source image file.
 * @param dst
 *            The output file.
 * @throws IOException
 * @throws ImageReadException
 * @throws ImageWriteException
 */
public void changeExifMetadata(final File jpegImageFile, final File dst)
        throws IOException, ImageReadException, ImageWriteException {

    try (FileOutputStream fos = new FileOutputStream(dst); OutputStream os = new BufferedOutputStream(fos);) {

        TiffOutputSet outputSet = null;

        // note that metadata might be null if no metadata is found.
        final ImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
        if (null != jpegMetadata) {
            // note that exif might be null if no Exif metadata is found.
            final TiffImageMetadata exif = jpegMetadata.getExif();

            if (null != exif) {
                // TiffImageMetadata class is immutable (read-only).
                // TiffOutputSet class represents the Exif data to write.
                //
                // Usually, we want to update existing Exif metadata by
                // changing
                // the values of a few fields, or adding a field.
                // In these cases, it is easiest to use getOutputSet() to
                // start with a "copy" of the fields read from the image.
                outputSet = exif.getOutputSet();
            }
        }

        // if file does not contain any exif metadata, we create an empty
        // set of exif metadata. Otherwise, we keep all of the other
        // existing tags.
        if (null == outputSet) {
            outputSet = new TiffOutputSet();
        }

        {
            // Example of how to add a field/tag to the output set.
            //
            // Note that you should first remove the field/tag if it already
            // exists in this directory, or you may end up with duplicate
            // tags. See above.
            //
            // Certain fields/tags are expected in certain Exif directories;
            // Others can occur in more than one directory (and often have a
            // different meaning in different directories).
            //
            // TagInfo constants often contain a description of what
            // directories are associated with a given tag.
            //
            final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
            // make sure to remove old value if present (this method will
            // not fail if the tag does not exist).
            exifDirectory.removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
            exifDirectory.add(ExifTagConstants.EXIF_TAG_APERTURE_VALUE, new RationalNumber(3, 10));
        }

        {
            // Example of how to add/update GPS info to output set.

            // New York City
            final double longitude = -74.0; // 74 degrees W (in Degrees East)
            final double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees
            // North)

            outputSet.setGPSInDegrees(longitude, latitude);
        }

        // printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);

        new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet);
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtilsTest.java

/**
 * Test {@link MapillaryUtils#degMinSecToDouble(RationalNumber[], String)}
 * method./*from  w w  w  .  j av a2  s  . c  om*/
 */
@Test
public void degMinSecToDoubleTest() {
    RationalNumber[] num = new RationalNumber[3];
    num[0] = new RationalNumber(1, 1);
    num[1] = new RationalNumber(0, 1);
    num[2] = new RationalNumber(0, 1);
    String ref = GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF_VALUE_NORTH;
    assertEquals(1, MapillaryUtils.degMinSecToDouble(num, ref), 0.01);
    ref = GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF_VALUE_SOUTH;
    assertEquals(-1, MapillaryUtils.degMinSecToDouble(num, ref), 0.01);
    num[0] = new RationalNumber(180, 1);
    assertEquals(-180, MapillaryUtils.degMinSecToDouble(num, ref), 0.01);
    num[0] = new RationalNumber(190, 1);
    assertEquals(170, MapillaryUtils.degMinSecToDouble(num, ref), 0.01);
}

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

@Test
public void testIsJpgWithEmbeddedExifGPSMetadata() throws IOException {
    File source = new File(TEST_PATH + "from-gps.jpg");
    ParserResult result = parser.read(source);
    assertNotNull(result);/*from  w  w w  .  j  a  v a2 s.  c  o  m*/
    assertEquals(PhotoFormat.class, result.getFormat().getClass());
    BaseRoute theRoute = result.getTheRoute();
    assertEquals(1, theRoute.getPositionCount());
    Wgs84Route route = (Wgs84Route) theRoute;
    PhotoPosition position = (PhotoPosition) route.getPosition(0);
    assertEquals("NIKON CORPORATION NIKON D90 Photo from 2010-08-31T10:31:27Z", position.getDescription());
    assertDoubleEquals(135.0, position.getElevation());
    assertDoubleEquals(8.474513333333334, position.getLongitude());
    assertDoubleEquals(53.026513333333334, position.getLatitude());
    assertNull(position.getSpeed());
    assertEquals(calendar(2010, 8, 31, 8, 30, 58), position.getTime());
    assertEquals(new Integer(10), position.getSatellites());
    assertNull(position.getHdop());
    assertNull(position.getPdop());
    assertNull(position.getVdop());
    assertNull(position.getHeading());
    assertEquals("NIKON CORPORATION", position.getMake());
    assertEquals("NIKON D90", position.getModel());
    assertEquals(new Integer(1024), position.getWidth());
    assertEquals(new Integer(680), position.getHeight());
    assertRationalNumberEquals(new RationalNumber(50, 10), position.getfNumber());
    assertRationalNumberEquals(new RationalNumber(10, 8000), position.getExposure());
    assertEquals(new Integer(0), position.getFlash());
    assertRationalNumberEquals(new RationalNumber(700, 10), position.getFocal());
    assertEquals(new Integer(200), position.getPhotographicSensitivity());
}

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

@Test
public void testIsJpgWithEmbeddedExifMetadata() throws IOException {
    File source = new File(TEST_PATH + "from-exif.jpg");
    ParserResult result = parser.read(source);
    assertNotNull(result);/*from  w ww . j a  v  a2s.  c  o  m*/
    assertEquals(PhotoFormat.class, result.getFormat().getClass());
    BaseRoute theRoute = result.getTheRoute();
    assertEquals(1, theRoute.getPositionCount());
    Wgs84Route route = (Wgs84Route) theRoute;
    PhotoPosition position = (PhotoPosition) route.getPosition(0);
    assertEquals("Palm Pre Photo from 2010-01-30T13:10:15Z", position.getDescription());
    assertNull(position.getElevation());
    assertNull(position.getLongitude());
    assertNull(position.getLatitude());
    assertNull(position.getSpeed());
    assertCalendarEquals(calendar(2010, 1, 30, 13, 10, 15), position.getTime());
    assertNull(position.getSatellites());
    assertNull(position.getHdop());
    assertNull(position.getPdop());
    assertNull(position.getVdop());
    assertNull(position.getHeading());
    assertEquals("Palm", position.getMake());
    assertEquals("Pre", position.getModel());
    assertEquals(new Integer(1520), position.getWidth());
    assertEquals(new Integer(2032), position.getHeight());
    assertRationalNumberEquals(new RationalNumber(24, 10), position.getfNumber());
    assertRationalNumberEquals(new RationalNumber(1, 65536000), position.getExposure());
    assertEquals(new Integer(24), position.getFlash());
    assertRationalNumberEquals(new RationalNumber(100, 41), position.getFocal());
    assertNull(position.getPhotographicSensitivity());
}

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

private void modifyImage(String path) throws IOException {
    File source = new File(path);
    ParserResult result = parser.read(source);
    assertNotNull(result);/*from ww  w  .ja  v  a  2s .c o  m*/
    BaseRoute theRoute = result.getTheRoute();
    PhotoFormat format = (PhotoFormat) result.getFormat();
    Wgs84Route route = (Wgs84Route) theRoute;
    PhotoPosition position = (PhotoPosition) route.getPosition(0);
    position.setDescription("description");
    position.setElevation(222.0);
    position.setLongitude(10.0);
    position.setLatitude(50.0);
    position.setSpeed(40.0);
    position.setTime(calendar(2014, 2, 11, 15, 3, 25));
    position.setSatellites(4);
    position.setHdop(1.0);
    position.setPdop(2.0);
    position.setVdop(3.0);
    position.setHeading(111.0);
    position.setMake("make");
    position.setModel("model");
    position.setWidth(1024);
    position.setHeight(512);
    position.setfNumber(new RationalNumber(53, 10));
    position.setExposure(new RationalNumber(11, 8000));
    position.setFlash(1);
    position.setFocal(new RationalNumber(35, 1));
    position.setPhotographicSensitivity(800);

    File target = createTempFile("target", ".jpg");
    target.deleteOnExit();
    try {
        parser.write(route, format, false, false, null, target);
        assertTrue(target.exists());

        ParserResult result2 = parser.read(target);
        assertNotNull(result2);
        assertEquals(PhotoFormat.class, result2.getFormat().getClass());
        BaseRoute theRoute2 = result2.getTheRoute();
        Wgs84Route route2 = (Wgs84Route) theRoute2;
        assertEquals(1, route2.getPositionCount());
        PhotoPosition position2 = (PhotoPosition) route2.getPosition(0);
        assertEquals("description", position2.getDescription());
        assertDoubleEquals(222.0, position2.getElevation());
        assertDoubleEquals(10.0, position2.getLongitude());
        assertDoubleEquals(50.0, position2.getLatitude());
        assertDoubleEquals(40.0, position2.getSpeed());
        assertCalendarEquals(calendar(2014, 2, 11, 15, 3, 25), position2.getTime());
        assertEquals(new Integer(4), position2.getSatellites());
        assertNull(position2.getHdop());
        assertDoubleEquals(2.0, position2.getPdop());
        assertNull(position2.getVdop());
        assertDoubleEquals(111.0, position2.getHeading());
        assertEquals("make", position2.getMake());
        assertEquals("model", position2.getModel());
        assertEquals(new Integer(1024), position2.getWidth());
        assertEquals(new Integer(512), position2.getHeight());
        assertRationalNumberEquals(new RationalNumber(53, 10), position2.getfNumber());
        assertRationalNumberEquals(new RationalNumber(11, 8000), position2.getExposure());
        assertEquals(new Integer(1), position2.getFlash());
        assertRationalNumberEquals(new RationalNumber(35, 1), position2.getFocal());
        assertEquals(new Integer(800), position.getPhotographicSensitivity());

        assertTrue(target.delete());
    } finally {
        // avoid to clutter the temp directory
        if (target.exists())
            assertTrue(target.delete());
    }
}