对于java的打印问题,各种书上谈的很少。我想主要原因可能是java的打印功能太弱了,没有什么可介绍的。 最近,我因为工作的关系,用到了java的打印图象功能。不过因为缺少可参考的例子和教材,我只有查看jdk 的API文档,的确不是很爽。下面就把我的程序给大家讲讲,顺便白话一下java的打印(是jdk1.2的)。 java的打印类都在java.awt.print包下,主要有四个类和两个interface:PrinterJob,PageFormat, Paper,Book; Printable,Pageable。(详细的情况,请查看jdk的API文档,我只讲我程序中用到的部分)
Interface包括 Printable:主要是用来打印的接口,在打印的时候,它的print()方法不断地被调用,直到 返回NO_SUCH_PAGE为止。
PrinterJob:初始化打印操作,可以显示系统特定的打印对话框,例如windows的。
PageFormat:描述可打印区。例如我的程序用的几个方法
public double getImageableX();
public double getImageableY();
public double getImageableWidth();
public double getImageableHeight();
package jinicup.printer;
import java.awt.*;
import java.awt.print.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.io.*;
/**********************************
* Implemenation of the printer service
***********************************/
public class PrinterImpl extends JPanel
implements Printable {
private Image image;
private PrinterJob printJob;
private double x,y,w,h;
private int imagew,imageh;
PrinterImpl () {
printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
printJob.printDialog();
}
public int print (Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
System.out.println("pageIndex"+pageIndex);
if (pageIndex = 1) {
return Printable.NO_SUCH_PAGE;
}
x = pageFormat.getImageableX();
y = pageFormat.getImageableY();
w = pageFormat.getImageableWidth();
h = pageFormat.getImageableHeight();
if(imagew = imageh){
h=w*imageh/imagew;
}else{
w=h*imagew/imageh;
}
System.out.println(x+" "+y);
System.out.println(w+" "+h);
drawGraphics(graphics);
return Printable.PAGE_EXISTS;
}
public void paint (Graphics graphics) {
drawGraphics(graphics);
}
private void drawGraphics (Graphics graphics) {
graphics.drawImage(image, (int)x,(int)y,(int)w,(int)h, null);
// graphics.drawOval(10, 10, 100, 50);
}
/**********************************
* starts the printing
* @param byteArrayOfJPEGFile a valid byte array of a jpg file (can be directly from the camera)
***********************************/
public void printByteArray (byte[] byteArrayOfJPEGFile) {
// Toolkit tool = Toolkit.getToolkit();
// image=tool.createImage(byteArrayOfJPEGFile);
image = (new ImageIcon(byteArrayOfJPEGFile)).getImage();
imagew=image.getWidth(null);
imageh=image.getHeight(null);
System.out.println(imagew+" "+imageh);
System.out.println("kkk");
try {
System.out.println("start printing");
printJob.print();
System.out.println("printing was spooled to the printer");
} catch (Exception ex) {
System.out.println(ex);
}
return;
}
/**********************************
* main method, only for text purposes
* @param args no args are used
***********************************/
public static void main (String[] args) {
PrinterImpl pi = new PrinterImpl();
try {
FileInputStream fs = new FileInputStream("e:/test.jpg");
System.out.println(fs.available());
byte[] array = new byte[fs.available()];
fs.read(array);
pi.printByteArray(array);
} catch (Exception e) {
System.out.println(e);
}
}
}