使 PrinterJob 对象适合 BufferedImage 的特定打印格式 [英] fitting PrinterJob Object to specific print format of BufferedImage

查看:34
本文介绍了使 PrinterJob 对象适合 BufferedImage 的特定打印格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PrinterJob 对象来打印我的 Bufferedimage,我有一个 BufferedImage,我将其处理并使用 Paper Format 等将其发送到打印机作业,但我无法使其适合我的卡片打印机.当我将它保存到我的硬盘并通过 Windows 打印管理器打印时,它在我的卡片打印机上打印得很好,但是使用 PrinterJob 时它太大了,不适合卡片

Im using PrinterJob object in order to print my Bufferedimage, I have a BufferedImage which I proccess and send it to Printer job with Paper Format etc, and I cant make it fittable to my card printer. when i save it to my hard-disk and print via windows printing manager it printing very good on my card printer but with PrinterJob it came out too big and not fittable for a card

卡片尺寸为86X54mm,我缓冲的图片尺寸为1300x816px代码:

the size of the card is 86X54mm and the size of my buffered image is 1300x816px The Code :

    PrinterJob printjob = PrinterJob.getPrinterJob();
    printjob.setJobName("CardPrint");

    Printable printable = new Printable() {

            public int print(Graphics pg, PageFormat pf, int pageNum) {

                    if (pageNum > 0) {
                            return Printable.NO_SUCH_PAGE;
                    }
                    JLayeredPane j1 = new JLayeredPane();
                    Dimension size = j1.getSize();

                    j1.print(bi.getGraphics());

                    Graphics2D g2 = (Graphics2D) pg;
                    g2.translate(pf.getImageableX(), pf.getImageableY());
                    g2.drawImage(bi, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);

                    return Printable.PAGE_EXISTS;
            }
    };

    Paper paper = new Paper();
    paper.setImageableArea(0, 0, 0, 0);
    paper.setSize(1.15, 0.72);

    PageFormat format = new PageFormat();
    format.setPaper(paper);


    printjob.setPrintable(printable, format);

    try {
            printjob.printDialog();
            printjob.print();

    } catch (Exception eee){
            System.out.println("NO PAGE FOUND."+eee.toString());

    }

我发现paper.setSize(1.15, 0.7);英寸 (http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/print/Paper.html)paper.setImageableArea(0, 0, 0, 0);我不知道这个 setImageableArea.

I found out that paper.setSize(1.15, 0.7); is in inch (http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/print/Paper.html) paper.setImageableArea(0, 0, 0, 0); and i dont know about this setImageableArea.

有人知道当前的尺寸吗,我计算有误吗?谢谢.

does any one has clue about the current sizes, do i made a mistake calculating ? thanks.

推荐答案

第一次打印,打印很有趣" - 记住,在凌晨 2 点重复这个...

First of printing, "Printing is Fun" - remember, repeat this at 2am...

基本上,您需要先将纸张尺寸从 CM 转换为像素.Java 的 API 使用 72dpi.

Basically, you need to start by converting your paper size from CM to pixels. Java uses 72dpi for it's API.

所以你的页面大小为 8.6x5.4cm 计算为 153.0708659856 x 243.7795273104 像素

So you page size of 8.6x5.4cm calculates to 153.0708659856 x 243.7795273104 pixels

然后你需要创建一个新的Paper来满足那些页面要求...

Then you need to create a new Paper that meets those page requirements...

这一切都需要重新包装成一个 PageFormat 并交还给 PrintJob.

This all needs to be wrapped back into a PageFormat and handed back to the PrintJob.

Printable 中,您需要缩放图像以适合"可打印区域.图像缩放很有趣...

Within the Printable, you need to scale the image to "fit" the printable area. Image scaling is fun...

所以,对于我的测试,我有一个 800x1159 的图像,它被缩小到 166x241

So, for me test, I had an image of 800x1159, which was scaled down to 166x241

有关图像缩放的更好讨论,请查看 这个问题

For a better discussion on image scaling check out this question

public class PrintTest02 {

    private static BufferedImage image;

    public static void main(String[] args) {
        try {
            image = ImageIO.read(new File("/path/to/image.png"));

            System.out.println(image.getWidth() + "x" + image.getHeight());

            PrinterJob pj = PrinterJob.getPrinterJob();
            if (pj.printDialog()) {
                PageFormat pf = pj.defaultPage();
                Paper paper = pf.getPaper();
//                        86X54mm
                double width = fromCMToPPI(8.6);
                double height = fromCMToPPI(5.4);
                paper.setSize(width, height);
                paper.setImageableArea(
                                fromCMToPPI(0.1),
                                fromCMToPPI(0.1),
                                width - fromCMToPPI(0.1),
                                height - fromCMToPPI(0.1));
                pf.setOrientation(PageFormat.PORTRAIT);
                pf.setPaper(paper);
                PageFormat validatePage = pj.validatePage(pf);
                System.out.println("Valid- " + dump(validatePage));
                pj.setPrintable(new MyPrintable(), validatePage);
                try {
                    pj.print();
                } catch (PrinterException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }

    protected static double fromPPItoCM(double dpi) {
        return dpi / 72 / 0.393700787;
    }

    protected static double fromCMToPPI(double cm) {
        return toPPI(cm * 0.393700787);
    }

    protected static double toPPI(double inch) {
        return inch * 72d;
    }

    protected static String dump(Paper paper) {
        StringBuilder sb = new StringBuilder(64);
        sb.append(paper.getWidth()).append("x").append(paper.getHeight())
                        .append("/").append(paper.getImageableX()).append("x").
                        append(paper.getImageableY()).append(" - ").append(paper
                        .getImageableWidth()).append("x").append(paper.getImageableHeight());
        return sb.toString();
    }

    protected static String dump(PageFormat pf) {
        Paper paper = pf.getPaper();
        return dump(paper);
    }

    public static class MyPrintable implements Printable {

        @Override
        public int print(Graphics graphics, PageFormat pageFormat,
                                         int pageIndex) throws PrinterException {
            System.out.println(pageIndex);
            int result = NO_SUCH_PAGE;
            if (pageIndex < 1) {
                Graphics2D g2d = (Graphics2D) graphics;
                System.out.println("[Print] " + dump(pageFormat));
                double width = pageFormat.getImageableWidth();
                double height = pageFormat.getImageableHeight();

                System.out.println("Print Size = " + fromPPItoCM(width) + "x" + fromPPItoCM(height));
                g2d.translate((int) pageFormat.getImageableX(),
                                (int) pageFormat.getImageableY());
                Image scaled = null;
                if (width > height) {
                    scaled = image.getScaledInstance((int)Math.round(width), -1, Image.SCALE_SMOOTH);
                } else {
                    scaled = image.getScaledInstance(-1, (int)Math.round(height), Image.SCALE_SMOOTH);
                }
                g2d.drawImage(scaled, 0, 0, null);
                result = PAGE_EXISTS;
            }
            return result;
        }

    }

}

这篇关于使 PrinterJob 对象适合 BufferedImage 的特定打印格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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