diff --git a/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/AnalyzedImage.kt b/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/AnalyzedImage.kt index 4bb0c04..75befa9 100644 --- a/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/AnalyzedImage.kt +++ b/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/AnalyzedImage.kt @@ -1,7 +1,5 @@ package dev.nuculabs.imagetagger.core -import com.drew.imaging.ImageMetadataReader -import com.drew.metadata.Metadata import dev.nuculabs.imagetagger.core.abstractions.IImageTagsPrediction import java.awt.image.BufferedImage import java.io.File @@ -15,10 +13,10 @@ import javax.imageio.ImageIO class AnalyzedImage(private val file: File, imageTagsPrediction: IImageTagsPrediction) { private var imageFile: File = file - private var imageMetadata: Metadata? = null private var bufferedImage: BufferedImage? = null private var predictedTags: List = emptyList() private val logger: Logger = Logger.getLogger("AnalyzedImage") + private lateinit var imageMetadata: IImageMetadata private var error: String = "" private var hasError: Boolean = false @@ -30,6 +28,7 @@ class AnalyzedImage(private val file: File, imageTagsPrediction: IImageTagsPredi imageFile = File(file.absolutePath) bufferedImage = ImageIO.read(imageFile) predictedTags = imageTagsPrediction.predictTags(bufferedImage!!) + imageMetadata = ImageMetadata(imageFile) } catch (e: NullPointerException) { val message = "Error while predicting image: invalid image type or type not supported." logger.warning(message) @@ -46,13 +45,8 @@ class AnalyzedImage(private val file: File, imageTagsPrediction: IImageTagsPredi /** * Returns an image's metadata */ - fun metadata(): ImageMetadata { - synchronized(this) { - if (imageMetadata == null) { - imageMetadata = ImageMetadataReader.readMetadata(imageFile) - } - return ImageMetadata(imageMetadata) - } + fun metadata(): IImageMetadata { + return imageMetadata } /** 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 92947b9..6e861c1 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 @@ -1,17 +1,35 @@ package dev.nuculabs.imagetagger.core +import com.drew.imaging.ImageMetadataReader import com.drew.metadata.Metadata import com.drew.metadata.exif.ExifIFD0Directory +import java.io.File // TODO: Lens Model, Camera Brand, Camera Model, ISO, Aperture, Shutter Speed -class ImageMetadata internal constructor(private val metadata: Metadata?) { +/** + * Interface that holds common image metadata objects used by this application. + */ +interface IImageMetadata { val artist: String +} + +/** + * An image metadata provider that uses + */ +class ImageMetadata internal constructor(file: File) : IImageMetadata { + private var metadata: Metadata? = null + + init { + metadata = ImageMetadataReader.readMetadata(file) + } + + override val artist: String get() { if (metadata == null) { return "Unknown" } - val exifDirectory = metadata.getFirstDirectoryOfType(ExifIFD0Directory::class.java) + val exifDirectory = metadata?.getFirstDirectoryOfType(ExifIFD0Directory::class.java) return exifDirectory?.getString(ExifIFD0Directory.TAG_ARTIST) ?: "Unknown" } }