2012-08-29 62 views
1

我需要使用Java打印圖像。所以我實現了print()接口的方法Printable。但是打印機總是隻打印一些原始圖像。如何使圖像適合打印區域(A4),因此打印機能夠打印整個圖像,而不是其中的一部分?現在我的代碼如下所示:使圖像適合打印區域

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { 


    if (pageIndex >= images.size()) { 
     return Printable.NO_SUCH_PAGE; 
    } 
    RenderedImage image; 


    Graphics2D graphics2D = (Graphics2D) graphics; 

    image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND); 


    double x = (pageFormat.getImageableWidth() - image.getWidth())/2 + pageFormat.getImageableX(); 
    double y = (pageFormat.getImageableHeight() - image.getHeight())/2 + pageFormat.getImageableY(); 


    graphics2D.drawRenderedImage(image, AffineTransform.getTranslateInstance(x, y)); 

    return PAGE_EXISTS; 
} 

回答

3

您需要縮小圖像以適應可用區域。

private int currentPage = -1; 
private Image cachedScaledImage = null; 

public double getScaleFactor(int iMasterSize, int iTargetSize) { 
    double dScale = 1; 
    if (iMasterSize > iTargetSize) { 
     dScale = (double) iTargetSize/(double) iMasterSize; 
    } else { 
     dScale = (double) iTargetSize/(double) iMasterSize; 
    } 
    return dScale; 
} 

public double getScaleFactorToFit(BufferedImage img, Dimension size) { 
    double dScale = 1; 
    if (img != null) { 
     int imageWidth = img.getWidth(); 
     int imageHeight = img.getHeight(); 
     dScale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size); 
    } 
    return dScale; 
} 

public double getScaleFactorToFit(Dimension original, Dimension toFit) { 
    double dScale = 1d; 
    if (original != null && toFit != null) { 
     double dScaleWidth = getScaleFactor(original.width, toFit.width); 
     double dScaleHeight = getScaleFactor(original.height, toFit.height); 

     dScale = Math.min(dScaleHeight, dScaleWidth); 
    } 
    return dScale; 
} 

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { 

    if (pageIndex >= images.size()) { 
     return Printable.NO_SUCH_PAGE; 
    } 
    RenderedImage image; 


    Graphics2D graphics2D = (Graphics2D) graphics; 

    int width = (int)Math.round(pageFormat.getImageableWidth()); 
    int height = (int)Math.round(pageFormat.getImageableHeight()); 

    if (currentPage != pageIndex || cachedScaledImage == null) { 
     currentPage = pageIndx;  

     image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND); 

     BufferedImage imageCopy = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2d = imageCopy.createGraphics(); 
     g2d.drawImage(imageCopy, 0, 0, null); 
     g2d.dispose(); 

     double scaleFactor = getScaleFactorToFit(new Dimension(image.getWidth(), image.getHeight()), new Dimension(width, height)); 

     int imageWidth = (int)Math.round(image.getWidth() * scaleFactor); 
     int imageHeight = (int)Math.round(image.getHeight() * scaleFactor); 

     double x = ((pageFormat.getImageableWidth() - imageWidth)/2) + pageFormat.getImageableX(); 
     double y = ((pageFormat.getImageableHeight() - imageHeight)/2) + pageFormat.getImageableY(); 

     cachedScaledImage = imageCopy.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH); 

    } 

    graphics2D.drawRenderedImage(cachedScaledImage, AffineTransform.getTranslateInstance(x, y)); 

    return PAGE_EXISTS; 
} 
+0

**謝謝。但我得到︰**'ClassCastExcepion' ** here:**'graphics2D.drawRenderedImage(scaledImage,AffineTransform.getTranslateInstance(x,y));'**我修改它(通過改變方法)爲**'drawImage(。 ..)'。 **但現在我得到**'Java.lang.OutOfMemoryError:Java堆空間' – MyTitle

+0

這並不奇怪。許多系統要求多次呈現特定的頁面頁面,這意味着每次我們都會創建一個新的圖像副本和縮放實例。你應該看看創建一個緩存,這將允許你緩存縮放實例並引用該實例,直到頁碼改變 – MadProgrammer

+0

我讀到JAI-ImageIO工具可能會導致這樣的錯誤。需要我'NullOpImage'?或者我可以通過另一個班級改變它? – MyTitle