无边距打印 [英] Borderless printing

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

问题描述

我正在尝试编写一个photobooth程序,但我很难做出无边框打印。我很近,但图像没有填充4x 6打印。我希望获得有关无边框打印的任何提示。

I'm trying to write a photobooth program but I'm having a hard time making a borderless print. I'm very close but the image does not fill a 4" x 6" print. I would appreciate any tips on achieving a borderless print.

干杯!

    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

this.pj = PrinterJob

推荐答案

我一直在与同样的问题作斗争。这是我的解决方案:

I have been fighting the same problems for a while now. Here is my solution:

确定打印机的实际页面大小:

Determine the actual page size of your printer:

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

对于我的带有4x6纸的HiTi P720L照片打印机,它实际上是4.133x6.147 。

For my HiTi P720L Photo Printers with 4"x6" paper it was actually 4.133"x6.147".

创建并设置一个具有完整页面大小和完整可成像区域的新Paper对象:

Create and set a new Paper object with the full page size and full imageable area:

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值)。

Then the final trick, draw to x/y coordinates outside the range. In my case I had to scale the x coordinates by 10% and translate the x coordinate by -55 (placing it into negative x values).

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

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

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