如何使用javafx8在打印机图像上打印 [英] How to print on printer image using javafx8

查看:538
本文介绍了如何使用javafx8在打印机图像上打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有javafx.scene.image.Image类的对象。如何使用javafx8在打印机上打印?请注意,我不想打印某个节点,例如ImageView。我需要打印图像。虽然这是一个非常简单的问题,但我无法在互联网上找到答案。
我找到的唯一代码是:

I have object of javafx.scene.image.Image class. How can I print it on printer using javafx8? Please, note, that I don't want to print some node, for example ImageView. I need to print image. Although it's very simple question I can't find answer in internet. The only code I found is:

PrinterJob job = PrinterJob.createPrinterJob();
if (job != null) {
boolean success = job.printPage(node);
if (success) {
job.endJob();
}
}

然而,它是关于打印节点。

However it is about printing the node.

推荐答案

问题



javafx.print。 PrinterJob 仅打印Node及其子类。 图片不是子类节点。因此,您必须将其包装在Node(ImageView)中或从普通Java打印。

Problem

javafx.print.PrinterJob only prints Node and it's subclasses. Image isn't a subclass of Node. So you have to wrap it in a Node (ImageView) or print from plain Java.

主要区别在于引入了JavaFX PrinterJob以用于Node对象。它将一些有用的东西设置为JavaFX属性,如Jobstatus或Printer本身。而且它比旧的AWT PrinterJob更安全。 AWT PrinterJob可以打印任何你想要的东西,如字符串,图像,弧形等,因为它需要一个AWT图形对象来绘制页面上的东西。

The main difference is, that the JavaFX PrinterJob was introduced for usage with Node objects. It has set some usefull things as a JavaFX Property like the Jobstatus or the Printer itself. And it is more thread safe as the older AWT PrinterJob. The AWT PrinterJob can print mostly anything you want like Strings, Images, Arc's, etc., because it takes an AWT Graphics Object to draw things on the page.

在使用普通Java解决方案之前,必须使用 SwingFXUtils.fromFXImage()。但是* .jpg文件有一个错误,如下所述: https://stackoverflow.com/a/30995307/4170073

Before you can use the plain Java solution, you have to convert your FX-Image to a BufferedImage with SwingFXUtils.fromFXImage(). But there is a bug with *.jpg Files, as described here: https://stackoverflow.com/a/30995307/4170073

下面显示的最小,完整和可验证的示例一个有效的解决方案:

The Minimal, Complete, and Verifiable example down below shows a working solution:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ImagePrinter extends Application {

  @Override
  public void start(Stage primaryStage) {

    Image image = new Image("http://www.gnu.org/graphics/gnu-head.png");
    BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null);

    Button btn = new Button();
    btn.setText("Print Image");
    btn.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent event) {
        printImage(bufferedImage);
      }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Image Printer");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    launch(args);
  }

  private void printImage(BufferedImage image) {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(new Printable() {
      @Override
      public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        // Get the upper left corner that it printable
        int x = (int) Math.ceil(pageFormat.getImageableX());
        int y = (int) Math.ceil(pageFormat.getImageableY());
        if (pageIndex != 0) {
          return NO_SUCH_PAGE;
        }
        graphics.drawImage(image, x, y, image.getWidth(), image.getHeight(), null);
        return PAGE_EXISTS;
      }
    });
    try {
      printJob.print();
    } catch (PrinterException e1) {
      e1.printStackTrace();
    }
  }
}

这篇关于如何使用javafx8在打印机图像上打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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