Java 将缓冲区图像打印到打印机

Java 将缓冲区图像打印到打印机,java,image,printing,dpi,Java,Image,Printing,Dpi,我有一个要打印图像的应用程序。图像作为BuffereImage对象加载。问题是,当我打印图像(到postscript或pdf文件)时,质量非常差。 当我使用其他一些工具(基本上是任何可以打印图像的图片查看器应用程序)时,效果会明显更好。 我知道DPI与分辨率之间可能存在一些问题,但我不确定如何计算正确的打印值。 我尝试了谷歌搜索,尝试了一些方法,但似乎没有任何效果。 基本上,我只想在打印机上打印一张图像(分辨率为3000x2000)(DPI为600x600) 以下是我创建打印作业的方式: Pri

我有一个要打印图像的应用程序。图像作为BuffereImage对象加载。问题是,当我打印图像(到postscript或pdf文件)时,质量非常差。
当我使用其他一些工具(基本上是任何可以打印图像的图片查看器应用程序)时,效果会明显更好。
我知道DPI与分辨率之间可能存在一些问题,但我不确定如何计算正确的打印值。
我尝试了谷歌搜索,尝试了一些方法,但似乎没有任何效果。 基本上,我只想在打印机上打印一张图像(分辨率为3000x2000)(DPI为600x600)

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

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);
}
其中image是buffereImage,PrintableImage.get返回实现Printable的新实例 然后实际的打印是这样做的(我让注释代码,我尝试了,但没有为我工作)

有人解决过同样的问题吗?
任何帮助都将不胜感激。
谢谢

Matej

我就是这样做的。它工作顺利。请注意,此代码段不会将图像居中

public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
                if (pageIndex > 0) {
                    return (NO_SUCH_PAGE);
                } else {
                    double pageHeight = pageFormat.getImageableHeight(), pageWidth = pageFormat.getImageableWidth();

                    Graphics2D g2d = (Graphics2D) g;
                    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                    if (pageHeight < image.getHeight() || pageWidth < image.getWidth()) {
                        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                        g2d.drawImage(image, 0, 0, (int) pageWidth, (int) pageHeight - textSize, null);
                    } else {
                        g2d.drawImage(image, 0, 0, null);
                    }
                    g2d.dispose();
                    return (PAGE_EXISTS);
                }
            }
public int print(Graphics g,PageFormat PageFormat,int pageIndex)抛出PrinterException{
如果(页面索引>0){
返回(无此页面);
}否则{
double pageHeight=pageFormat.getImageableHeight(),pageWidth=pageFormat.getImageableWidth();
Graphics2D g2d=(Graphics2D)g;
translate(pageFormat.getImageableX(),pageFormat.getImageableY());
if(pageHeight
@Viktor Fonic 谢谢你,我一直在寻找很多这样做! 您的解决方案工作正常,但有一个小错误, textSize未声明,因此:

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