【问题标题】:Java : Get image as a BufferedImage from classpathJava:从类路径获取图像作为 BufferedImage
【发布时间】:2016-01-15 15:26:01
【问题描述】:

我正在处理一个 JavaFX 项目,我必须在其中打印一些我有路径的图像。现在的问题是,我想将图像作为 BufferedImage 获取,然后调用我拥有的打印函数。我怎样才能做到这一点。任何帮助都会很好。

代码:

public void printThis(String localPath){

       // What should I do here? 

    System.out.println("Lets print this. ");
}


private void printImage(BufferedImage image) {
    java.awt.print.PrinterJob printJob = java.awt.print.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;
        }
    });

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            try {
                printJob.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    });
}

编辑:

更新打印功能:

public void printImage(ImageView image) {
        Printer printer = Printer.getDefaultPrinter();
        PrinterJob printJob = PrinterJob.createPrinterJob(printer);
        PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
        if (printJob != null) {
            boolean success = printJob.printPage(image);
            if (success) {
                printJob.endJob();
            }
        }
    }

图像在类路径中。请告诉我。谢谢你。 :-)

【问题讨论】:

  • 您能否详细说明“图像在类路径中”以及在那种情况下localPath 参数是什么?您的意思是要将图像与您为应用程序创建的 jar 文件捆绑在一起吗?
  • @James_D : 我的项目架构是src-->Main.java(这里是打印函数),src包含目录资源-->img-->abc.png。现在我想打印这个 abc.png
  • 所以图像是相对于类路径部署为/img/abc.png,还是/resources/img/abc.png?那是("/img/abc.png" 或"/resources/img/abc.png")作为localPath 传入的值吗?

标签: java printing javafx


【解决方案1】:

我不太清楚参数包含什么,但基本思想是

public void printThis(String localPath) {
    try {
        InputStream in = getClass().getResourceAsStream(localPath);
        BufferedImage image = ImageIO.read(in);
        printImage(image);
    } catch (Exception exc) {
        exc.printStackTrace();
        // handle elegantly...
    }
}

这假定localPath 包含定义为here 的资源名称,或者相对于当前类(但请注意,路径的所有组件都必须是有效的Java 标识符,因此没有.. 等),或绝对(即相对于类路径)如果它以/ 开头。

不过,由于您正在编写 JavaFX 项目,因此使用 JavaFX API 肯定更有意义。另请注意,无需在 FX 应用程序线程上进行打印,并且可能不建议这样做。所以你可以这样做

public void printThis(String localPath) {

    // note you can use overloaded forms of the Image constructor
    // if you want to scale, etc
    Image image = new Image(getClass().getResource(localPath).toExternalForm());
    new Thread(() -> printImage(image)).start();
}

public void printImage(Image image) {
    ImageView imageView = new ImageView(image);
    PrinterJob printJob = PrinterJob.createPrinterJob();
    if (printJob != null) {
        // scale image if necessary by using imageView.setPreserveRatio(true)
        // along with imageView.setFitWidth(...) and imageView.setFitHeight(...)
        boolean success = printJob.printPage(imageView);
        if (success) {
            printJob.endJob();
        }
    }
}

【讨论】:

  • 图片只为前25%,我想打印整张图片。你知道我必须在 printImage() 方法中改变什么吗?
  • 什么意思?图片对页面来说太大了吗?
  • 不,上面的方法是将图像从顶部缩放到 25%。因此,如果将图像分成 4 个部分,则只有一个部分被打印出来。我认为 printImage 代码正在这样做。用 graphics.DrawImage().
  • 不知道为什么会这样。顺便说一句,如果这是一个 JavaFX 项目,为什么要使用 AWT 类?为什么不使用javafx.scene.image 和javafx.print API?
  • 知道如何使用 JavaFX api 打印 BufferedImage 吗?请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
相关资源
最近更新 更多