2012-02-27 202 views
2

我有一個應用程序,我想從中打印圖像。該圖像作爲BufferedImage對象加載。問題是,當我打印圖像(後附或pdf文件)時,質量非常差。
當我使用其他工具(基本上可以打印圖像的任何圖片查看器應用程序)時,結果顯着更好。
我知道DPI vs分辨率可能存在一些問題,但我不確定如何計算正確的打印值。
我試圖谷歌和嘗試一些方法,但似乎沒有工作,因爲我的預期。基本上,我只想打印一張圖像(分辨率爲3000x2000)到打印機(DPI爲600x600)。打印bufferedimage到打印機

這是我如何創建打印作業:

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); 
} 

圖像0​​是 BufferedImage的 PrintableImage.get回報新實例,實現打印 那麼實際打印是這樣做的方式(我讓評論的代碼,我試過但沒有爲我工作)

@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; 
} 

有人解決同樣的問題嗎?
任何幫助,將不勝感激。
感謝
馬捷

回答

1

這是我做到了。它工作順利。請注意,此代碼片段不會使圖片居中。

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); 
       } 
      } 
1

@Viktor Fonic 感謝你爲這個,我正在尋找很多事情要做這個! 你的解決方案完美地工作,但有一個小錯誤, TEXTSIZE未聲明的,因此:

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