[Feature] [ALL] Abstract ImageMetadata

This commit is contained in:
Denis-Cosmin Nutiu 2024-05-19 12:14:34 +03:00
parent 7539d3dadf
commit f17b8ff262
2 changed files with 24 additions and 12 deletions

View file

@ -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<String> = 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
}
/**

View file

@ -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"
}
}