将PrinterJob对象修改为BufferedImage的特定打印格式 [英] fitting PrinterJob Object to specific print format of BufferedImage
问题描述
我使用PrinterJob对象打印我的Bufferedimage,我有一个BufferedImage,我处理并发送到打印机作业与纸格式等,我不能使其适合我的卡片打印机。当我将其保存到我的硬盘并通过Windows打印管理器打印,它在我的卡片打印机打印非常好,但是PrinterJob它出来太大,不适合卡
Im using PrinterJob object in order to print my Bufferedimage, I have a BufferedImage which I proccess and send it to Printer job with Paper Format etc, and I cant make it fittable to my card printer. when i save it to my hard-disk and print via windows printing manager it printing very good on my card printer but with PrinterJob it came out too big and not fittable for a card
卡的大小是86X54mm,我的缓冲图像的大小是1300x816px
代码:
the size of the card is 86X54mm and the size of my buffered image is 1300x816px The Code :
PrinterJob printjob = PrinterJob.getPrinterJob();
printjob.setJobName("CardPrint");
Printable printable = new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum) {
if (pageNum > 0) {
return Printable.NO_SUCH_PAGE;
}
JLayeredPane j1 = new JLayeredPane();
Dimension size = j1.getSize();
j1.print(bi.getGraphics());
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.drawImage(bi, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);
return Printable.PAGE_EXISTS;
}
};
Paper paper = new Paper();
paper.setImageableArea(0, 0, 0, 0);
paper.setSize(1.15, 0.72);
PageFormat format = new PageFormat();
format.setPaper(paper);
printjob.setPrintable(printable, format);
try {
printjob.printDialog();
printjob.print();
} catch (Exception eee){
System.out.println("NO PAGE FOUND."+eee.toString());
}
我发现
paper.setSize 1.15,0.7);
为英寸( http ://docs.oracle.com/javase/1.5.0/docs/api/java/awt/print/Paper.html )
paper.setImageableArea(0,0,0,0);
,我不知道这个setImageableArea。
I found out that paper.setSize(1.15, 0.7); is in inch (http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/print/Paper.html) paper.setImageableArea(0, 0, 0, 0); and i dont know about this setImageableArea.
有没有任何关于当前大小的线索,我做错了计算?
谢谢。
does any one has clue about the current sizes, do i made a mistake calculating ? thanks.
推荐答案
首先打印,打印是有趣的 - 记住,重复上午2点...
First of printing, "Printing is Fun" - remember, repeat this at 2am...
基本上,您需要首先将纸张尺寸从CM转换为像素。 Java使用72dpi作为API。
Basically, you need to start by converting your paper size from CM to pixels. Java uses 72dpi for it's API.
所以你的页面大小为8.6x5.4厘米计算为153.0708659856 x 243.7795273104像素
So you page size of 8.6x5.4cm calculates to 153.0708659856 x 243.7795273104 pixels
然后你需要创建符合这些页面要求的新的 Paper
Then you need to create a new Paper
that meets those page requirements...
这一切都需要被包裹回 PageFormat
并交回 PrintJob
。
This all needs to be wrapped back into a PageFormat
and handed back to the PrintJob
.
在可打印
中,您需要将图像缩放为适合可打印区域。图像缩放很有趣...
Within the Printable
, you need to scale the image to "fit" the printable area. Image scaling is fun...
所以,对我来说,我的图像是800x1159,缩小到166x241
So, for me test, I had an image of 800x1159, which was scaled down to 166x241
要更好地讨论图像缩放,请参阅这个问题
For a better discussion on image scaling check out this question
public class PrintTest02 {
private static BufferedImage image;
public static void main(String[] args) {
try {
image = ImageIO.read(new File("/path/to/image.png"));
System.out.println(image.getWidth() + "x" + image.getHeight());
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj.printDialog()) {
PageFormat pf = pj.defaultPage();
Paper paper = pf.getPaper();
// 86X54mm
double width = fromCMToPPI(8.6);
double height = fromCMToPPI(5.4);
paper.setSize(width, height);
paper.setImageableArea(
fromCMToPPI(0.1),
fromCMToPPI(0.1),
width - fromCMToPPI(0.1),
height - fromCMToPPI(0.1));
pf.setOrientation(PageFormat.PORTRAIT);
pf.setPaper(paper);
PageFormat validatePage = pj.validatePage(pf);
System.out.println("Valid- " + dump(validatePage));
pj.setPrintable(new MyPrintable(), validatePage);
try {
pj.print();
} catch (PrinterException ex) {
ex.printStackTrace();
}
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
protected static double fromPPItoCM(double dpi) {
return dpi / 72 / 0.393700787;
}
protected static double fromCMToPPI(double cm) {
return toPPI(cm * 0.393700787);
}
protected static double toPPI(double inch) {
return inch * 72d;
}
protected static String dump(Paper paper) {
StringBuilder sb = new StringBuilder(64);
sb.append(paper.getWidth()).append("x").append(paper.getHeight())
.append("/").append(paper.getImageableX()).append("x").
append(paper.getImageableY()).append(" - ").append(paper
.getImageableWidth()).append("x").append(paper.getImageableHeight());
return sb.toString();
}
protected static String dump(PageFormat pf) {
Paper paper = pf.getPaper();
return dump(paper);
}
public static class MyPrintable implements Printable {
@Override
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) throws PrinterException {
System.out.println(pageIndex);
int result = NO_SUCH_PAGE;
if (pageIndex < 1) {
Graphics2D g2d = (Graphics2D) graphics;
System.out.println("[Print] " + dump(pageFormat));
double width = pageFormat.getImageableWidth();
double height = pageFormat.getImageableHeight();
System.out.println("Print Size = " + fromPPItoCM(width) + "x" + fromPPItoCM(height));
g2d.translate((int) pageFormat.getImageableX(),
(int) pageFormat.getImageableY());
Image scaled = null;
if (width > height) {
scaled = image.getScaledInstance((int)Math.round(width), -1, Image.SCALE_SMOOTH);
} else {
scaled = image.getScaledInstance(-1, (int)Math.round(height), Image.SCALE_SMOOTH);
}
g2d.drawImage(scaled, 0, 0, null);
result = PAGE_EXISTS;
}
return result;
}
}
}
这篇关于将PrinterJob对象修改为BufferedImage的特定打印格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!