Java绘制图片并实现打印前生成图片文件进行预览, 适用于开发阶段. 如果要使用界面完成预览,请另找资源.
这里用这个功能主要是用于结果预览,因为如果使用打印来进行结果验证,会浪费大量的纸张.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
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 java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.synnex.reception.util.PLBarCode39;
import com.synnex.reception.vo.VisitInfoVo;
public class PrintTest {
public static void main(String[] args) {
String barCode = "201408210001" ;
String dateStr = "2014/08/25 23:59:59";
String name = "Christopher Adam Hernandez Sotelo";
String visitorCompany = "CHINA Chengdu";
String employeeName = "Marquisha Ardell Eddington-Williams";
int zoom = 6;
BufferedImage resultImage = null;
BufferedImage bufferedImage = new BufferedImage(216*zoom,144*zoom,BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
// g.draw3DRect(0, 0, 216, 144, true);
g.setColor(Color.WHITE);
g.fillRect(0, 0, 216*zoom, 144*zoom);
g.setColor(Color.BLACK);
Image image;
try {
image = ImageIO.read(PrintTest.class.getClassLoader().getResource("logo.bmp").openStream());
if (image != null) {
g.drawImage(image, 6*zoom, 6*zoom, 60*zoom, 24*zoom, null);
}
g.setFont(new Font("Arial", Font.BOLD, 14*zoom));
g.drawString("VISITOR",100*zoom, 24*zoom);
g.setFont(new Font("Arial", Font.BOLD, 10*zoom));
g.drawString("Name:",61*zoom, 42*zoom);
g.drawString("Entry Time:", 37*zoom,63*zoom);
g.drawString("Company Name:", 12*zoom,80*zoom);
g.drawString("Employee Name:", 10*zoom,97*zoom);
g.setFont(new Font("Arial", Font.PLAIN, 8*zoom));
g.drawString(name,92*zoom, 42*zoom);
g.drawString(dateStr, 92*zoom,63*zoom);
g.drawString(visitorCompany, 92*zoom,80*zoom);
g.drawString(employeeName, 92*zoom,97*zoom);
g.drawImage(generateBarcodeImage(barCode), 40*zoom, 112*zoom, 152*zoom, 30*zoom, null);
g.dispose();
resultImage = bufferedImage;//rotate(bufferedImage, 360);
// encode: com.sun.image.codec.jpeg
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("C:\\test.jpg")));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(resultImage);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(resultImage);
}catch(IOException ioe) {
ioe.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
//
// print(resultImage);
}
上面代码主要是先绘制自己要产生的图片, 然后将图片输出到文件, 手动打开文件即可查看结果. 其中如下代码为导出图片到磁盘:
// encode: com.sun.image.codec.jpeg
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("C:\\test.jpg")));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(resultImage);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(resultImage);
}catch(IOException ioe) {
ioe.printStackTrace();
}