简体   繁体   中英

Pixelated result when trying to print a Node including an image from JavaFx

i stumbled upon a problem trying to print (to a printer or a pdf) an image through JavaFx. My code works and prints, but pixelates (downgrades quality) on every picture that i try to print.

My code is

 package testingprinting; import java.io.File; import java.net.MalformedURLException; import java.util.Iterator; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.print.PageLayout; import javafx.print.PageOrientation; import javafx.print.Paper; import javafx.print.Printer; import javafx.print.PrinterJob; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.transform.Scale; import javafx.stage.Stage; /** * * @author me */ public class TestingPrinting extends Application { @Override public void start(Stage primaryStage) throws MalformedURLException { GridPane leftGridPane = new GridPane(); leftGridPane.getColumnConstraints().add(new ColumnConstraints(25)); leftGridPane.getColumnConstraints().add(new ColumnConstraints(25)); leftGridPane.getColumnConstraints().add(new ColumnConstraints(25)); leftGridPane.getColumnConstraints().add(new ColumnConstraints(25)); leftGridPane.getColumnConstraints().add(new ColumnConstraints(25)); leftGridPane.getColumnConstraints().add(new ColumnConstraints(25)); leftGridPane.getColumnConstraints().add(new ColumnConstraints(25)); leftGridPane.getColumnConstraints().add(new ColumnConstraints(25)); leftGridPane.getColumnConstraints().add(new ColumnConstraints(824)); VBox root = new VBox(1); root.getChildren().add(leftGridPane); Scene scene = new Scene(root, 1024, 800); primaryStage.setTitle("Print Testing"); primaryStage.setScene(scene); primaryStage.show(); Image testImage = new Image(new File("C:\\\\Users\\\\me\\\\Documents\\\\800px-Gull_portrait_ca_usa.JPG").toURI().toURL().toExternalForm()); ImageView testImageImageView = new ImageView(); testImageImageView.setFitWidth(800); testImageImageView.setFitHeight(600); testImageImageView.setImage(testImage); leftGridPane.add(testImageImageView,0,0,9,1); Button printButton = new Button("Print"); root.getChildren().add(printButton); printButton.setOnAction(e -> { //Printer pdfPrinter = null; Iterator<Printer> iter = Printer.getAllPrinters().iterator(); while (iter.hasNext()) { Printer printer = iter.next(); if (printer.getName().endsWith("PDF")) { // pdfPrinter = printer; } } Printer printer = Printer.getDefaultPrinter(); PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT); double scaleX = pageLayout.getPrintableWidth() / leftGridPane.getBoundsInParent().getWidth(); double scaleY = pageLayout.getPrintableHeight() / leftGridPane.getBoundsInParent().getHeight(); //leftGridPane.getTransforms().add(new Scale(scaleX, scaleY)); PrinterJob job = PrinterJob.createPrinterJob(); if(job != null){ System.out.println("Job is not null"); job.showPrintDialog(primaryStage); // Window must be your main Stage job.printPage(leftGridPane); job.endJob(); } else { job.cancelJob(); System.out.println("Job is null");} } ); } public static void main(String[] args) { launch(args); } } 

and as you can see on the picture that i snipped 1. is from printing and the image is pixelated and 2. is from the code that i run and it is not pixelated. side by side comparison

does any one has an idea how to solve this?

Thank you in advance.

What you could try is setting the print quality to high on the printer job. After you create the printer job call

job.getJobSettings().setPrintQuality(PrintQuality.HIGH);

I haven't had time to try it but it might help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM