2013-09-24 52 views
8

我想在Java中的图像并将其打印在标签上一个300dpi的标签打印机上印刷,尺寸150×100毫米。 如何制作图像,以便在位置(10,10)(以毫米为单位)准确打印直线(或任何类型的元素),并且该直线在位置(10,50)处结束?换句话说:我的挑战不是如何制作一条线(我使用的是Graphics2D,bufferedImage),但是它是如何能够准确地确定标签线在标签上的位置(以毫米为单位) 。如何用Java设计的图像要在300 dpi的打印机

任何想法?

+1

通常情况下,打印是在72 dpi完成的(好吧,事实并非如此,但API认为是)。因此,首先尽量不要担心300dpi,并专注于从72dpi转换为您需要的任何内容 – MadProgrammer

回答

5

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要求只能支持300×300 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厘米@ 72dpi(大致)。这就是Java的工作原理,它是基本的打印API一直在72dpi的假设工作。

所以。如果我准备10 x 50毫米@ 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

那么有很多事情要考虑,其中大部分是基本的数学。我不是特别熟悉的Java2D,所以我不能告诉你,如果有任何辅助功能,但这里是数学:

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

1" 等于300个像素,并且等于至25.4mm这意味着s表示1毫米等于约12像素(11.8)。

所以,如果你想使一个线起点在10 x 10毫米,你需要乘以一个毫米的像素数量,在这种情况下为12.所以开始画你的线在120x120像素

同样,如果你需要结束线在10x50mm,你需要结束你的线条画在120x600。

根据打印机打印的分辨率不同,数学方法各不相同,但对于所问的问题,这些数字应该足够了。

希望它有帮助。

相关问题