将bufferedimage打印到打印机 [英] printing bufferedimage to a printer

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

问题描述

我有一个应用程序,我想从中打印图像。图像作为BufferedImage对象加载。问题是,当我打印图像(到postscript或pdf文件)时,质量真的很差。

当我使用其他一些工具时(基本上任何可以打印的图片查看器应用程序)图像)结果明显更好。

我知道DPI与分辨率有一些问题,但我不确定如何计算正确的打印值。

我尝试谷歌尝试了一些方法,但似乎没有像我预期的那样工作。
Basicaly我只想打印一张图像(分辨率为3000x2000)打印机(DPI为600x600)。

i have an application from which i want to print an image. The image is loaded as a BufferedImage object. The problem is, when i print the image (to the postscript or to the pdf file), the quality is really poor.
When i'm using some other tools (basically any picture viewer application which can print the image) the result is significantly better.
I know there can be some problems with the DPI vs resolution but i'm not exactly sure how to compute the correct values for printing.
I tried to google and tried some methods, but nothing seems to work as i expected. Basicaly i just want to print an image (in resolution let's say 3000x2000) to a printer (with DPI for example 600x600).

这是我创建打印作业的方式:

This is how i create the print job:

PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
printAttributes.add(PrintQuality.HIGH);
printAttributes.add(new PrinterResolution(600, 600 PrinterResolution.DPI)); 
printAttributes.add(new Destination(URI.create("file:/tmp/test.ps")));
PageFormat pf = printerJob.defaultPage();
Paper paper = pf.getPaper();
double xMargin = 0.0;
double yMargin = 0.0;
paper.setImageableArea(xMargin, yMargin, paper.getWidth() - 2 * xMargin, paper.getHeight() - 2 * yMargin);
pf.setPaper(paper);

// create new Printable for the specified image
printerJob.setPrintable(PrintableImage.get(image), pf)

if (printerJob.printDialog(printAttributes)) {
    printerJob.print(printAttributes);
}

其中图像 BufferedImage PrintableImage.get 返回实现 Printable
的新实例然后实际的打印是这样做的(我让我尝试的注释代码但没有'为我工作)

Where image is BufferedImage and PrintableImage.get returns new instance which implements Printable Then the actual print is doing this way (i let the commented code which i tried but didn't work for me)

@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (image == null)
    throw new PrinterException("no image specified to be printed");

// We have only one page, and 'page' is zero-based
if (pageIndex > 0) {
    return NO_SUCH_PAGE;
}

// tranlate the coordinates (according to the orientations, margins, etc)
Graphics2D printerGraphics = (Graphics2D) graphics;

//g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
//g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

printerGraphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

// THIS IS A TEST - javax.printing api uses 72 DPI, but we have 600DPI set for the printer
//AffineTransform at = printerGraphics.getTransform();
//printerGraphics.scale((double)72 / (double)600, (double)72 / (double)600);
//printerGraphics.drawRenderedImage(image, null);
//printerGraphics.setTransform(at);
//if(printerGraphics != null)
    //return PAGE_EXISTS;

double scale = 72.0 / 600.0;

Dimension pictureSize = new Dimension((int)Math.round(image.getWidth() / scale), (int) Math.round(image.getHeight() / scale));


// center the image horizontaly and verticaly on the page
int xMargin = (int) ((pageFormat.getImageableWidth() - image.getWidth()) / 2);
int yMargin = (int) ((pageFormat.getImageableHeight() - image.getHeight()) / 2);
xMargin = yMargin = 0;

System.out.println(String.format("page size [%.2f x %.2f], picture size [%.2f x %.2f], margins [%d x %d]", pageFormat.getImageableWidth(), pageFormat.getImageableHeight(), pictureSize.getWidth(), pictureSize.getHeight(), xMargin, yMargin));

printerGraphics.drawImage(image, xMargin, yMargin, (int)pageFormat.getWidth(), (int)pageFormat.getHeight(), null);

//printerGraphics.drawImage(image, 0, 0, null);
//printerGraphics.drawImage(image, xMargin, yMargin, pictureSize.width, pictureSize.height, null);
//printerGraphics.drawImage(image, xMargin, yMargin, (int) pageFormat.getImageableWidth(), (int) pageFormat.getImageableHeight(), 0, 0, pictureSize.width, pictureSize.height, null);
//printerGraphics.drawImage(image, 0, 0, (int) pageFormat.getWidth() - xMargin, (int) pageFormat.getHeight() - yMargin, 0, 0, pictureSize.width, pictureSize.height, null);

return PAGE_EXISTS;
}

有人解决同样的问题吗?

任何帮助将不胜感激。

谢谢

Matej

Does anybody solves the same problem?
Any help would be appreciated.
Thanks
Matej

推荐答案

@Viktor Fonic
谢谢你,我正在寻找这么多!
您的解决方案完美无缺,但有一个小错误,
未声明textSize,因此:

@Viktor Fonic Thank you for this, I was searching a lot to do this! You're solution works perfectly, but has a small error, textSize was not declared, so:

int textSize =  (int) (pageHeight - image.getHeight()*pageWidth/image.getWidth());

这篇关于将bufferedimage打印到打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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