项目中需要用到热敏打印机,并且在打印的同时,要生成二维码。并且打印,先说说二维码的生成吧
package com.jinke.util;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
/**
* 二维码生成
* 李亚飞
*/
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
}
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
public class QRCodeEvents {
/**
* @discription 生成二维码图片,返回Image对象
* @author 李亚飞
* @created 2016年2月3日 上午11:42:21
* @param text 二维码文字
* @return
* @throws Exception
*/
public Image writeQrCodeContent(String text, String path) throws Exception {
int width = 100; // 二维码图片宽度
int height = 100; // 二维码图片高度
String format = "jpg";// 二维码的图片格式
Hashtable
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
// 生成二维码
File outputFile = new File(path + File.separator + "1.jpg");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
File file=new File(path+"1.jpg");
InputStream is = new FileInputStream(file);
BufferedImage bi;
bi = ImageIO.read(is);
java.awt.Image im = (java.awt.Image)bi;
return im;
}
}
这个是生成二维码的,再说说java调用热敏打印机的吧
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.jinke.util.QRCodeEvents;
/**
* 通用热敏打印机类
*/
public class Prient implements Printable {
/**
* 打印内容主题
*/
private List
/**
* 小票标题
*/
private String title;
/**
* 打印纸高度
*/
private Integer height;
/**
* 标题距左距离
*/
private Integer marginLeft = 50;
/**
* 标题距上距离
*/
private Integer marginTop = 10;
public int commonPrint(){
int height = 0 + listModel.size() * 20 + this.height;
// 通俗理解就是书、文档
Book book = new Book();
// 打印格式
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(230, height);
p.setImageableArea(5, -20, 230, height + 20);
pf.setPaper(p);
// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new Prient(this.listModel, this.title), pf);
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(book);
try {
job.print();
} catch (PrinterException e) {
System.out.println("================打印出现异常");
}
return 0;
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(new Font(title, Font.PLAIN, 12));
g2d.drawString(title, marginLeft, marginTop);
g2d.setFont(new Font(title, Font.PLAIN, 8));
int i = 1;
g2d.drawString("--------------------------------------------", 7, 20);
for (PrintModel m : listModel) {
if(!m.getIsWrap()){
if(StringUtils.isNotBlank(m.getName())){
g2d.drawString(m.getName() + m.getVal(), 7, 30+i*12);
}else if (null != m.getImage()) {
g2d.drawImage(m.getImage(), 15, 20+i*12, null);
i+=8;
}
}else {
g2d.drawString(m.getName(), 7, 30+i*12);
i++;
g2d.drawString(m.getVal(), 7, 30+i*12);
}
i++;
}
return PAGE_EXISTS;
}
public static void main(String[] args) {
List
oo.add(new PrintModel("编号1", "0055726"));
try {
oo.add(new PrintModel(new QRCodeEvents().writeQrCodeContent("text", "D:/")));
} catch (Exception e1) {
}
Prient p = new Prient(oo, "运输单", 340);
p.commonPrint();
}
public Prient(List
this.listModel = listModel;
this.title = title;
this.height = height;
this.marginLeft = marginLeft;
this.marginTop = marginTop;
}
public Prient(List
this.listModel = listModel;
this.title = title;
this.height = height;
}
public Prient(List
this.listModel = listModel;
this.title = title;
}
}
里面封装了一个model,用于支持多地方调用。
import java.awt.Image;
/**
* 热敏打印机辅助类
*/
public class PrintModel {
/**
* 变量前面的文字描述
*/
private String name;
/**
* 变量值
*/
private String val;
/**
* 是否换行打印
*/
private Boolean isWrap = false;
/**
* 图片信息
*/
private Image image;
public PrintModel(){
}
public PrintModel(String name, String val) {
this.name = name;
this.val = val;
}
public PrintModel(Image image) {
this.image = image;
}
public PrintModel(String name, String val, Boolean isWrap) {
this.name = name;
this.val = val;
this.isWrap = isWrap;
}
/**
* 变量前面的文字描述
*/
public String getName() {
return name;
}
/**
*
*/
public void setName(String name) {
this.name = name;
}
/**
* 变量值
*/
public String getVal() {
return val;
}
/**
*
*/
public void setVal(String val) {
this.val = val;
}
/**
* 是否换行打印
*/
public Boolean getIsWrap() {
return isWrap;
}
/**
*
*/
public void setIsWrap(Boolean isWrap) {
this.isWrap = isWrap;
}
/**
* 图片信息
*/
public Image getImage() {
return image;
}
/**
*
*/
public void setImage(Image image) {
this.image = image;
}
}
使用
List
pmList.add(new PrintModel("编号:", billTransport.getTransportCode()));//文字打印
pmList.add(new PrintModel(new QRCodeEvents().writeQrCodeContent("运输单号:"+billTransport.getTransportCode(), request.getRealPath("tmp")+"/")));//生成二维码的同时,添加到打印队列中
Prient p = new Prient(pmList, "运输单(司机)", 340, 10, 10);//设置打印的标题
p.commonPrint();
代码下载:
http://download.csdn.net/detail/muzi45ya/9427292