0

i'am using JavaFX and illustrate a Chart with the LineChart concept in Javafx. If i draw a chart, i export a screenshot of this with this code.

WritableImage image = lc.snapshot(new SnapshotParameters(), null);  
File file = new File("Chart.png");       

try {
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} 
        catch (IOException e) {
        //bla
}

This works perfect!

Now: is there a simple way to create this "WritableImage" image to a Base64 String? Furthermore i want to use it to reproduce this Base64-String to a PNG file in PHP.

Any Ideas? THX

Anton Krosnev
  • 3,964
  • 1
  • 21
  • 37
Denis
  • 439
  • 4
  • 17

2 Answers2

1

Your code need to continue with something like this:

        //File file = new File("Chart.png"); -> this is already there
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int read= 0;
        while( (read = fis.read(buffer)) > -1){ 
            baos.write(buffer, 0, read);
        }
        fis.close();
        baos.close();
        byte pgnBytes [] = baos.toByteArray();
        Base64.Encoder base64_enc = Base64.getEncoder();
        String base64_image = base64_enc.encodeToString(pgnBytes);

This can be optimized further to write the file directly into byte array, if you do not need the graphic stored into file:

            WritableImage image = lc.snapshot(new SnapshotParameters(), null);  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
                ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", baos);
        } 
                catch (IOException e) {
                //bla
        } 
        byte pgnBytes [] = baos.toByteArray();
        Base64.Encoder base64_enc = Base64.getEncoder();
        String base64_image = base64_enc.encodeToString(pgnBytes);
     }

In both cases image is stored in memory, which can cause OutOfMemory Error, if the image is too large etc.

Anton Krosnev
  • 3,964
  • 1
  • 21
  • 37
  • It still works with both... into file, and Base64-String: `ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file); ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", baos);` – Denis Jul 14 '15 at 15:35
1

SwingFXUtils.fromFXImage() returns a BufferedImage which can be easily converted to a Base64-String using BASE64.Encoder introduced in Java 8.

A complete working example :

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox box = new VBox();
        box.setStyle("-fx-background-color:RED;");
        Scene scene = new Scene(box, 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
        createEncodedString(box);
    }

    private void createEncodedString(Node node) {
        WritableImage image = node.snapshot(new SnapshotParameters(), null);
        String base64String = encodeImageToString(SwingFXUtils.fromFXImage(image, null), "png");
        System.out.println("Base64 String : " + base64String);
    }

    private String encodeImageToString(BufferedImage image, String type) {
        String imageString = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        try {
            ImageIO.write(image, type, bos);
            byte[] imageBytes = bos.toByteArray();
            imageString = Base64.getEncoder().encodeToString(imageBytes);
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageString;
    }
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176