简体   繁体   English

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

[英]How to print on printer image using javafx8

I have object of javafx.scene.image.Image class. 我有javafx.scene.image.Image类的对象。 How can I print it on printer using javafx8? 如何使用javafx8在打印机上打印? Please, note, that I don't want to print some node, for example ImageView. 请注意,我不想打印某些节点,例如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. 但是,这与打印节点有关。

Problem 问题

javafx.print. javafx.print。 PrinterJob only prints Node and it's subclasses. PrinterJob仅打印Node及其子类。 Image isn't a subclass of Node. 图像不是Node的子类。 So you have to wrap it in a Node (ImageView) or print from plain Java. 因此,您必须将其包装在Node(ImageView)中或从纯Java打印。

Difference of JavaFX-PrinterJob and AWT-PrinterJob JavaFX-PrinterJob和AWT-PrinterJob的区别

The main difference is, that the JavaFX PrinterJob was introduced for usage with Node objects. 主要区别在于,引入了JavaFX PrinterJob以便与Node对象一起使用。 It has set some usefull things as a JavaFX Property like the Jobstatus or the Printer itself. 它已将一些有用的东西设置为JavaFX属性,例如Jobstatus或Printer本身。 And it is more thread safe as the older AWT PrinterJob. 而且,与旧的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. AWT PrinterJob几乎可以打印任何您想要的内容,例如字符串,图像,弧等,因为它需要AWT图形对象在页面上绘制内容。

Solution

Before you can use the plain Java solution, you have to convert your FX-Image to a BufferedImage with SwingFXUtils.fromFXImage() . 在使用纯Java解决方案之前,您必须使用SwingFXUtils.fromFXImage()将FX-Image转换为BufferedImage。 But there is a bug with *.jpg Files, as described here: https://stackoverflow.com/a/30995307/4170073 但是* .jpg文件存在错误,如此处所述: 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();
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM