drewnoakes / metadata-extractor

Extracts Exif, IPTC, XMP, ICC and other metadata from image, video and audio files
Apache License 2.0
2.57k stars 482 forks source link

Exposure time of 0.04 sec has wrong string representation #573

Open ghost opened 2 years ago

ghost commented 2 years ago

This photo.jpg (part of the free Unsplash image set) was taken with an exposure time of 1/25

This sample code reports the exposure time as 0.04 sec:

val metadata = ImageMetadataReader.readMetadata(file)

val exif: ExifSubIFDDirectory? = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory::class.java)

val exposureTimeString = exif?.getString(ExifSubIFDDirectory.TAG_EXPOSURE_TIME)

println(exposureTimeString)

I assume there is a problem with Rational.toSimpleString()

Using this Kotlin method I get the expected string 1/25:

    fun formatExposureTime(seconds: Double): String {

        if (seconds < 1.0) {

            return "1/" + (0.5 + 1 / seconds).toInt()

        } else {

            val roundedSeconds = seconds.toInt()

            val fractionSeconds = seconds - roundedSeconds

            if (fractionSeconds > 0.0001)
                return "$roundedSeconds'' 1/" + (0.5 + 1 / fractionSeconds).toInt()

            return "$roundedSeconds''"
        }
    }
drewnoakes commented 2 years ago

Changing toSimpleString can negatively impact other property descriptions. For example:

diff --git a/heic/metadata/java/Issue 263 dotnet.heic.txt b/heic/metadata/java/Issue 263 dotnet.heic.txt
index f1caa71c..14e9e6f9 100644
--- a/heic/metadata/java/Issue 263 dotnet.heic.txt  
+++ b/heic/metadata/java/Issue 263 dotnet.heic.txt  
@@ -73,7 +73,7 @@ TYPE: HEIF
 [Exif SubIFD - 0xa404] Digital Zoom Ratio = 1.1
 [Exif SubIFD - 0xa405] Focal Length 35 = 28 mm
 [Exif SubIFD - 0xa406] Scene Capture Type = Standard
-[Exif SubIFD - 0xa432] Lens Specification = 4.25mm f/1.8
+[Exif SubIFD - 0xa432] Lens Specification = 17/4mm f/1.8
 [Exif SubIFD - 0xa433] Lens Make = Apple
 [Exif SubIFD - 0xa434] Lens Model = iPhone XR back camera 4.25mm f/1.8

I don't think there's a one-size-fits-all here. For focal length, decimals feel more natural. For shutter and aperture, rationals feel more natural.

To improve this I'd suggest a fix in the descriptor for only the ExifSubIFDDirectory.TAG_EXPOSURE_TIME tag.