From a76218a26435eef86296666073a5a1b9d04f4931 Mon Sep 17 00:00:00 2001 From: Amin Bouzerda Date: Thu, 18 Jul 2024 13:21:11 +0200 Subject: [PATCH] Added BufferedImage constructor to ImageVisual. --- .../jvmMain/kotlin/mapper/ComponentMapper.kt | 2 +- .../tools/aqua/bgw/visual/ImageVisual.kt | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/bgw-gui/src/jvmMain/kotlin/mapper/ComponentMapper.kt b/bgw-gui/src/jvmMain/kotlin/mapper/ComponentMapper.kt index c990901cc..2ab69f144 100644 --- a/bgw-gui/src/jvmMain/kotlin/mapper/ComponentMapper.kt +++ b/bgw-gui/src/jvmMain/kotlin/mapper/ComponentMapper.kt @@ -438,7 +438,7 @@ object VisualMapper { //TODO: Check properly if path is URL or local path private fun isRelativeFilePath(path: String): Boolean { - return !path.startsWith("http://") && !path.startsWith("https://") + return !path.startsWith("http://") && !path.startsWith("https://") && !path.startsWith("data:image/png;base64,") } } diff --git a/bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/visual/ImageVisual.kt b/bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/visual/ImageVisual.kt index 4dbe7a176..6f7670783 100644 --- a/bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/visual/ImageVisual.kt +++ b/bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/visual/ImageVisual.kt @@ -19,9 +19,13 @@ package tools.aqua.bgw.visual -import tools.aqua.bgw.io.BufferedImage -import tools.aqua.bgw.io.File -import tools.aqua.bgw.observable.properties.Property +import java.awt.image.BufferedImage +import java.io.ByteArrayOutputStream +import java.io.File +import java.util.* +import javax.imageio.ImageIO +import kotlin.math.min + /** * Visual showing an [Image]. @@ -47,6 +51,22 @@ open class ImageVisual( val offsetX: Int = 0, val offsetY: Int = 0 ) : SingleLayerVisual() { + constructor( + image: BufferedImage, + width: Int = -1, + height: Int = -1, + offsetX: Int = 0, + offsetY: Int = 0, + ) : this(toDataURI(image), width, height, offsetX, offsetY) + + companion object { + fun toDataURI(image: BufferedImage) : String { + val baos = ByteArrayOutputStream() + ImageIO.write(image, "png", baos) + val base64 = Base64.getEncoder().encodeToString(baos.toByteArray()) + return "data:image/png;base64,$base64" + } + } // TODO - Reimplement