目录
1、生成二维码,并调用画布进行二维码和文字的描绘。
2、主程序:获取打印机,并打印内容
3、打印效果
参考文献:
前期工作是安装好打印机驱动,可连接打印机。
添加三个依赖jar:
具体见文底的参考文献。
package print;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* 此类,用于构建打印内容。
* @author Administrator
*
*/
public class LabelPrintable implements Printable {
private String serNo = "643566786420230921054158";
private String f1="45.5";
private String f2="46.5";
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// 绘制二维码
BufferedImage img = createImage(serNo);
// 将二维码绘制到打印机
g2d.drawImage(img, 0, 0, 35, 35, null);
// 设置字体大小
Font font = new Font("Arial", Font.PLAIN, 7);
g2d.setFont(font);
// 将文字绘制到打印机
g2d.drawString(serNo.substring(0, serNo.length()-14), 40, 7);
g2d.drawString(serNo.substring(serNo.length()-14), 40, 16);
g2d.drawString("FMAX1:"+f1, 40, 25);
g2d.drawString("FMAX5:"+f2, 40, 34);
g2d.dispose();
return PAGE_EXISTS;
}
/**
* 生成二维码
* @param serNo
* @return
*/
private BufferedImage createImage(String serNo) {
//图片宽度、高度,二维码和边框的边距
//二维码的容错等级
Map map = new HashMap();
map.put(EncodeHintType.MAX_SIZE, 100);
//设置编剧(二维码与图片编剧边框的距离)枚举
map.put(EncodeHintType.MARGIN,2);
//设置容错的等级 有四级 L(7%) M(15%) Q(25%) H(30%)
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//设置字符集,避免中文乱码的情况
map.put(EncodeHintType.CHARACTER_SET,"utf-8");
//二维码的输出流 IO流
MultiFormatWriter mfw = new MultiFormatWriter();
try{
//通过流创建一个二维矩阵643566786420230921054158
BitMatrix bm = mfw.encode(serNo, BarcodeFormat.QR_CODE,400,400,map);
//声明一张图片来回值二维码
BufferedImage img = new BufferedImage(400,400,BufferedImage.TYPE_INT_BGR);
//将二维码的矩阵回值到图片上
//重复的做某件事就使用循环结构
for (int i = 0; i < 400; i++) {
for (int j = 0; j < 400; j++) {
//声明一个颜色
int rgb = 0;
//画矩阵图
rgb = bm.get(i,j) ? Color.BLACK.getRGB() : Color.WHITE.getRGB();
//将内容绘制到图片中
img.setRGB(i,j,rgb);
}
}
//指定二维码的存储路径
// ImageIO.write(img,"jpg",new File("D:/简书主页.jpg"));
System.out.println("二维码生成完成");
return img;
// }catch(WriterException | IOException e){
}catch(WriterException e){
e.printStackTrace();
}
return null;
}
}
画布的核心代码是:
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// 绘制二维码
BufferedImage img = createImage(serNo);
// 将二维码绘制到打印机
g2d.drawImage(img, 0, 0, 35, 35, null);
// 设置字体大小
Font font = new Font("Arial", Font.PLAIN, 7);
g2d.setFont(font);
// 将文字绘制到打印机
g2d.drawString(serNo.substring(0, serNo.length()-14), 40, 7);
g2d.drawString(serNo.substring(serNo.length()-14), 40, 16);
g2d.drawString("FMAX1:"+f1, 40, 25);
g2d.drawString("FMAX5:"+f2, 40, 34);
g2d.dispose();
g2d.drawImage(img, 0, 0, 35, 35, null);
以上是将二维码绘制到坐标0.0,图像宽度35、高度35
// 将文字绘制到打印机
g2d.drawString(serNo.substring(0, serNo.length()-14), 40, 7);
g2d.drawString(serNo.substring(serNo.length()-14), 40, 16);
g2d.drawString("FMAX1:"+f1, 40, 25);
g2d.drawString("FMAX5:"+f2, 40, 34);
以上是将四行文字绘制到画布里x坐标40。
以上坐标根据具体情况和打印机进行调整。
package print;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
public class MainPrint {
public static void main(String[] args) {
try {
// 连接到标签打印机
PrintService printService = getLabelPrinter();
// 创建打印任务
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintService(printService);
// 设置打印内容
Printable printable = new LabelPrintable();
printerJob.setPrintable(printable);
// 发送打印任务
printerJob.print();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查找打印机
* @return
*/
private static PrintService getLabelPrinter() {
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService printService : printServices) {
System.out.println(printService.getName());
// 查找标签打印机Label PPLB
if (printService.getName().contains("Label")) {
return printService;
}
}
return null;
}
}
1、
小项目教学——用Java生成二维码_程序员小橙的博客-CSDN博客
2、
java调用标签打印机_mob649e81630984的技术博客_51CTO博客