如何在300 dpi打印机上用java设计打印图像
我想用Java制作一个图像,并将其打印在300dpi标签打印机上,标签尺寸为150 x 100 mm。 如何制作图像,使一行(或任何类型的元素)精确打印在位置(10,10)(以毫米为单位)上,并且该行结束于位置(10,50) 换句话说:我的挑战不是如何画一条线(我使用的是Graphics2D,BuffereImage),而是如何准确地知道这条线在标签上的位置(以毫米为单位)如何在300 dpi打印机上用java设计打印图像,java,Java,我想用Java制作一个图像,并将其打印在300dpi标签打印机上,标签尺寸为150 x 100 mm。 如何制作图像,使一行(或任何类型的元素)精确打印在位置(10,10)(以毫米为单位)上,并且该行结束于位置(10,50) 换句话说:我的挑战不是如何画一条线(我使用的是Graphics2D,BuffereImage),而是如何准确地知道这条线在标签上的位置(以毫米为单位) 有什么想法吗?Java的打印API基本上是以72 dpi的速度完成所有工作的。这意味着您可以将其用作转换不同测量值的基础
有什么想法吗?Java的打印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));
打印时,my可打印
的可成像区域为425.20 x 283.46像素,相当于72dpi时的15.03 x 10.02 cm(大致)。这就是Java的工作原理,它的基本打印API一直在假设72dpi的情况下工作
所以。如果我在72 DPI下准备一个10 x 50 mm的图像,我会得到一个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;
}
}
}
导入java.awt.Color;
导入java.awt.EventQueue;
导入java.awt.FontMetrics;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.geom.Line2D;
导入java.awt.geom.Rectangle2D;
导入java.awt.image.buffereImage;
导入java.awt.print.PageFormat;
导入java.awt.print.Printable;
导入java.awt.print.PrinterException;
导入java.awt.print.PrinterJob;
导入javax.print.attribute.HashPrintRequestAttributeSet;
导入javax.print.attribute.PrintRequestAttributeSet;
导入javax.print.attribute.standard.MediaPrintableArea;
导入javax.print.attribute.standard.PrinterResolution;
公共类TestHiResPrinting{
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
PrintRequestAttributeSet aset=新的HashPrintRequestAttributeSet();
aset.add(新的PrinterResolution(300300,PrinterResolution.DPI));
aset.add(新的MediaPrintableArea(0,0,150,100,MediaPrintableArea.MM));
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(newprinttask());
if(打印对话框(aset)){
试一试{
pj.印刷品(aset);
}捕获(打印例外){
例如printStackTrace();
}
}
}
});
}
//每英寸的厘米数
公共静态最终双厘米/英寸=0.393700787d;
//每厘米的英寸数
公共静态最终双英寸每厘米=2.545d;
//每毫米的英寸数
公共静态最终双英寸每毫米=25.45d;
/**
*根据提供的DPI将给定像素转换为厘米
*
*@param像素
*@param dpi
*@返回
*/
公共静态双像素CMS(双像素,双dpi){
返回inchesToCms(像素/dpi);
}
/**
*根据提供的DPI将给定cm转换为像素
*
*@param-cms
*@param dpi
*@返回
*/
公共静态双cmsToPixel(双cms,双dpi){
返回cmToInches(cms)*dpi;
}
/**
*将给定厘米转换为英寸
*
*@param-cms
*@返回
*/
公共静态双cmToInches(双cms){
每英寸返回厘米*厘米;
}
/**
*将给定的英寸转换为厘米
*
*@param英寸
*@返回
*/
公共静态双英寸ESTOCMS(双英寸){
返回英寸*英寸/厘米;
}
公共静态类PrintTask实现可打印{
专用缓冲图像img;
公共打印任务(){
双宽度=cmsToPixel(1,72);
双倍高度=cmsToPixel(5,72);
img=新的buffereImage((int)Math.round(width),(int)Math.round(height),buffereImage.TYPE_int_ARGB);
Graphics2D g2d=img.createGraphics();
g2d.setColor(Color.RED);
绘制(新矩形2D.Double(0,0,宽度-1,高度-1));
绘制(新的Line2D.Double(0,0,宽度,高度));
g2d.draw(新的Line2D.Double(0,高度,宽度,0));
g2d.dispose();
}
@凌驾
公共整型打印(图形图形、页面格式、页面格式、整型页面索引)引发PrinterException{
int result=无此类页面;
如果(页面索引<2){
Graphics2D g2d=(Graphics2D)图形;
double width=pageFormat.getImageableWidth();
double height=pageFormat.getImageableHeight();
System.out.println(“页面宽度=”+width+“=”+pixelsToCms(宽度,72));
System.out.println(“页面高度=”+height+“=”+pixelsToCms(高度,72));
g2d.translate((int)pageFormat.GetImageTablex(),
(int)pageFormat.getImageTabley());
双x=cmsToPixel(1,72);
// 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.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);