From 3ecdf21296a290eb0044f156af29b4e648fecb5e Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Fri, 24 May 2024 22:07:13 +0300 Subject: [PATCH] [Feature] [ALL] Implement image metadata extraction --- .../imagetagger/core/ImageMetadata.kt | 84 ++++++++++++++++++- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/ImageMetadata.kt b/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/ImageMetadata.kt index 6e861c1..17dd7f6 100644 --- a/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/ImageMetadata.kt +++ b/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/ImageMetadata.kt @@ -3,15 +3,20 @@ package dev.nuculabs.imagetagger.core import com.drew.imaging.ImageMetadataReader import com.drew.metadata.Metadata import com.drew.metadata.exif.ExifIFD0Directory +import com.drew.metadata.exif.ExifSubIFDDirectory import java.io.File -// TODO: Lens Model, Camera Brand, Camera Model, ISO, Aperture, Shutter Speed - /** * Interface that holds common image metadata objects used by this application. */ interface IImageMetadata { val artist: String + val aperture: String + val shutterSpeed: String + val iso: String + val lensModel: String + val cameraModel: String + val cameraBrand: String } /** @@ -19,17 +24,88 @@ interface IImageMetadata { */ class ImageMetadata internal constructor(file: File) : IImageMetadata { private var metadata: Metadata? = null + private lateinit var _cameraBrand: String + private lateinit var _cameraModel: String + private lateinit var _lensModel: String + private lateinit var _artist: String + private lateinit var _aperture: String + private lateinit var _iso: String + private lateinit var _shutterSpeed: String init { metadata = ImageMetadataReader.readMetadata(file) + if (metadata != null) { + val exifDirectory = metadata?.getFirstDirectoryOfType(ExifIFD0Directory::class.java) + val exifSubDirectory = metadata?.getFirstDirectoryOfType(ExifSubIFDDirectory::class.java) + + _cameraBrand = exifDirectory?.getString(ExifIFD0Directory.TAG_MAKE) ?: "Unknown" + _cameraModel = exifDirectory?.getString(ExifIFD0Directory.TAG_MODEL) ?: "Unknown" + _lensModel = exifSubDirectory?.getString(ExifIFD0Directory.TAG_LENS_MODEL) ?: "Unknown" + _artist = exifDirectory?.getString(ExifIFD0Directory.TAG_ARTIST) ?: "Unknown" + _aperture = exifSubDirectory?.getString(ExifIFD0Directory.TAG_FNUMBER) ?: "Unknown" + _iso = exifSubDirectory?.getString(ExifIFD0Directory.TAG_ISO_EQUIVALENT) ?: "Unknown" + _shutterSpeed = exifSubDirectory?.getString(ExifIFD0Directory.TAG_EXPOSURE_TIME) ?: "Unknown" + } + } + override val cameraBrand: String + get() { + if (metadata == null) { + return "Unknown" + } + return _cameraBrand + } + + override val cameraModel: String + get() { + if (metadata == null) { + return "Unknown" + } + return _cameraModel + } + + override val lensModel: String + get() { + if (metadata == null) { + return "Unknown" + } + return _lensModel + } + override val artist: String get() { if (metadata == null) { return "Unknown" } - val exifDirectory = metadata?.getFirstDirectoryOfType(ExifIFD0Directory::class.java) - return exifDirectory?.getString(ExifIFD0Directory.TAG_ARTIST) ?: "Unknown" + return _artist } + + override val aperture: String + get() { + if (metadata == null) { + return "Unknown" + } + return _aperture + } + + override val iso: String + get() { + if (metadata == null) { + return "Unknown" + } + return _iso + } + + override val shutterSpeed: String + get() { + if (metadata == null) { + return "Unknown" + } + return _shutterSpeed + } + + override fun toString(): String { + return "ImageMetadata(cameraBrand='$cameraBrand', cameraModel='$cameraModel', lensModel='$lensModel', artist='$artist', aperture='$aperture', iso='$iso', shutterSpeed='$shutterSpeed')" + } }