java - 无边距打印

标签 java printing

我正在尝试编写一个 photobooth 程序,但我很难制作无边框照片。我已经很接近了,但图像无法填满 4"x 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 = 打印机作业

最佳答案

一段时间以来,我一直在与同样的问题作斗争。这是我的解决方案:

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

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

对于我使用 4"x6"纸张的 HiTi P720L 照片打印机,它实际上是 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);

关于java - 无边距打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10594242/

相关文章:

java - 使用快速查找算法 (Java) 优化有向图中查找所有弱连通分量

Java 8 optional 添加仅当 optional.isPresent 时才返回结果

java - 同时注册MouseListener和MouseMotionListener

drupal - 仅打印 drupal field_view_field 值

带数据库的 Java Applet

java - m2Eclipse 插件 : What does 'Resolve Workspace Artifacts' do?

jquery - 如何让 jQuery printElement 打印带有 css 类的 div?

c# - 从Windows打印设置对话框保存打印设置

python - 打印对象和 unicode,背后是什么?好的指导方针是什么?

javascript - 使用 window.print() 打印输出到不同的打印机