JAVA连接打印机打印小票、产品信息。标签打印机
2018年写了一个标签打印机(TSC TX600),标签打印程序,实现功能是通过串口读出设备ID生成二维码和设备信息,用于esp8266 smart config配网。源码和资料:https://download.csdn.net/download/u012854263/12080191
package com.aiyun.print;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
public class PrintLabel {
public static void CreateQRCode(String content, String path, String fileName) {
// 二维码生成
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = null;
try {
File file = new File(path, fileName + ".jpg");
bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 17, 17, hints);
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void PrintInit(String ID, String QRCodeName1) throws PrinterException {
// 通俗理解就是书、文档
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper paper = new Paper();
// 1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595×842点
// A4纸的尺寸是209*296 点数是209/25.4*72=593 296/25.4*72=839
//85 57
// ------新版本标签规格 30*20---------
int label_width_new = 85;
int label_height_new = 57;
paper.setSize(label_width_new, label_height_new);
paper.setImageableArea(0, 0, label_width_new, label_height_new);// 设置打印范围
// ------旧版本标签规格 35*25---------
// int label_width_old = 100;
// int label_height_old = 70;
// paper.setSize(label_width_old, label_height_old);
// paper.setImageableArea(0, 0, label_width_old, label_height_old);// 设置打印范围
// 设置打印配置
pf.setPaper(paper);
// 添加打印类
QRCodeInfo qrcode = new QRCodeInfo(ID, QRCodeName1);
book.append(qrcode, pf);
// 设置打印数据的格式,此处为图片gif格式
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.GIF;
// 设置打印属性
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));// 打印份数
// 查找所有打印服务
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// 将所有查找出来的打印机与自己想要的打印机进行匹配,找出自己想要的打印机
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++) {
// System.out.println("service found: " + services[i]);
String svcName = services[i].toString();
if (svcName.contains("TSC TX600")) { // 指定打印机
//if (svcName.contains("AYZN_Print")) { // 指定打印机
//if (svcName.contains("ZMIN X1i")) { // 指定打印机
System.out.println("OK");
myPrinter = services[i];
}
}
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
// 获取打印服务对象
if (myPrinter != null) {
try {
job.setPrintService(myPrinter);
// 设置打印类
job.setPageable(book);
// 打印
// System.out.println("--》打印数据");
job.print();
} catch (Exception pe) {
pe.printStackTrace();
}
} else {
System.out.println("no printer services found");
}
}
}
2016:
1.商品信息类
package com.smartsuppermarket.commodity;
public class Commodity {
// 商品名称
private String name;
// 单价
private String unit_price;
// 数量
private String num;
// 总价
private String sum;
public Commodity(String name, String unit_price, String num, String sum, String barcode) {
super();
this.name = name;
this.unit_price = unit_price;
this.num = num;
this.sum = sum;
this.barcode = barcode;
}
// 条码
private String barcode;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUnit_price() {
return unit_price;
}
public void setUnit_price(String unit_price) {
this.unit_price = unit_price;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getSum() {
return sum;
}
public void setSum(String sum) {
this.sum = sum;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode;
}
}
2.打印布局类
package com.smartsuppermarket.printer;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.Calendar;
import com.smartsuppermarket.commodity.Commodity;
//实现Printable接口 用于创建打印内容
public class SalesTicket implements Printable {
private ArrayList<Commodity> list;
private String cashier;
private Font font;
private String sale_num;
private String sale_sum;
private String practical;
private String changes;
private String orders;
// 构造函数
public SalesTicket(ArrayList<Commodity> list, String cashier, String orders, String sale_num, String sale_sum,
String practical, String changes) {
this.list = list;
// 收银员编号
this.cashier = cashier;
// 订单标号
this.orders = orders;
// 商品总数
this.sale_num = sale_num;
// 总金额
this.sale_sum = sale_sum;
// 实收
this.practical = practical;
// 找零
this.changes = changes;
}
/**
* @param Graphic指明打印的图形环境
* @param PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595×
* 842点)
* @param pageIndex指明页号
**/
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
Component c = null;
// 转换成Graphics2D 拿到画笔
Graphics2D g2 = (Graphics2D) graphics;
// 设置打印颜色为黑色
g2.setColor(Color.black);
// 打印起点坐标
double x = pageFormat.getImageableX();
double y = pageFormat.getImageableY();
// 虚线
float[] dash1 = { 4.0f };
// width - 此 BasicStroke 的宽度。此宽度必须大于或等于 0.0f。如果将宽度设置为
// 0.0f,则将笔划呈现为可用于目标设备和抗锯齿提示设置的最细线条。
// cap - BasicStroke 端点的装饰
// join - 应用在路径线段交汇处的装饰
// miterlimit - 斜接处的剪裁限制。miterlimit 必须大于或等于 1.0f。
// dash - 表示虚线模式的数组
// dash_phase - 开始虚线模式的偏移量
// 设置画虚线
g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 4.0f, dash1, 0.0f));
// 设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)
font = new Font("宋体", Font.PLAIN, 11);
g2.setFont(font);// 设置字体
float heigth = font.getSize2D();// 字体高度
// 标题
g2.drawString("广西机电工程学校智能超市", (float) x, (float) y + heigth);
float line = 2 * heigth;
font = new Font("宋体", Font.PLAIN, 8);
g2.setFont(font);// 设置字体
heigth = font.getSize2D();// 字体高度
// 显示收银员
g2.drawString("收银员:" + cashier, (float) x, (float) y + line);
// 显示订单号
g2.drawString("订单号:" + orders, (float) x + 100, (float) y + line);
line += heigth;
// 显示标题
g2.drawString("名称", (float) x + 20, (float) y + line);
g2.drawString("单价", (float) x + 60, (float) y + line);
g2.drawString("数量", (float) x + 85, (float) y + line);
g2.drawString("总额", (float) x + 115, (float) y + line);
line += heigth;
g2.drawLine((int) x, (int) (y + line), (int) x + 158, (int) (y + line));
// 第4行
line += heigth;
// 显示内容
for (int i = 0; i < list.size(); i++) {
Commodity commodity = list.get(i);
g2.drawString(commodity.getName(), (float) x, (float) y + line);
line += heigth;
g2.drawString(commodity.getBarcode(), (float) x, (float) y + line);
g2.drawString(commodity.getUnit_price(), (float) x + 60, (float) y + line);
g2.drawString(commodity.getNum(), (float) x + 90, (float) y + line);
g2.drawString(commodity.getSum(), (float) x + 120, (float) y + line);
line += heigth;
}
line += heigth;
g2.drawLine((int) x, (int) (y + line), (int) x + 158, (int) (y + line));
line += heigth;
g2.drawString("售出商品数:" + sale_num + "件", (float) x, (float) y + line);
g2.drawString("合计:" + sale_sum + "元", (float) x + 70, (float) y + line);
line += heigth;
g2.drawString("实收:" + practical + "元", (float) x, (float) y + line);
g2.drawString("找零:" + changes + "元", (float) x + 70, (float) y + line);
line += heigth;
g2.drawString("时间:" + Calendar.getInstance().getTime().toLocaleString(), (float) x, (float) y + line);
line += heigth;
g2.drawString("天天平价,日日新鲜", (float) x + 20, (float) y + line);
line += heigth;
g2.drawString("钱票请当面点清,离开柜台恕不负责", (float) x, (float) y + line);
switch (pageIndex) {
case 0:
return PAGE_EXISTS;
default:
return NO_SUCH_PAGE;
}
}
}
3.打印小票
/**
* 打印销售小票
*
* @param order
* 订单号
* @param num
* 数量
* @param sum
* 总金额
* @param practical
* 实收
* @param change
* 找零
*/
private void PrintSale(String order, String num, String sum, String practical, String change) {
try {
// 通俗理解就是书、文档
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
ArrayList<Commodity> cmd_list = new ArrayList<Commodity>();
// 取出数据
for (int i = 0; i < list.size(); i++) {
Collect c = list.get(i);
Commodity cd = new Commodity(c.getName(), String.valueOf(c.getSell()), String.valueOf(c.getNum()),
String.valueOf(c.getTotal()), c.getCode());
cmd_list.add(cd);
}
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper paper = new Paper();
paper.setSize(158, 30000);// 纸张大小
paper.setImageableArea(0, 0, 158, 30000);// A4(595 X
// 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72
pf.setPaper(paper);
book.append(new SalesTicket(cmd_list, Windows.user_num, order, num, sum, practical, change), pf);
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
// 设置打印类
job.setPageable(book);
job.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
//-------使用方法----------
// 订单编号 数量 总额 实收 找零 负责人
String num = String.valueOf(number);
String sum1 = String.valueOf(CD.getSum());
String ptc = CD.getPractical();
String change = String.valueOf(CD.getChange());
String mess[] = { order, num, sum1, ptc, change, Windows.user_num };
// 插入数据到订单销售表
tm.InsertOrders(mess);
// 打印小票
PrintSale(order, num, sum1, ptc, change);
原文作者:major_jm
原文地址: https://blog.csdn.net/u012854263/article/details/51137097
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/u012854263/article/details/51137097
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章