【问题标题】:JavaFX : Image getting scaled to 25% and then getting printed.JavaFX:图像被缩放到 25%,然后被打印。
【发布时间】:2016-04-21 07:37:54
【问题描述】:

我正在尝试使用 JavaFX api 打印图像。不幸的是,它正在剪切图像的一部分,大约 25%,然后将其拉伸到整个 A4 页面并打印。我在打印代码上做错了什么。无论打印机是什么,我如何指示将图像适合打印页面。请告诉我。

代码:

 public void printThis() {

        System.out.println("I was called");
        // note you can use overloaded forms of the Image constructor
        // if you want to scale, etc
        String path = "resources/img/printouts/image.png";
        Image image = new Image(getClass().getResource(path).toExternalForm());
        ImageView imageView = new ImageView(image);
        new Thread(() -> printImage(imageView)).start();
    }

    public void printImage(ImageView image) {
        Printer printer = Printer.getDefaultPrinter();
        PrinterJob printJob = PrinterJob.createPrinterJob(printer);
        PageLayout pageLayout = printJob.getJobSettings().getPageLayout();
        //PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
        printJob.getJobSettings().setPageLayout(pageLayout);
        if (printJob != null) {
            boolean success = printJob.printPage(image);
            if (success) {
                printJob.endJob();
            }
        }
    }

请让我知道我做错了什么。谢谢你。 :-)

【问题讨论】:

  • 顺便说一句:if check 语句在那里没有做任何有用的事情:在上一行(成功)执行后条件始终为真。
  • 而上一行什么也不做,鉴于它之前的行......
  • 现在我在打印机旁,我测试了@fabian 发布的解决方案,它似乎确实有效。我不太清楚为什么,但也许有些关于打印分辨率的事情我不明白。
  • @james_d 是的.. 我花了大约一个小时试图解决这个问题......不幸的是没有运气......最后我明天将研究与位置无关的资源访问,即 2016 年 1 月 16 日。
  • 我想你误会了。 @fabian 发布然后删除的解决方案(也许在我的误导性评论之后)工作得很好。只需执行image.setPreserveRatio(true);image.setFitWidth(pageLayout.getPrintableHeight());image.setFitWidth(pageLayout.getPrintableWidth());。我会让他恢复他的答案。

标签: java image printing javafx image-scaling


【解决方案1】:

您可以在printImage 方法中添加以下代码:

image.setPreserveRatio(true);
image.setFitHeight(pageLayout.getPrintableHeight());
image.setFitWidth(pageLayout.getPrintableWidth());

这将打印缩放到可以放入pageLayout.getPrintableWidth() x pageLayout.getPrintableHeight() 的矩形中的最大尺寸的图像,保留比例,请参阅ImageView.preserveRation

【讨论】:

  • 这些单元是否可以正常工作? (我只是感兴趣...)PageLayout.getPrintableHeight()getPrintableWidth() 以 1/72 英寸为单位报告尺寸。大概image 尺寸以像素为单位。那么您是否必须考虑打印分辨率,或者这是否可行?
【解决方案2】:

如果我理解正确,您想在任意打印机上以任何方向、纸张尺寸等打印图像。图像不应被裁剪或按比例更改,但应尽可能多地填充纸可以吗?对吗?

所以,我做了一个简单的例子来说明你是如何做到的。您需要以宽度和高度缩放图像,但两个缩放值应该相同(保留比例)。您需要打印机的可打印区域(默认纸张大小),然后您就可以计算出正确的值。

import javafx.application.Application;
import javafx.print.PageLayout;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;

public class ImagePrint extends Application {

    @Override
    public void start(Stage stage) {
        Image image = new Image("https://openclipart.org/image/800px/svg_to_png/93337/road08.png");
        ImageView imageView = new ImageView(image);
        new Thread(() -> printImage(imageView)).start();
    }

    public void printImage(Node node) {

        Printer printer = Printer.getDefaultPrinter();
        PageLayout pageLayout = printer.getDefaultPageLayout();
        System.out.println("PageLayout: " + pageLayout);

        // Printable area
        double pWidth = pageLayout.getPrintableWidth();
        double pHeight = pageLayout.getPrintableHeight();
        System.out.println("Printable area is " + pWidth + " width and "
                + pHeight + " height.");

        // Node's (Image) dimensions
        double nWidth = node.getBoundsInParent().getWidth();
        double nHeight = node.getBoundsInParent().getHeight();
        System.out.println("Node's dimensions are " + nWidth + " width and "
                + nHeight + " height");

        // How much space is left? Or is the image to big?
        double widthLeft = pWidth - nWidth;
        double heightLeft = pHeight - nHeight;
        System.out.println("Width left: " + widthLeft
                + " height left: " + heightLeft);

        // scale the image to fit the page in width, height or both
        double scale = 0;

        if (widthLeft < heightLeft) {
            scale = pWidth / nWidth;
        } else {
            scale = pHeight / nHeight;
        }

        // preserve ratio (both values are the same)
        node.getTransforms().add(new Scale(scale, scale));

        // after scale you can check the size fit in the printable area
        double newWidth = node.getBoundsInParent().getWidth();
        double newHeight = node.getBoundsInParent().getHeight();
        System.out.println("New Node's dimensions: " + newWidth
                + " width " + newHeight + " height");

        PrinterJob job = PrinterJob.createPrinterJob();
        if (job != null) {
            boolean success = job.printPage(node);
            if (success) {
                job.endJob();
                System.exit(0);
            }
        }
    }

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

【讨论】:

  • 我没有要打印的节点。我有文件的路径,这是一个 PNG 图像。
  • 简单的放在一个节点(ImageView)中,不一定要可见。
  • 这听起来是个好主意。我将在配置后对此进行测试,并会回复您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多