用Java打印BufferedImage的正确方法 [英] Proper way of printing a BufferedImage in Java

查看:677
本文介绍了用Java打印BufferedImage的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有适当的方式在Java中打印 BufferedImage
基本上我创建了一个可以正常工作的照片处理程序,我可以保存图像等
但是我的真正目标是将其发送到打印机软件,以便您可以选择所需的页数打印和页面类型。

所以我缩短的问题是,如何将缓冲图像发送到打印机,以便打印机选择屏幕将弹出等,然后能够打印?



如果任何人都可以给我一个这样的例子,它将不胜感激。

解决方案

这是我的一个Java项目中的一个。此代码将缩放并在打印机页面上打印一张图片。



您称之为:

<$ p $ $

$ b $ public $ action $ (new PrintActionListener(image))。start();
}
});

以下是Runnable:

  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;

public class PrintActionListener实现Runnable {

private BufferedImage image;

public PrintActionListener(BufferedImage image){
this.image = image;
}

@Override
public void run(){
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(new ImagePrintable(printJob,image));

if(printJob.printDialog()){
try {
printJob.print();
} catch(PrinterException prt){
prt.printStackTrace();




public class ImagePrintable implements Printable {

private double x,y,width;

private int orientation;

私有BufferedImage图像;

public ImagePrintable(PrinterJob printJob,BufferedImage image){
PageFormat pageFormat = printJob.defaultPage();
this.x = pageFormat.getImageableX();
this.y = pageFormat.getImageableY();
this.width = pageFormat.getImageableWidth();
this.orientation = pageFormat.getOrientation();
this.image = image;

$ b @Override
public int print(Graphics g,PageFormat pageFormat,int pageIndex)
抛出PrinterException {
if(pageIndex == 0) {
int pWidth = 0;
int pHeight = 0;
if(orientation == PageFormat.PORTRAIT){
pWidth =(int)Math.min(width,(double)image.getWidth());
pHeight = pWidth * image.getHeight()/ image.getWidth();
} else {
pHeight =(int)Math.min(width,(double)image.getHeight());
pWidth = pHeight * image.getWidth()/ image.getHeight();
}
g.drawImage(image,(int)x,(int)y,pWidth,pHeight,null);
返回PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}

}

}


I would like to know if there is a proper way of printing a BufferedImage in Java. Basically I have created a photo manipulation program which works fine, I can save images etc. But my real goal is to send it to the printer software so that you can select the amount of pages you want to print and page type.

So my shortened question is, how do I send a buffered image to the printer so that a printer selection screen will popup etc and then be able to print?

If anyone can show me an example of this, it would be greatly appreciated.

解决方案

Here's one from one of my Java projects. This code will scale and print one image on a printer page.

You call it like this:

    printButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            //Start a Thread
            new Thread(new PrintActionListener(image)).start();         
        }
    });

Here's the Runnable:

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;

public class PrintActionListener implements Runnable {

    private BufferedImage       image;

    public PrintActionListener(BufferedImage image) {
        this.image = image;
    }

    @Override
    public void run() {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(new ImagePrintable(printJob, image));

        if (printJob.printDialog()) {
            try {
                printJob.print();
            } catch (PrinterException prt) {
                prt.printStackTrace();
            }
        }
    }

    public class ImagePrintable implements Printable {

        private double          x, y, width;

        private int             orientation;

        private BufferedImage   image;

        public ImagePrintable(PrinterJob printJob, BufferedImage image) {
            PageFormat pageFormat = printJob.defaultPage();
            this.x = pageFormat.getImageableX();
            this.y = pageFormat.getImageableY();
            this.width = pageFormat.getImageableWidth();
            this.orientation = pageFormat.getOrientation();
            this.image = image;
        }

        @Override
        public int print(Graphics g, PageFormat pageFormat, int pageIndex)
                throws PrinterException {
            if (pageIndex == 0) {
                int pWidth = 0;
                int pHeight = 0;
                if (orientation == PageFormat.PORTRAIT) {
                    pWidth = (int) Math.min(width, (double) image.getWidth());
                    pHeight = pWidth * image.getHeight() / image.getWidth();
                } else {
                    pHeight = (int) Math.min(width, (double) image.getHeight());
                    pWidth = pHeight * image.getWidth() / image.getHeight();
                }
                g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null);
                return PAGE_EXISTS;
            } else {
                return NO_SUCH_PAGE;
            }
        }

    }

}

这篇关于用Java打印BufferedImage的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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