File tree Expand file tree Collapse file tree
main/kotlin/com/almasb/fxgl/texture
test/kotlin/com/almasb/fxgl/texture Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -17,8 +17,10 @@ import javafx.scene.effect.BlendMode
1717import javafx.scene.image.*
1818import javafx.scene.paint.Color
1919import javafx.scene.shape.Rectangle
20+ import java.io.ByteArrayOutputStream
2021import java.nio.file.Files
2122import java.nio.file.Path
23+ import java.util.Base64
2224import java.util.concurrent.Callable
2325import javax.imageio.ImageIO
2426import kotlin.math.abs
@@ -608,6 +610,20 @@ fun Image.subImage(area: Rectangle2D): Image {
608610 return newImage
609611}
610612
613+ fun Image.toByteArray (): ByteArray {
614+ val awtImage = toBufferedImage(this )
615+
616+ val baos = ByteArrayOutputStream ()
617+
618+ ImageIO .write(awtImage, " png" , baos)
619+
620+ return baos.toByteArray()
621+ }
622+
623+ fun Image.toBase64 (): String {
624+ return Base64 .getEncoder().encodeToString(this .toByteArray())
625+ }
626+
611627/* *
612628 * @return the sum of rgb values, which is in range [0..3]
613629 */
Original file line number Diff line number Diff line change @@ -105,4 +105,15 @@ class ImagesTest {
105105
106106 assertThat(result, `is `(0.0 ))
107107 }
108+
109+ @Test
110+ fun `Convert to base64` () {
111+ val img1 = ColoredTexture (100 , 100 , Color .WHITE ).image
112+
113+ val s = img1.toBase64()
114+
115+ assertThat(s, `is `(
116+ " iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAA/ElEQVR4Xu3RoQEAMAyAsP7/dOv3wBCJxDJLyryBvwyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTGkBhDYgyJMSTmAPgrrNZaawxDAAAAAElFTkSuQmCC"
117+ ))
118+ }
108119}
You can’t perform that action at this time.
0 commit comments