如何在java中设计要在300 dpi打印机上打印的图像

时间:2013-09-24 07:35:38

标签: java

我想用Java制作图像并将其打印在尺寸为150 x 100 mm的标签上的300dpi标签打印机上。 如何制作图像以便在位置(10,10)(毫米)处准确打印一条线(或任何类型的元素),并且该线在位置(10,50)处结束?

换句话说:我的挑战不是如何制作一条线(我正在使用Graphics2D,bufferedImage),但它是如何能够准确地告诉标签上该线的位置(以毫米为单位)。

任何想法?

3 个答案:

答案 0 :(得分:7)

Java的print API基本上假设一切都是在72 dpi完成的。这意味着您可以将其用作转换为不同测量值的基础......

这只是意味着你需要并开始价值和目标测量......

// The number of CMs per Inch
public static final double CM_PER_INCH = 0.393700787d;
// The number of Inches per CMs
public static final double INCH_PER_CM = 2.545d;
// The number of Inches per mm's
public static final double INCH_PER_MM = 25.45d;

/**
 * Converts the given pixels to cm's based on the supplied DPI
 * @param pixels
 * @param dpi
 * @return 
 */
public static double pixelsToCms(double pixels, double dpi) {
    return inchesToCms(pixels / dpi);
}

/**
 * Converts the given cm's to pixels based on the supplied DPI
 * @param cms
 * @param dpi
 * @return 
 */
public static double cmsToPixel(double cms, double dpi) {
    return cmToInches(cms) * dpi;
}

/**
 * Converts the given cm's to inches
 * @param cms
 * @return 
 */
public static double cmToInches(double cms) {
    return cms * CM_PER_INCH;
}

/**
 * Converts the given inches to cm's 
 * @param inch
 * @return 
 */
public static double inchesToCms(double inch) {
    return inch * INCH_PER_CM;
}

因此,为了以10mmx10mm打印图像,您需要将其转换为每英寸像素数

double point = cmsToPixel(1, 72);

您可能还需要考虑缩小图像尺寸以适应可打印区域。

举个例子......

使用测试结果进行更新

所以我做了一些测试(代码要遵循)......

首先,我设置PrintRequestAttributeSet仅请求能够支持300x300 dpi的打印服务......

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));

打印时,我的Printable通过了425.20 x 283.46像素的可成像区域,相当于15.03 x 10.02 cm @ 72dpi(粗略)。这就是Java的工作原理,它的基本打印API一直在假设72dpi。

因此。如果我准备一张10 x 50 mm @ 72 DPI的图像,我得到的图像尺寸为28.346 x 141.732像素,可轻松放入可成像区域(425.20 x 283.46)。

然而,如果我使用300 dpi,我会得到118.11 x 590.551像素的图像尺寸,这会让我们遇到麻烦,需要我们缩小图像...

实际上,这可能是可取的,您必须执行一些测试才能找到答案。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.PrinterResolution;

public class TestHiResPrinting {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
                aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));

                PrinterJob pj = PrinterJob.getPrinterJob();
                pj.setPrintable(new PrintTask());

                if (pj.printDialog(aset)) {
                    try {
                        pj.print(aset);
                    } catch (PrinterException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
    }

    // The number of CMs per Inch
    public static final double CM_PER_INCH = 0.393700787d;
    // The number of Inches per CMs
    public static final double INCH_PER_CM = 2.545d;
    // The number of Inches per mm's
    public static final double INCH_PER_MM = 25.45d;

    /**
     * Converts the given pixels to cm's based on the supplied DPI
     *
     * @param pixels
     * @param dpi
     * @return
     */
    public static double pixelsToCms(double pixels, double dpi) {
        return inchesToCms(pixels / dpi);
    }

    /**
     * Converts the given cm's to pixels based on the supplied DPI
     *
     * @param cms
     * @param dpi
     * @return
     */
    public static double cmsToPixel(double cms, double dpi) {
        return cmToInches(cms) * dpi;
    }

    /**
     * Converts the given cm's to inches
     *
     * @param cms
     * @return
     */
    public static double cmToInches(double cms) {
        return cms * CM_PER_INCH;
    }

    /**
     * Converts the given inches to cm's
     *
     * @param inch
     * @return
     */
    public static double inchesToCms(double inch) {
        return inch * INCH_PER_CM;
    }

    public static class PrintTask implements Printable {

        private BufferedImage img;

        public PrintTask() {
            double width = cmsToPixel(1, 72);
            double height = cmsToPixel(5, 72);

            img = new BufferedImage((int) Math.round(width), (int) Math.round(height), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = img.createGraphics();
            g2d.setColor(Color.RED);
            g2d.draw(new Rectangle2D.Double(0, 0, width - 1, height - 1));
            g2d.draw(new Line2D.Double(0, 0, width, height));
            g2d.draw(new Line2D.Double(0, height, width, 0));
            g2d.dispose();
        }

        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            int result = NO_SUCH_PAGE;
            if (pageIndex < 2) {
                Graphics2D g2d = (Graphics2D) graphics;
                double width = pageFormat.getImageableWidth();
                double height = pageFormat.getImageableHeight();

                System.out.println("Page width = " + width + " = " + pixelsToCms(width, 72));
                System.out.println("Page height = " + height + " = " + pixelsToCms(height, 72));

                g2d.translate((int) pageFormat.getImageableX(),
                                (int) pageFormat.getImageableY());
                double x = cmsToPixel(1, 72);
                double y = cmsToPixel(1, 72);
                System.out.println("Draw At " + x + "x" + y);
                g2d.drawRect(0, 0, (int)width - 1, (int)height - 1);
                g2d.drawImage(img, (int)x, (int)y, null);
                result = PAGE_EXISTS;
            }
            return result;
        }

    }
}

答案 1 :(得分:-1)

有很多事情需要考虑,其中大部分都是基础数学。我对Java2D并不是特别熟悉,所以我不能告诉你是否有任何辅助函数,但这里是数学:

150 x 100毫米大约是6x4英寸。在300 DPI时,您需要像素分辨率1800x1200。

1“等于300像素,等于25.4 mm,这意味着1 mm等于约12像素(11.8)。

因此,如果您想要以10x10mm开始生成一条线,则需要将其与mm中的像素数相乘,在本例中为12.因此,请开始以120x120像素绘制线条。

同样,如果您需要以10x50mm结束直线,则需要以120x600结束线条绘制。

每种情况下的数学运算都不同,具体取决于打印机打印的分辨率,但对于提出的问题,这些数字应该足够了。

希望它有所帮助。

答案 2 :(得分:-1)

在我使用Brother DPC-135C打印机的测试中,通过MadProgrammer运行编辑的程序,评论以下几行:

//   aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
//   aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));

...
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
   g2d.drawRect((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY(), (int) pageFormat.getImageableWidth(), (int) pageFormat.getImageableHeight());
...

我认为上面的g2d.drawRect()正确覆盖了javadoc(PageFormat)pageFormat.getImageableWidth()和pageFormat.getImageableHeight()中定义的整个mediaprintablearea,返回1/72英寸的值。例如,返回451.2755的pageFormat.getImageableWidth()表示可打印区域的宽度为451.2755 / 72 = ~6.26英寸。因此,从pageFormat.getImageableX(),(int)pageFormat.getImageableY()开始,每毫米约为2.8346个单位(72个单位为1英寸,1英寸= 25.4毫米)。因此要绘制图像(缩放图像的像素以适合指定的宽度和高度):

 g2d.drawImage(img, (int) (pageFormat.getImageableX() + 2.8346 * 10),   // start printing at 10 mm from left of printable area
                    (int) (pageFormat.getImageableY() + 2.8346 * 10),   // start printing at 10 mm from top of printable area
                    (int) 2.8346 * 100,   // width of 100 mm
                    (int) 2.8346 * 150,   // height of 150 mm
                    this);

在打印对话框中,您需要删除边距。像MadProgrammer上面的代码那样设置每英寸点数我觉得在打印输出中测量到一个英寸的确切位置(关于SDK中声明的是什么,pageFormat.getImageableWidth()和pageFormat.getImageableHeight()返回1 / 72英寸)。在我的测试中,我使用尺子测量打印的图像,它非常准确。