且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用javafx8在打印机图像上打印

更新时间:2023-09-19 21:35:46

问题



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://***.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://***.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();
    }
  }
}