scala - ScalaFX: How to convert an Image object to a byte array -



scala - ScalaFX: How to convert an Image object to a byte array -

here below code generate qr-code scalafx , zxing:

import java.util.{hashmap => javahashmap} import org.apache.commons.codec.binary.base64 import scalafx.scene.image.{image, pixelformat, writableimage} import scalafx.scene.paint.color import com.google.zxing.common.bitmatrix import com.google.zxing.qrcode.qrcodewriter import com.google.zxing.qrcode.decoder.errorcorrectionlevel import com.google.zxing.{barcodeformat, encodehinttype} object qrcode { private val hints = new javahashmap[encodehinttype, any]() { put(encodehinttype.error_correction, errorcorrectionlevel.l) } def encode(text: string, size: int): string = { val bitmatrix = new qrcodewriter().encode(text, barcodeformat.qr_code, size, size, hints) val image = towritableimage(bitmatrix) val bytes = // how convert image byte array? base64.encodebase64string(bytes) } private def towritableimage(bitmatrix: bitmatrix): writableimage = { val image = new writableimage(bitmatrix.getwidth, bitmatrix.getheight) val author = image.getpixelwriter val format = pixelformat.getbytergbinstance (y <- 0 (bitmatrix.getheight - 1); x <- 0 (bitmatrix.getwidth - 1)) { writer.setcolor(x, y, if (bitmatrix.get(x, y)) color.black else color.white) } image } }

since need qr-code base64 string, i'm wondering how convert image object byte array can convert base64 apache's commons-codec.

convert writabableimage bufferedimage using fromfximage-method on swingfxutils. bufferedimage can in turn written stream using imageio.write. if pass in bytearrayoutputstream can byte[] stream.

scala scalafx

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -