L'API di stampa Java funziona fondamentalmente sul presupposto che tutto sia fatto a 72 dpi. Ciò significa che è possibile utilizzare questo come basi per la conversione da/per diverse misure ...
Questo significa solo il necessario e iniziare a valore e la misura di destinazione ...
// 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;
}
Così, al fine di stampa l'immagine a 10mmx10mm, si avrebbe bisogno di convertire questo in pixel per pollice di
double point = cmsToPixel(1, 72);
probabilmente stai anche andando ad avere bisogno di pensare forse ridimensionamento l'immagine per adattarla nell'area stampabile.
Per alcuni esempi ...
aggiornamento con i risultati dei test
così ho fatto alcuni test (codice a seguire) ...
In primo luogo, ho istituito un PrintRequestAttributeSet
per richiedere i servizi di stampa solo in grado di supportare 300x300 dpi ...
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));
Quando stampata, la mia Printable
è stata approvata una superficie stampabile del 425.20 x 283.46 pixel, il che equivale a 15,03 x 10,02 cm @ 72 dpi (all'incirca). Ecco come funziona Java, la sua API di stampa di base ha sempre funzionato sull'assunzione di 72 dpi.
Così. Se preparo un'immagine di 10 x 50 mm a 72 DPI, ottengo una dimensione dell'immagine di 28,346 x 141,732 pixel, che si adatta facilmente all'area immaginabile (di 425.20 x 283.46).
Tuttavia, se uso 300 dpi, ho ottenere una dimensione immagine di 118.11 x 590.551 pixel, che è andare a correre nei guai, ci impone di rimpicciolire l'immagine ...
Questo, in realtà, può essere desiderabile, dovrai eseguire alcuni test per scoprirlo.
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;
}
}
}
Generalmente, la stampa viene eseguita a 72 dpi (ok, non lo è, ma l'API pensa che lo sia). Inizia quindi provando a non preoccuparti dei 300 dpi e concentrati sulla conversione da 72 dpi a ciò che ti serve – MadProgrammer