对于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);
}
}
}
闽公网安备 35060202000074号