Skip to content

Commit

Permalink
Added BufferedImage constructor to ImageVisual.
Browse files Browse the repository at this point in the history
  • Loading branch information
abouzerda committed Jul 18, 2024
1 parent 6033021 commit a76218a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bgw-gui/src/jvmMain/kotlin/mapper/ComponentMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,")
}
}

Expand Down
26 changes: 23 additions & 3 deletions bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/visual/ImageVisual.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -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

Expand Down

0 comments on commit a76218a

Please sign in to comment.