JavaFX : Image getting scaled to 25% and then getting printed.

旧时模样 提交于 2019-11-28 06:46:02

问题


I am trying to print an image using JavaFX api's. Unfortunately, it is cutting a part of the image, approximately 25% and then stretching that to the entire A4 page, and printing it. What am I doing wrong with the print code. How can I instruct to fit the image to page for printing, no matter what the printer is. Kindly let me know.

Code :

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

Kindly let me know what am I doing wrong. Thank you. :-)


回答1:


You could add the following code to the printImage method:

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

This will print the image scaled to the largest size that can be fit into a rectangle of pageLayout.getPrintableWidth() x pageLayout.getPrintableHeight() preserving the ratio, see ImageView.preserveRation.




回答2:


If I understood it right, you want to print an image on a arbitrary printer in any orientation, paper size, etc. And the image should not be cropped or changed by it's ratio, but it should be filled as much of the paper it can? Is that correct?

So, I've made a cheap example of how you are able to do it. You need to scale the image in widht and height, but both scale values should be the same (preserve ratio). You need the printable area of the printer (with default paper size) and then you will be able to calculate the correct values.

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


来源:https://stackoverflow.com/questions/34815660/javafx-image-getting-scaled-to-25-and-then-getting-printed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!