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

j3d

Here below is the code to generate a QR-Code with ScalaFX and 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 do I convert image to a byte array?
    Base64.encodeBase64String(bytes)
  }

  private def toWritableImage(bitMatrix: BitMatrix): WritableImage = {
    val image = new WritableImage(bitMatrix.getWidth, bitMatrix.getHeight)
    val writer = image.getPixelWriter
    val format = PixelFormat.getByteRgbInstance
    for (y <- 0 to (bitMatrix.getHeight - 1); x <- 0 to (bitMatrix.getWidth - 1)) {
      writer.setColor(x, y, if (bitMatrix.get(x, y)) Color.Black else Color.White)
    }
    image
  }
}

Since I need the QR-Code as a base64 string, I'm wondering how to convert an Image object to a byte array so that I can convert it to base64 with Apache's commons-codec.

Felix Leipold

Convert the WritabableImage to a BufferedImage using the fromFXImage-method on SwingFXUtils. The BufferedImage can in turn be written to a stream using ImageIO.write. If you pass in a ByteArrayOutputStream you can get the byte[] from the stream.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to convert byte array to buffered image

From Dev

How to convert Byte array into bitmap image?

From Dev

How to convert byte array to image in javascript

From Dev

How to convert a bitmap or byte array to an image?

From Dev

Convert byte array to Image

From Dev

How to convert byte array to Mat object in Java

From Dev

Convert byte array to object

From Dev

How to convert raster byte array of an image to original image in java?

From Dev

Convert PIL Image to byte array?

From Dev

Convert image to byte array quickly

From Dev

How to Convert Byte Array to Image inside in Linq output List

From Dev

c# How to convert pictureBox.Image to Byte Array?

From Dev

How to convert a byte-array to a Image in C#?

From Dev

How to convert a .bmp image into Byte array using C Program

From Dev

How can I convert a image into a byte array in uwp platform

From Dev

How to convert Bitmap image to byte array in wp7?

From Dev

How Can I Convert My new Byte Array To an Image

From Dev

How to convert a .bmp image into Byte array using C Program

From Dev

c# How to convert pictureBox.Image to Byte Array?

From Dev

How to convert UIImagePicker image into a byte array objective-c

From Dev

How to compress image byte[] array to JPEG/PNG and return ImageSource object

From Dev

Convert Access image OLE Object into raw image byte array in C#

From Dev

How to convert a Scala Array[Byte] to Java byte[]?

From Dev

How to pass byte array of an Image over a soap web-service(XML) and decode this byte array on android device to convert it back to an Image

From Dev

How to pass byte array of an Image over a soap web-service(XML) and decode this byte array on android device to convert it back to an Image

From Dev

How to convert wand image object to open cv image (numpy array)

From Dev

How to convert outputStream to a byte array?

From Dev

How to convert Byte array to ByteArrayOutputStream

Related Related

  1. 1

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

  2. 2

    How to convert byte array to buffered image

  3. 3

    How to convert Byte array into bitmap image?

  4. 4

    How to convert byte array to image in javascript

  5. 5

    How to convert a bitmap or byte array to an image?

  6. 6

    Convert byte array to Image

  7. 7

    How to convert byte array to Mat object in Java

  8. 8

    Convert byte array to object

  9. 9

    How to convert raster byte array of an image to original image in java?

  10. 10

    Convert PIL Image to byte array?

  11. 11

    Convert image to byte array quickly

  12. 12

    How to Convert Byte Array to Image inside in Linq output List

  13. 13

    c# How to convert pictureBox.Image to Byte Array?

  14. 14

    How to convert a byte-array to a Image in C#?

  15. 15

    How to convert a .bmp image into Byte array using C Program

  16. 16

    How can I convert a image into a byte array in uwp platform

  17. 17

    How to convert Bitmap image to byte array in wp7?

  18. 18

    How Can I Convert My new Byte Array To an Image

  19. 19

    How to convert a .bmp image into Byte array using C Program

  20. 20

    c# How to convert pictureBox.Image to Byte Array?

  21. 21

    How to convert UIImagePicker image into a byte array objective-c

  22. 22

    How to compress image byte[] array to JPEG/PNG and return ImageSource object

  23. 23

    Convert Access image OLE Object into raw image byte array in C#

  24. 24

    How to convert a Scala Array[Byte] to Java byte[]?

  25. 25

    How to pass byte array of an Image over a soap web-service(XML) and decode this byte array on android device to convert it back to an Image

  26. 26

    How to pass byte array of an Image over a soap web-service(XML) and decode this byte array on android device to convert it back to an Image

  27. 27

    How to convert wand image object to open cv image (numpy array)

  28. 28

    How to convert outputStream to a byte array?

  29. 29

    How to convert Byte array to ByteArrayOutputStream

HotTag

Archive