user7467082
user7467082

Reputation:

How to convert Image object to Base64 image in java?

I have an Image object created using

SnapshotParameters para = new SnapshotParameters();
para.setFill(Color.TRANSPARENT);
Image img = myStackPane.snapshot(para, null);

Now, I want to convert it to Base64 image to put it into javafx WebView. But I did not find any method to do it. Can anybody help me please?

Upvotes: 1

Views: 1679

Answers (1)

MBec
MBec

Reputation: 2210

Convert Image to byte array such as in this answer. Then use any Base64 library to encode. For example Apache Commons.

EDIT

BufferedImage bImage = SwingFXUtils.fromFXImage(logo.getImage(), null);
ByteArrayOutputStream s = new ByteArrayOutputStream();
ImageIO.write(bImage, "png", s);
byte[] res  = s.toByteArray()
s.close();
Base64.encode(res);

Upvotes: 4

Related Questions