2013-09-24 51 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。

根據打印機打印的分辨率不同,數學方法各不相同,但對於所問的問題,這些數字應該足夠了。

希望它有幫助。

相關問題