【问题标题】:Can JavaFX images be saved larger than it is?JavaFX 图像可以保存得比实际更大吗?
【发布时间】:2015-03-21 13:19:09
【问题描述】:

我决定制作一个应用程序,该应用程序基本上可以根据输入的测量值在画布上为人们绘制特定图像。然后,我希望该人能够以更大的格式打印出这张图片(或保存为更大的 JPEG、PNG、PDF 文件)。

所以在屏幕上它基本上是一个像素的问题,但在打印上它会是英寸长,如果这一切都有意义吗?

这可能吗?

谢谢!

【问题讨论】:

    标签: canvas printing javafx resize


    【解决方案1】:

    我已经想出了如何缩放图像以将其保存为更大的格式,所以这是为那些仍在努力解决问题的人准备的。

        // Create the canvas
        Stage primaryStage = new Stage();
        Group root = new Group();
        Canvas canvas = new Canvas(500, 600);              
        GraphicsContext gc = canvas.getGraphicsContext2D();       
        drawShapes(gc);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    
        /* If you're going to scale your image by 2, 
        * for example, make sure your image size is large 
        * enough to contain the new image. As you can see, 
        * the image size is double the canvas size. */
    
        WritableImage wim = new WritableImage(1000, 1200);
        SnapshotParameters spa = new SnapshotParameters();
    
        // Scale the x and y axis by a factor of 2
    
        spa.setTransform(Transform.scale(2, 2));
        canvas.snapshot(spa, wim);
    
        File file = new File("CanvasImage.png");
    
        try {
            ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
        } catch (Exception s) {
        }
    

    【讨论】:

    • 您成就了我的一天(可能在不知情的情况下:-)),因为您描述的相同技术也适用于从带有 Retina 显示屏的 Mac 上的画布上获取清晰的快照。在这种情况下,直接创建快照只会导致图像模糊。
    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 2012-08-16
    • 1970-01-01
    • 2015-09-05
    • 2018-07-27
    • 2023-03-12
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多