JavaFX:图像缩放到25%然后打印。 [英] JavaFX : Image getting scaled to 25% and then getting printed.

查看:811
本文介绍了JavaFX:图像缩放到25%然后打印。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

代码:

 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. :-)

推荐答案

如果我理解正确,你想在任意方向,纸张大小的任意打印机上打印图像并且图像不应该按照它的比例裁剪或改变,但它应该尽可能多地填充纸张?这是正确的吗?

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?

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

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

这篇关于JavaFX:图像缩放到25%然后打印。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆