2012-05-15 141 views
2

我正在嘗試寫一個PhotoBooth程序,但我很難做出無邊界打印。我非常接近,但圖像不填充4「×6」的印刷品。我希望獲得無邊界打印的任何提示。無邊界打印

乾杯!

final BufferedImage img = ImageIO.read(new File(image)); 

    // Assuming that images are going to be 300 DPI 
    PrinterResolution pr = new PrinterResolution(300, 300, 
     PrinterResolution.DPI); 

    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
    pras.add(pr); 

    // Set print job so the image name shows (in the print queue) 
    this.pj.setJobName(new File(image).getName()); 

    PageFormat pf = this.pj.getPageFormat(null); 
    Paper paper = pf.getPaper(); 
    paper.setSize(4 * 72, 6 * 72); 
    paper.setImageableArea(
     0.0, 0.0, 
     paper.getWidth(), paper.getHeight() 
    ); 

    if(img.getWidth(null) > img.getHeight(null)) 
     pf.setOrientation(PageFormat.LANDSCAPE); 
    else 
     pf.setOrientation(PageFormat.PORTRAIT); 

    pf.setPaper(paper); 

    // Create the page 
    this.pj.setPrintable(new Printable() { 
     public int print(Graphics g, PageFormat pf, int i) throws 
      PrinterException { 
      if (i != 0) 
       return NO_SUCH_PAGE; 

      double width = img.getWidth(null); 
      double height = img.getHeight(null); 

      double w = Math.floor(pf.getImageableWidth() - 
       pf.getImageableX())/(width * 1.0); 

      double h = Math.floor(pf.getImageableHeight() - 
       pf.getImageableY())/(height * 1.0); 

      double scale = Math.min(w, h); 

      Graphics2D g2 = (Graphics2D) g; 
      g2.translate(0, 0); 
      g2.scale(scale, scale); 
      g2.drawImage(img, 0, 0, (int)width, (int)height, null); 

      return PAGE_EXISTS; 
     } 
    }, this.pj.validatePage(pf)); 

    // Get number of copies 
    int nCopies = SetPrintQuantity.getPrintQuantity(new File(image)); 

    // Print 
    if(nCopies != 0) 
     for(int i = 0; i < nCopies; i++) 
      this.pj.print(pras); 

    System.out.println(nCopies + ((nCopies == 1) ? " copy" : " copies")); 

this.pj =的PrinterJob

+0

除非打印機支持[full bleed](http://en.wikipedia.org/wiki/Bleed_%28printing%29),否則這可能是不可能的。 – trashgod

+0

我不確定打印機是否可以完全流血,但我知道我可以通過使用Windows圖像查看器中的打印功能獲得無邊界打印。所以我想我的目標是實現這樣的目標。 – Johnny

+0

我有同樣的情況,但在Mac上。命令行中的'lp'沒有問題,打印一個完整的出血4x6,但是Java在每一邊放置了'setImageableArea'似乎無法刪除的大量邊際。 –

回答

1

我已經奮鬥了同樣的問題,而現在。這裏是我的解決方案:

確定您的打印機的實際頁面大小:

final PrinterJob printJob = PrinterJob.getPrinterJob(); 
printJob.setPrintService(ps); 
final PageFormat pf = printJob.defaultPage(); 
System.out.println("Printer Page width=" + pf.getWidth() + " height=" + pf.getHeight()); 

對於我的呈妍P720L照片打印機,4「x6」的論文這實際上是4.133「x6.147」。

創建和設置新的Paper對象,具有完整的頁面大小和全成像區域:

final Paper paper = new Paper(); 
paper.setSize(pageWidth * 72.0f, pageHeight * 72.0f); 
paper.setImageableArea(0.0, 0.0, paper.getWidth(), paper.getHeight()); 
pf.setPaper(paper); 

然後最後絕招,繪製到X/Y座標的範圍之外。在我的情況下,我必須將x座標縮放10%,並將x座標轉換爲-55(將其放入負x值)。

printJob.setPrintable(new Printable() { 
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { 
    Graphics2D g2 = (Graphics2D) graphics; 
    final double xScale = 1.1; 
    final double xTranslate = -55; 
    final double yScale = 1; 
    final double yTranslate = 0; 
    final double widthScale = (pageFormat.getWidth()/image.getWidth()) * xScale; 
    final double heightScale = (pageFormat.getHeight()/image.getHeight()) * yScale; 
    System.out.println("Setting scale to " + widthScale + "x" + heightScale); 
    final AffineTransform at = AffineTransform.getScaleInstance(widthScale, heightScale); 
    System.out.println("Setting translate to " + xTranslate + "x" + yTranslate); 
    at.translate(xTranslate, yTranslate); 
    if (pageIndex != 0) { 
     return NO_SUCH_PAGE; 
    } 
    g2.drawRenderedImage(image, at); 
    return PAGE_EXISTS; 
} 
}, pf);