创建实体类
public class Test {
//条形码
private String username;
private String password;
// 菜品名称
private String name;
// 价格
private double money;
// 数量
private Integer num;
//菜品分类
private String fenlei;
public Test() {
super();
}
public Test(String name, double money, Integer num) {
super();
this.name = name;
this.money = money;
this.num = num;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getFenlei() {
return fenlei;
}
public void setFenlei(String fenlei) {
this.fenlei = fenlei;
}
}
创建工具类
import com.example.demo.pojo.Test;
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.ArrayList;
import java.util.List;
import static com.example.demo.utils.GoogleBarCodeUtils.getBarCode;
import static com.example.demo.utils.GoogleBarCodeUtils.insertWords;
public class Prient implements Printable{
//条形码
public static Test testt = new Test();
// 菜品集合
public static List<Test> testList = new ArrayList<Test>();
// 设置小票打印
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
// 设置颜色
g2d.setColor(Color.BLACK);
//模式 字体 字体大小
g2d.setFont(new Font("Default", Font.PLAIN, 30));
// 参数1:显示内容 参数2:横向偏移 参数3:纵向偏移
g2d.drawString("点菜清单", 100, 50);
g2d.drawString("------------------------------------------------", 40, 70);
g2d.setFont(new Font("Default", Font.PLAIN, 20));
g2d.drawString("点餐员:自定义", 40, 90);
g2d.drawString("电话:自定义", 40, 110);
g2d.drawString("用餐时间:自定义", 40, 130);
g2d.drawString("用餐地址:打印测试", 40, 150);
g2d.setFont(new Font("Default", Font.PLAIN, 20));
g2d.drawString("------------------------------------------------", 40, 170);
g2d.drawString("菜品 单价 小计", 40, 190);
g2d.setFont(new Font("Default", Font.PLAIN, 20));
int H1 = 190;
double zmoney = 0;
int count = 0;
for (Test test : testList) {
count = count + 1;
H1 = H1 + 20;
zmoney = test.getMoney() * test.getNum() + zmoney;
g2d.drawString(count + "." + test.getName() + "(" + test.getNum()
+ "份) ¥" + test.getMoney()+" ¥"+test.getMoney()*test.getNum(), 40, H1);
}
g2d.setFont(new Font("Default", Font.PLAIN, 20));
g2d.drawString("------------------------------------------------", 40, H1 + 20);
g2d.setFont(new Font("Default", Font.PLAIN, 20));
g2d.drawString("合计:¥" + zmoney, 40, H1 + 40);
g2d.drawString("优惠金额:¥" + 20, 40, H1 + 60);
g2d.drawString("应收:¥" + (zmoney-20), 40, H1 + 80);
g2d.drawString("实收:¥" + zmoney, 40, H1 + 100);
g2d.drawString("找零:¥" + 20, 40, H1 + 120);
g2d.drawString("收银员:" + "打印测试", 40, H1 + 140);
g2d.drawString("谢谢惠顾,欢迎下次光临!", 80, H1 + 160);
BufferedImage image = insertWords(getBarCode(testt.getUsername()), testt.getPassword());
g2d.drawImage(image, 70, H1 + 180, null);
return PAGE_EXISTS;
}
public static List<Test> getTestList() {
return testList;
}
public static void Settest(Test text){
Prient.testt=text;
}
public static void setTestList(List<Test> testList) {
Prient.testList = testList;
}
生成条形码
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
@Service
public class GoogleBarCodeUtils {
/** 条形码宽度 */
private static final int WIDTH = 300;
/** 条形码高度 */
private static final int HEIGHT = 50;
/** 加文字 条形码 */
private static final int WORDHEIGHT = 75;
/**
* 设置 条形码参数
*/
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
// 设置编码方式
put(EncodeHintType.CHARACTER_SET, "utf-8");
}
};
/**
* 生成 图片缓冲
* @author fxbin
* @param vaNumber VA 码
* @return 返回BufferedImage
*/
public static BufferedImage getBarCode(String vaNumber){
try {
Code128Writer writer = new Code128Writer();
// 编码内容, 编码类型, 宽度, 高度, 设置参数
BitMatrix bitMatrix = writer.encode(vaNumber, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
/**
* 把带logo的二维码下面加上文字
* @author fxbin
* @param image 条形码图片
* @param words 文字
* @return 返回BufferedImage
*/
public static BufferedImage insertWords(BufferedImage image, String words){
// 新的图片,把带logo的二维码下面加上文字
if (StringUtils.isNotEmpty(words)) {
BufferedImage outImage = new BufferedImage(WIDTH, WORDHEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = outImage.createGraphics();
// 抗锯齿
setGraphics2D(g2d);
// 设置白色
setColorWhite(g2d);
// 画条形码到新的面板
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
// 画文字到新的面板
Color color=new Color(0, 0, 0);
g2d.setColor(color);
// 字体、字型、字号
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 18));
//文字长度
int strWidth = g2d.getFontMetrics().stringWidth(words);
//总长度减去文字长度的一半 (居中显示)
int wordStartX=(WIDTH - strWidth) / 2;
//height + (outImage.getHeight() - height) / 2 + 12
int wordStartY=HEIGHT+20;
// 画文字
g2d.drawString(words, wordStartX, wordStartY);
g2d.dispose();
outImage.flush();
return outImage;
}
return null;
}
/**
* 设置 Graphics2D 属性 (抗锯齿)
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
*/
private static void setGraphics2D(Graphics2D g2d){
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
g2d.setStroke(s);
}
/**
* 设置背景为白色
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
*/
private static void setColorWhite(Graphics2D g2d){
g2d.setColor(Color.WHITE);
//填充整个屏幕
g2d.fillRect(0,0,600,600);
//设置笔刷
g2d.setColor(Color.BLACK);
}
// public String add(addpojo addpojo){
// BufferedImage image = insertWords(getBarCode(addpojo.getUsername()), addpojo.getUsername());
// try {
// ImageIO.write(image, "jpg", new File("D://impl/impl.jpg"));
// } catch (IOException e) {
// e.printStackTrace();
// }
// return "成功";
// }
}
开始调试
import com.example.demo.pojo.Test;
import com.example.demo.utils.Prient;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;
import java.awt.print.*;
import java.util.ArrayList;
import java.util.List;
public class DemoApplicationTests {
public static void main(String[] args) {
//这里是测试用的数据,自己定义就好了
List<Test> testList = new ArrayList<Test>();
Test t1 = new Test("麻辣牛肉", 23.00, 2);
Test t2 = new Test("麻辣牛肉", 23.00, 4);
Test t3 = new Test("精品千层肚", 24.00, 3);
Test t4 = new Test("麻辣牛肉", 23.00, 2);
Test t5 = new Test("极品鲜毛肚", 26.00, 1);
Test t6 = new Test("极品鲜毛肚", 26.00, 1);
Test t7 = new Test("极品鲜毛肚", 26.00, 1);
Test t8 = new Test("极品鲜毛肚", 26.00, 1);
Test t9 = new Test("极品鲜毛肚", 26.00, 1);
testList.add(t1);
testList.add(t2);
testList.add(t3);
testList.add(t4);
testList.add(t5);
testList.add(t6);
testList.add(t7);
testList.add(t8);
testList.add(t9);
Test test2 = new Test();
test2.setUsername("353632");
test2.setPassword("22222");
Prient.Settest(test2);
// 设置小票模型菜品集合
Prient.setTestList(testList);
// 定义纸张高度
int height = 500 + (testList.size() * 20) + 160;//这里根据纸张的大小
// 通俗理解就是书、文档
Book book = new Book();
// 打印格式
PageFormat pf = new PageFormat();
// 原点在纸张的左上方,x 指向右方,y 指向下方。
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
// 页面宽度 页面高度
p.setSize(260, height);
// x轴 y轴 宽度 高度
p.setImageableArea(0, 0, 280, height);
pf.setPaper(p);
// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new Prient(), pf);
// 指定打印机打印(printerName打印机名称)
HashAttributeSet hs = new HashAttributeSet();
String printerName = "Canon MF220 Series";
hs.add(new PrinterName(printerName, null));
PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);
if (pss.length == 0) {
System.out.println("无法找到打印机:" + printerName);
return;
}
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
// 写入书
job.setPageable(book);
try {
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
System.out.println("Z"+System.currentTimeMillis()+random6.toString());
// 添加指定的打印机
job.setPrintService(pss[0]);
// 打印执行
job.print();
} catch (PrinterException e) {
System.out.println("================打印出现异常");
}
}
}
导入依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.7.0</version>
</dependency>