尝试从JavaFx打印包含图像的节点时出现像素化结果

时间:2019-06-05 08:44:01

标签: java javafx printing

我偶然发现了尝试通过JavaFx打印(到打印机或pdf)图像的问题。 我的代码可以工作并打印,但是在我尝试打印的每张图片上像素化(降低了质量)。

我的代码是

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);
    }
    
}

正如您在图片上看到的,我剪断了1.来自打印和图像的像素化,而2.来自我运行的代码,但没有像素化。 side by side comparison

有人知道如何解决吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试在打印机作业上将打印质量设置为高。创建打印机作业调用之后

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

我还没来得及尝试,但这可能会有所帮助。