JavaFX可打印到图像非常像素,但完美的打印机 [英] JavaFX Printable to Image very pix-elated, but perfect to printer

查看:886
本文介绍了JavaFX可打印到图像非常像素,但完美的打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可打印的对象可以完美打印到打印机,但是当我将它打印到图像时,它是非常像素化的?

I have a printable object that prints perfectly fine to a printer, but when i print it to an Image, then it is very pixelated?

这是我的代码设置打印机进行打印和图像

Here is my code to setup a printer for printing and to an image

private PageFormat setupPrinter() {
    // sizing (standard is 72dpi, so multiple inches by this)
    Double height = 4d * 72d;
    Double width = 3d * 72d;
    Double margin = 0.1d * 72d;

    // now lets print it
    AttributeSet attributes = new HashAttributeSet();
    attributes.add(new Copies(1));

    PrinterJob printJob = PrinterJob.getPrinterJob();
    PageFormat pageFormat = printJob.defaultPage();

    // if there are more than 10 items, up the paper size to 6inch. 
    // This will handle up to 24 different items
    if (1 == 2) height = 6d * 72d;

    // set page size
    Paper paper = pageFormat.getPaper();

    paper.setSize(width, height);
    paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2));

    // set orientation and paper
    pageFormat.setOrientation(PageFormat.PORTRAIT);
    pageFormat.setPaper(paper);

    return pageFormat;
}

private void printToImage() {

    MyPrintable myPrintable = new MyPrintable();

    // setup our printer
    PageFormat pageFormat = setupPrinter();

    // set size of imageView, using the hieght and width values
    double imageHeight = pageFormat.getHeight();
    double imageWidth = pageFormat.getWidth();

    // create our image/graphics
    BufferedImage image = new BufferedImage((int)imageWidth, (int)imageHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = image.getGraphics();

    // color background white
    graphics.setColor(Color.WHITE);
    ((Graphics2D) graphics).fill(new Rectangle.Float(0, 0, (float)imageWidth, (float)imageHeight));

    // directly call our print method, passing graphics, pageFormat and pageIndex
    try {
        myPrintable.print(graphics, pageFormat, 0);
    } catch (PrinterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    graphics.dispose();

    // set our image
    imgReport.setImage(SwingFXUtils.toFXImage(image, null));

    // now lets show the preview pane
    reportPane.setVisible(true);
}

public void printToPrinter(ActionEvent event) {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    PageFormat pageFormat = setupPrinter();

    printJob.setPrintable(new MyPrintable(), pageFormat);

    try {
        printJob.print();
    } catch (Exception e) {
        e.printStackTrace();    
    }

}

public MyPrintable() {
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        // user (0,0) is typically outside the imageable area, so we must translate
        // by the X and Y values in the pageFormat to avoid clipping
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // add text etc here
        ........
    }
}

以下是像素化的示例。它完全适合图像....

Here is an example of how pixelated it is. Bit it fits in the image perfectly....

推荐答案

您可以使用控制渲染质量
读取此内容:

You can use a controlling rendering quality read this:

https://docs.oracle .com / javase / tutorial / 2d / advanced / quality.html

您的代码示例:

Graphics2D graphics = (Graphics2D)image.getGraphics();
RenderingHints rh = new RenderingHints(
         RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
graphics.setColor(Color.WHITE);
graphics.fill(new Rectangle.Float(0, 0, (float)imageWidth, (float)imageHeight));

这篇关于JavaFX可打印到图像非常像素,但完美的打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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