JavaFX:图像缩放到25%然后打印。

我正在尝试使用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(); } } } 

请让我知道我做错了什么。 谢谢。 🙂

您可以将以下代码添加到printImage方法:

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

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

如果我理解正确,你想要在任意方向,纸张尺寸等任意打印机上打印图像。图像不应该被裁剪或改变它的比例,但它应该填充尽可能多的纸张? 那是对的吗?

所以,我已经做了一个很便宜的例子,告诉你如何做到这一点。 您需要在widht和height中缩放图像,但两个缩放值应该相同(保留比率)。 您需要打印机的可打印区域(使用默认纸张尺寸),然后您才能计算出正确的值。

 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("http://sofzh.miximages.com/java/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); } }