Reputation: 1
How i can save an javafx.image.Image to a jpg file in a javafxports android app? I can't find an api the only i have founded is ImageIO that is not supported on android. i need some help Example code:
@Override public void start(Stage primaryStage) {
StackPane root = new StackPane();
Scene scene = new Scene(root, 400, 450);
WritableImage wim = new WritableImage(300, 250);
Canvas canvas = new Canvas(300, 250);
GraphicsContext gc = canvas.getGraphicsContext2D();
drawShapes(gc);
canvas.snapshot(null, wim);
root.getChildren().add(canvas);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
File file = new File("CanvasImage.png");
try {
//on desktop ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
// on android ??????????
} catch (Exception s) {
}
}
Upvotes: 0
Views: 512
Reputation: 11
On Android you can use android.graphics.Bitmap to save to file:
public void saveImageToPngFile(File file, WritableImage image) {
int width = (int) image.getWidth();
int height = (int) image.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
try {
PixelReader pr = image.getPixelReader();
IntBuffer buffer = IntBuffer.allocate(width * height);
pr.getPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(), buffer, width);
bitmap.setPixels(buffer.array(), 0, width, 0, 0, width, height);
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1