display an error when image fails to be analyzed

This commit is contained in:
Denis-Cosmin Nutiu 2024-04-27 23:50:33 +03:00
parent cb00500705
commit 48076c69a5
5 changed files with 45 additions and 12 deletions

View file

@ -15,6 +15,7 @@ class AnalyzedImage(private val file: File, imageTagsPrediction: IImageTagsPredi
private var predictedTags: List<String> = emptyList()
private val logger: Logger = Logger.getLogger("AnalyzedImage")
private var error: String = ""
private var hasError: Boolean = false
/**
* Initializes the analyzed image and predicts its tags.
@ -23,24 +24,39 @@ class AnalyzedImage(private val file: File, imageTagsPrediction: IImageTagsPredi
try {
bufferedImage = ImageIO.read(File(file.absolutePath))
predictedTags = imageTagsPrediction.predictTags(bufferedImage!!)
} catch (e: NullPointerException) {
val message = "Error while predicting image: invalid image type or type not supported."
logger.warning(message)
hasError = true
error = message
} catch (e: Exception) {
logger.warning("Error while predicting images $e")
val message = "Error loading image $e"
logger.warning(message)
hasError = true
error = e.message.toString()
}
}
/**
* Returns a boolean indicating if the image analysis has errors.
* The flag is `True` if it has errors, `False` otherwise.
*/
fun hasError(): Boolean {
return hasError
}
/**
* Returns the prediction error
*/
fun errorMessage(): String {
return error;
return error
}
/**
* Returns the absolute file path of the image.
*/
fun absolutePath(): String {
return file.absolutePath;
return file.absolutePath
}
/**

View file

@ -60,8 +60,13 @@ class ImageTagsEntryControl(private val image: AnalyzedImage) : HBox() {
} catch (exception: IOException) {
throw RuntimeException(exception)
}
setImage(image.absolutePath())
setText(image.tags())
setImage(image)
if (image.hasError()) {
setText(listOf(image.errorMessage()))
} else {
setText(image.tags())
}
setupEventHandlers()
}
@ -95,13 +100,20 @@ class ImageTagsEntryControl(private val image: AnalyzedImage) : HBox() {
/**
* Setter for setting the image.
*/
private fun setImage(imagePath: String) {
val file = File(imagePath)
file.inputStream().use {
imageView.image = Image(it, 244.0, 244.0, true, true)
private fun setImage(analyzedImage: AnalyzedImage) {
val file = File(analyzedImage.absolutePath())
if (analyzedImage.hasError()) {
fileNameLabel.text = "Failed: ${file.name}"
fileNameLabel.styleClass.add("errorLabel")
imageView.image = Image(this.javaClass.getResourceAsStream("images/failed.png"))
imageView.isCache = true
} else {
file.inputStream().use {
imageView.image = Image(it, 244.0, 244.0, true, true)
imageView.isCache = true
}
fileNameLabel.text = "File: ${file.name}"
}
fileNameLabel.text = "File: ${file.name}"
}
/**

View file

@ -0,0 +1,5 @@
.errorLabel {
-fx-text-background-color: red;
-fx-font-family: "Inter Bold"
}

View file

@ -6,8 +6,8 @@
<?import javafx.scene.image.ImageView?>
<?import org.kordamp.ikonli.javafx.FontIcon?>
<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml">
<StackPane style="-fx-background-color: lightgray;" prefWidth="244" prefHeight="244">
<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml" stylesheets="@image-tags-entry.css">
<StackPane prefWidth="244" prefHeight="244">
<ImageView fx:id="imageView"/>
</StackPane>
<VBox>

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB