This variant of the above code should do the conversion much faster because it completely avoids the encoding and decoding of the raw image data to PNG and back again. Note: This only works with recent JavaFX versions because it makes use of some newer features.
import org.jetbrains.skija.Canvas;
import org.jetbrains.skija.Image;
import org.jetbrains.skija.Paint;
import org.jetbrains.skija.Surface;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelBuffer;
import javafx.scene.image.PixelFormat;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SkijaFX extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
Surface surface = Surface.makeRasterN32Premul(100, 100);
Canvas canvas = surface.getCanvas();
Paint paint = new Paint();
canvas.drawCircle(50, 50, 30, paint);
Image image = surface.makeImageSnapshot();
ImageView imageView = new ImageView(new WritableImage(new PixelBuffer<>(image.getWidth(), image.getHeight(), image.peekPixels().asIntBuffer(), PixelFormat.getIntArgbPreInstance())));
StackPane root = new StackPane(imageView);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Update: In this variant the colors are wrong. The native image representation of Skia does not seem to be IntArgbPre. (Needs some more investigation.) This problem is avoided in the original version via the PNG conversion.