創(chuàng)建實體類
public class Test {
//條形碼
private String username;
private String password;
// 菜品名稱
private String name;
// 價格
private double money;
// 數(shù)量
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;
}
}
創(chuàng)建工具類
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>();
// 設(shè)置小票打印
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
// 設(shè)置顏色
g2d.setColor(Color.BLACK);
//模式 字體 字體大小
g2d.setFont(new Font("Default", Font.PLAIN, 30));
// 參數(shù)1:顯示內(nèi)容 參數(shù)2:橫向偏移 參數(shù)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("優(yōu)惠金額:¥" + 20, 40, H1 + 60);
g2d.drawString("應(yīng)收:¥" + (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;
/**
* 設(shè)置 條形碼參數(shù)
*/
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
// 設(shè)置編碼方式
put(EncodeHintType.CHARACTER_SET, "utf-8");
}
};
/**
* 生成 圖片緩沖
* @author fxbin
* @param vaNumber VA 碼
* @return 返回BufferedImage
*/
public static BufferedImage getBarCode(String vaNumber){
try {
Code128Writer writer = new Code128Writer();
// 編碼內(nèi)容, 編碼類型, 寬度, 高度, 設(shè)置參數(shù)
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);
// 設(shè)置白色
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;
}
/**
* 設(shè)置 Graphics2D 屬性 (抗鋸齒)
* @param g2d Graphics2D提供對幾何形狀、坐標(biāo)轉(zhuǎn)換渴析、顏色管理和文本布局更為復(fù)雜的控制
*/
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);
}
/**
* 設(shè)置背景為白色
* @param g2d Graphics2D提供對幾何形狀箱玷、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制
*/
private static void setColorWhite(Graphics2D g2d){
g2d.setColor(Color.WHITE);
//填充整個屏幕
g2d.fillRect(0,0,600,600);
//設(shè)置筆刷
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 "成功";
// }
}
開始調(diào)試
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) {
//這里是測試用的數(shù)據(jù)絮吵,自己定義就好了
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);
// 設(shè)置小票模型菜品集合
Prient.setTestList(testList);
// 定義紙張高度
int height = 500 + (testList.size() * 20) + 160;//這里根據(jù)紙張的大小
// 通俗理解就是書弧烤、文檔
Book book = new Book();
// 打印格式
PageFormat pf = new PageFormat();
// 原點在紙張的左上方,x 指向右方,y 指向下方暇昂。
pf.setOrientation(PageFormat.PORTRAIT);
// 通過Paper設(shè)置頁面的空白邊距和可打印區(qū)域莺戒。必須與實際打印紙張大小相符。
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);
// 指定打印機(jī)打哟硬(printerName打印機(jī)名稱)
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("無法找到打印機(jī):" + printerName);
return;
}
// 獲取打印服務(wù)對象
PrinterJob job = PrinterJob.getPrinterJob();
// 寫入書
job.setPageable(book);
try {
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
System.out.println("Z"+System.currentTimeMillis()+random6.toString());
// 添加指定的打印機(jī)
job.setPrintService(pss[0]);
// 打印執(zhí)行
job.print();
} catch (PrinterException e) {
System.out.println("================打印出現(xiàn)異常");
}
}
}
導(dǎo)入依賴
<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>