com.drew.lang.Rational Java Examples

The following examples show how to use com.drew.lang.Rational. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OlympusCameraSettingsMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
@Nullable
public String getAfPointSelectedDescription()
{
    Rational[] values = _directory.getRationalArray(TagAfPointSelected);
    if (values == null)
        return "n/a";

    if (values.length < 4)
        return null;

    int index = 0;
    if (values.length == 5 && values[0].longValue() == 0)
        index = 1;

    int p1 = (int)(values[index].doubleValue() * 100);
    int p2 = (int)(values[index + 1].doubleValue() * 100);
    int p3 = (int)(values[index + 2].doubleValue() * 100);
    int p4 = (int)(values[index + 3].doubleValue() * 100);

    if(p1 + p2 + p3 + p4 == 0)
        return "n/a";

    return String.format("(%d%%,%d%%) (%d%%,%d%%)", p1, p2, p3, p4);
}
 
Example #2
Source File: OlympusCameraSettingsMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
@Nullable
public String getFlashIntensityDescription()
{
    Rational[] values = _directory.getRationalArray(TagFlashIntensity);
    if (values == null || values.length == 0)
        return null;

    if (values.length == 3) {
        if (values[0].getDenominator() == 0 && values[1].getDenominator() == 0 && values[2].getDenominator() == 0)
            return "n/a";
    } else if (values.length == 4) {
        if (values[0].getDenominator() == 0 && values[1].getDenominator() == 0 && values[2].getDenominator() == 0 && values[3].getDenominator() == 0)
            return "n/a (x4)";
    }

    StringBuilder sb = new StringBuilder();
    for (Rational t : values)
        sb.append(t).append(", ");

    return sb.substring(0, sb.length() - 2);
}
 
Example #3
Source File: GpsDirectory.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
/**
 * Parses various tags in an attempt to obtain a single object representing the latitude and longitude
 * at which this image was captured.
 *
 * @return The geographical location of this image, if possible, otherwise null
 */
@Nullable
public GeoLocation getGeoLocation()
{
    Rational[] latitudes = getRationalArray(TAG_LATITUDE);
    Rational[] longitudes = getRationalArray(TAG_LONGITUDE);
    String latitudeRef = getString(TAG_LATITUDE_REF);
    String longitudeRef = getString(TAG_LONGITUDE_REF);

    // Make sure we have the required values
    if (latitudes == null || latitudes.length != 3)
        return null;
    if (longitudes == null || longitudes.length != 3)
        return null;
    if (latitudeRef == null || longitudeRef == null)
        return null;

    Double lat = GeoLocation.degreesMinutesSecondsToDecimal(latitudes[0], latitudes[1], latitudes[2], latitudeRef.equalsIgnoreCase("S"));
    Double lon = GeoLocation.degreesMinutesSecondsToDecimal(longitudes[0], longitudes[1], longitudes[2], longitudeRef.equalsIgnoreCase("W"));

    // This can return null, in cases where the conversion was not possible
    if (lat == null || lon == null)
        return null;

    return new GeoLocation(lat, lon);
}
 
Example #4
Source File: MovieHeaderBox.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
public void addMetadata(Mp4Directory directory)
{
    // Get creation/modification times
    directory.setDate(Mp4Directory.TAG_CREATION_TIME, DateUtil.get1Jan1904EpochDate(creationTime));
    directory.setDate(Mp4Directory.TAG_MODIFICATION_TIME, DateUtil.get1Jan1904EpochDate(modificationTime));

    // Get duration and time scale
    directory.setLong(Mp4Directory.TAG_DURATION, duration);
    directory.setLong(Mp4Directory.TAG_TIME_SCALE, timescale);
    directory.setRational(Mp4Directory.TAG_DURATION_SECONDS, new Rational(duration, timescale));

    directory.setIntArray(Mp4Directory.TAG_TRANSFORMATION_MATRIX, matrix);

    // Calculate preferred rate fixed point
    double preferredRateInteger = (rate & 0xFFFF0000) >> 16;
    double preferredRateFraction = (rate & 0x0000FFFF) / Math.pow(2, 4);
    directory.setDouble(Mp4Directory.TAG_PREFERRED_RATE, preferredRateInteger + preferredRateFraction);

    // Calculate preferred volume fixed point
    double preferredVolumeInteger = (volume & 0xFF00) >> 8;
    double preferredVolumeFraction = (volume & 0x00FF) / Math.pow(2, 2);
    directory.setDouble(Mp4Directory.TAG_PREFERRED_VOLUME, preferredVolumeInteger + preferredVolumeFraction);

    directory.setLong(Mp4Directory.TAG_NEXT_TRACK_ID, nextTrackID);
}
 
Example #5
Source File: GpsDirectory.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the date stamp tag and the time stamp tag to obtain a single Date object representing the
 * date and time when this image was captured.
 *
 * @return A Date object representing when this image was captured, if possible, otherwise null
 */
@Nullable
public Date getGpsDate()
{
    String date = getString(TAG_DATE_STAMP);
    Rational[] timeComponents = getRationalArray(TAG_TIME_STAMP);

    // Make sure we have the required values
    if (date == null)
        return null;
    if (timeComponents == null || timeComponents.length != 3)
        return null;

    String dateTime = String.format(Locale.US, "%s %02d:%02d:%02.3f UTC",
        date, timeComponents[0].intValue(), timeComponents[1].intValue(), timeComponents[2].doubleValue());
    try {
        DateFormat parser = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss.S z");
        return parser.parse(dateTime);
    } catch (ParseException e) {
        return null;
    }
}
 
Example #6
Source File: DirectoryTest.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnderlyingInt() throws Exception
{
    int value = 123;
    int tagType = 321;
    _directory.setInt(tagType, value);

    assertEquals(value, _directory.getInt(tagType));
    assertEquals(Integer.valueOf(value), _directory.getInteger(tagType));
    assertEquals((float)value, _directory.getFloat(tagType), 0.00001);
    assertEquals((double)value, _directory.getDouble(tagType), 0.00001);
    assertEquals((long)value, _directory.getLong(tagType));
    assertEquals(Integer.toString(value), _directory.getString(tagType));
    assertEquals(new Rational(value, 1), _directory.getRational(tagType));
    assertArrayEquals(new int[]{value}, _directory.getIntArray(tagType));
    assertArrayEquals(new byte[]{(byte)value}, _directory.getByteArray(tagType));
}
 
Example #7
Source File: OlympusCameraSettingsMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
@Nullable
public String getManualFlashStrengthDescription()
{
    Rational[] values = _directory.getRationalArray(TagManualFlashStrength);
    if (values == null || values.length == 0)
        return "n/a";

    if (values.length == 3) {
        if (values[0].getDenominator() == 0 && values[1].getDenominator() == 0 && values[2].getDenominator() == 0)
            return "n/a";
    } else if (values.length == 4) {
        if (values[0].getDenominator() == 0 && values[1].getDenominator() == 0 && values[2].getDenominator() == 0 && values[3].getDenominator() == 0)
            return "n/a (x4)";
    }

    StringBuilder sb = new StringBuilder();
    for (Rational t : values)
        sb.append(t).append(", ");

    return sb.substring(0, sb.length() - 2);
}
 
Example #8
Source File: Directory.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
/** Returns the specified tag's value as a Rational.  If the value is unset or cannot be converted, <code>null</code> is returned. */
@Nullable
public Rational getRational(int tagType)
{
    Object o = getObject(tagType);

    if (o == null)
        return null;

    if (o instanceof Rational)
        return (Rational)o;
    if (o instanceof Integer)
        return new Rational((Integer)o, 1);
    if (o instanceof Long)
        return new Rational((Long)o, 1);

    // NOTE not doing conversions for real number types

    return null;
}
 
Example #9
Source File: OlympusRawInfoMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 6 votes vote down vote up
@Nullable
public String getYCbCrCoefficientsDescription()
{
    int[] values = _directory.getIntArray(TagYCbCrCoefficients);
    if (values == null)
        return null;

    Rational[] ret = new Rational[values.length / 2];
    for(int i = 0; i < values.length / 2; i++)
    {
        ret[i] = new Rational((short)values[2*i], (short)values[2*i + 1]);
    }

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < ret.length; i++) {
        sb.append(ret[i].doubleValue());
        if (i < ret.length - 1)
            sb.append(" ");
    }
    return sb.length() == 0 ? null : sb.toString();
}
 
Example #10
Source File: ExifIFD0DescriptorTest.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Test
public void testXResolutionDescription() throws Exception
{
    ExifIFD0Directory directory = new ExifIFD0Directory();
    directory.setRational(TAG_X_RESOLUTION, new Rational(72, 1));
    // 2 is for 'Inch'
    directory.setInt(TAG_RESOLUTION_UNIT, 2);
    ExifIFD0Descriptor descriptor = new ExifIFD0Descriptor(directory);
    assertEquals("72 dots per inch", descriptor.getDescription(TAG_X_RESOLUTION));
}
 
Example #11
Source File: Directory.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
/** Returns the specified tag's value as an array of Rational.  If the value is unset or cannot be converted, <code>null</code> is returned. */
@Nullable
public Rational[] getRationalArray(int tagType)
{
    Object o = getObject(tagType);
    if (o == null)
        return null;

    if (o instanceof Rational[])
        return (Rational[])o;

    return null;
}
 
Example #12
Source File: ExifReaderTest.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Test
public void testThumbnailYResolution() throws Exception
{
    ExifThumbnailDirectory directory = ExifReaderTest.processBytes("Tests/Data/manuallyAddedThumbnail.jpg.app1", ExifThumbnailDirectory.class);

    Rational rational = directory.getRational(ExifThumbnailDirectory.TAG_Y_RESOLUTION);
    assertNotNull(rational);
    assertEquals(72, rational.getNumerator());
    assertEquals(1, rational.getDenominator());
}
 
Example #13
Source File: ExifReaderTest.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Test
public void testThumbnailXResolution() throws Exception
{
    ExifThumbnailDirectory directory = ExifReaderTest.processBytes("Tests/Data/manuallyAddedThumbnail.jpg.app1", ExifThumbnailDirectory.class);

    Rational rational = directory.getRational(ExifThumbnailDirectory.TAG_X_RESOLUTION);
    assertNotNull(rational);
    assertEquals(72, rational.getNumerator());
    assertEquals(1, rational.getDenominator());
}
 
Example #14
Source File: NikonType2MakernoteTest2.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Test
public void testExifDirectory_MatchesKnownValues() throws Exception
{
    assertEquals("          ", _exifIFD0Directory.getString(ExifIFD0Directory.TAG_IMAGE_DESCRIPTION));
    assertEquals("NIKON", _exifIFD0Directory.getString(ExifIFD0Directory.TAG_MAKE));
    assertEquals("E995", _exifIFD0Directory.getString(ExifIFD0Directory.TAG_MODEL));
    assertEquals(300, _exifIFD0Directory.getDouble(ExifIFD0Directory.TAG_X_RESOLUTION), 0.001);
    assertEquals(300, _exifIFD0Directory.getDouble(ExifIFD0Directory.TAG_Y_RESOLUTION), 0.001);
    assertEquals(2, _exifIFD0Directory.getInt(ExifIFD0Directory.TAG_RESOLUTION_UNIT));
    assertEquals("E995v1.6", _exifIFD0Directory.getString(ExifIFD0Directory.TAG_SOFTWARE));
    assertEquals("2002:08:29 17:31:40", _exifIFD0Directory.getString(ExifIFD0Directory.TAG_DATETIME));
    assertEquals(1, _exifIFD0Directory.getInt(ExifIFD0Directory.TAG_YCBCR_POSITIONING));

    assertEquals(new Rational(2439024, 100000000), _exifSubIFDDirectory.getRational(ExifSubIFDDirectory.TAG_EXPOSURE_TIME));
    assertEquals(2.6, _exifSubIFDDirectory.getDouble(ExifSubIFDDirectory.TAG_FNUMBER), 0.001);
    assertEquals(2, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_EXPOSURE_PROGRAM));
    assertEquals(100, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT));
    assertEquals("48 50 49 48", _exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_EXIF_VERSION));
    assertEquals("2002:08:29 17:31:40", _exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED));
    assertEquals("2002:08:29 17:31:40", _exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL));
    assertEquals("1 2 3 0", _exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_COMPONENTS_CONFIGURATION));
    assertEquals(0, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_EXPOSURE_BIAS));
    assertEquals("0", _exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_MAX_APERTURE));
    assertEquals(5, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_METERING_MODE));
    assertEquals(0, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_WHITE_BALANCE));
    assertEquals(1, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_FLASH));
    assertEquals(8.2, _exifSubIFDDirectory.getDouble(ExifSubIFDDirectory.TAG_FOCAL_LENGTH), 0.001);
    assertEquals("0 0 0 0 0 0 0 0 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32", _exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_USER_COMMENT));
    assertEquals("48 49 48 48", _exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_FLASHPIX_VERSION));
    assertEquals(1, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_COLOR_SPACE));
    assertEquals(2048, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH));
    assertEquals(1536, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT));
    assertEquals(3, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_FILE_SOURCE));
    assertEquals(1, _exifSubIFDDirectory.getInt(ExifSubIFDDirectory.TAG_SCENE_TYPE));
}
 
Example #15
Source File: ExifIFD0DescriptorTest.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Test
public void testYResolutionDescription() throws Exception
{
    ExifIFD0Directory directory = new ExifIFD0Directory();
    directory.setRational(TAG_Y_RESOLUTION, new Rational(50, 1));
    // 3 is for 'cm'
    directory.setInt(TAG_RESOLUTION_UNIT, 3);
    ExifIFD0Descriptor descriptor = new ExifIFD0Descriptor(directory);
    assertEquals("50 dots per cm", descriptor.getDescription(TAG_Y_RESOLUTION));
}
 
Example #16
Source File: OlympusMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getIsoValueDescription()
{
    Rational value = _directory.getRational(TAG_ISO_VALUE);
    if (value == null)
        return null;

    return String.valueOf(Math.round(Math.pow(2, value.doubleValue() - 5) * 100));
}
 
Example #17
Source File: TagDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getRationalOrDoubleString(int tagType)
{
    Rational rational = _directory.getRational(tagType);
    if (rational != null)
        return rational.toSimpleString(true);

    Double d = _directory.getDoubleObject(tagType);
    if (d != null) {
        DecimalFormat format = new DecimalFormat("0.###");
        return format.format(d);
    }

    return null;
}
 
Example #18
Source File: TagDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getSimpleRational(final int tagType)
{
    Rational value = _directory.getRational(tagType);
    if (value == null)
        return null;
    return value.toSimpleString(true);
}
 
Example #19
Source File: TagDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getDecimalRational(final int tagType, final int decimalPlaces)
{
    Rational value = _directory.getRational(tagType);
    if (value == null)
        return null;
    return String.format("%." + decimalPlaces + "f", value.doubleValue());
}
 
Example #20
Source File: ExifDescriptorBase.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getCameraElevationAngleDescription()
{
    Rational value = _directory.getRational(TAG_CAMERA_ELEVATION_ANGLE);
    if (value == null)
        return null;
    if (value.getDenominator() == 0xFFFFFFFFL)
        return "Unknown";
    DecimalFormat formatter = new DecimalFormat("0.##");
    return formatter.format(value.doubleValue()) + " degrees";
}
 
Example #21
Source File: OlympusMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getFocalPlaneDiagonalDescription()
{
    Rational value = _directory.getRational(TAG_FOCAL_PLANE_DIAGONAL);
    if (value == null)
        return null;

    DecimalFormat format = new DecimalFormat("0.###");
    return format.format(value.doubleValue()) + " mm";
}
 
Example #22
Source File: OlympusMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getDigitalZoomDescription()
{
    Rational value = _directory.getRational(TAG_DIGITAL_ZOOM);
    if (value == null)
        return null;
    return value.toSimpleString(false);
}
 
Example #23
Source File: NikonType2MakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getDigitalZoomDescription()
{
    Rational value = _directory.getRational(TAG_DIGITAL_ZOOM);
    if (value==null)
        return null;
    return value.intValue() == 1
            ? "No digital zoom"
            : value.toSimpleString(true) + "x digital zoom";
}
 
Example #24
Source File: OlympusFocusInfoMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getFocusDistanceDescription()
{
    Rational value = _directory.getRational(TagFocusDistance);
    if (value == null)
        return "inf";
    if (value.getNumerator() == 0xFFFFFFFFL || value.getNumerator() == 0x00000000L)
        return "inf";

    return value.getNumerator() / 1000.0 + " m";
}
 
Example #25
Source File: NikonType1MakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getFocusDescription()
{
    Rational value = _directory.getRational(TAG_FOCUS);
    return value == null
        ? null
        : value.getNumerator() == 1 && value.getDenominator() == 0
            ? "Infinite"
            : value.toSimpleString(true);
}
 
Example #26
Source File: NikonType1MakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getDigitalZoomDescription()
{
    Rational value = _directory.getRational(TAG_DIGITAL_ZOOM);
    return value == null
        ? null
        : value.getNumerator() == 0
            ? "No digital zoom"
            : value.toSimpleString(true) + "x digital zoom";
}
 
Example #27
Source File: AppleMakernoteDescriptor.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getAccelerationVectorDescription()
{
    Rational[] values = _directory.getRationalArray(AppleMakernoteDirectory.TAG_ACCELERATION_VECTOR);
    if (values == null || values.length != 3)
        return null;
    return String.format("%.2fg %s, ", values[0].getAbsolute().doubleValue(), values[0].isPositive() ? "left" : "right") +
           String.format("%.2fg %s, ", values[1].getAbsolute().doubleValue(), values[1].isPositive() ? "down" : "up") +
           String.format("%.2fg %s",   values[2].getAbsolute().doubleValue(), values[2].isPositive() ? "forward" : "backward");
}
 
Example #28
Source File: ExifDescriptorBase.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getDigitalZoomRatioDescription()
{
    Rational value = _directory.getRational(TAG_DIGITAL_ZOOM_RATIO);
    return value == null
        ? null
        : value.getNumerator() == 0
            ? "Digital zoom not used"
            : new DecimalFormat("0.#").format(value.doubleValue());
}
 
Example #29
Source File: ExifDescriptorBase.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getFocalPlaneXResolutionDescription()
{
    Rational rational = _directory.getRational(TAG_FOCAL_PLANE_X_RESOLUTION);
    if (rational == null)
        return null;
    final String unit = getFocalPlaneResolutionUnitDescription();
    return rational.getReciprocal().toSimpleString(_allowDecimalRepresentationOfRationals)
        + (unit == null ? "" : " " + unit.toLowerCase());
}
 
Example #30
Source File: ExifDescriptorBase.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getFocalPlaneYResolutionDescription()
{
    Rational rational = _directory.getRational(TAG_FOCAL_PLANE_Y_RESOLUTION);
    if (rational == null)
        return null;
    final String unit = getFocalPlaneResolutionUnitDescription();
    return rational.getReciprocal().toSimpleString(_allowDecimalRepresentationOfRationals)
        + (unit == null ? "" : " " + unit.toLowerCase());
}