public class PrintTools {
private static final String TAG = PrintTools.class.getSimpleName();
/**
* 根据后台拼接好的内容打印出来
*/
public static void printByBTPrinter(String jsContent) {
printByPrinter(jsContent, "");
}
public static void printByNetPrinter(String jsContent, String ip) {
printByPrinter(jsContent, ip);
}
private static void printByPrinter(String jsContent, String ip) {
boolean bluePrint = TextUtils.isEmpty(ip);
EscCommand esc = new EscCommand();
if (esc != null) {
esc.addPrintAndFeedLines((byte) 8);
esc.addCutPaper();
List<PrintLineData> content = GsonUtils.gsonToList(jsContent, PrintLineData.class);
if (TextUtils.isEmpty(jsContent)) {
return;
}
for (PrintLineData line : content) {
if (checkNonNull(line.getColumns())) {
if (line.getColumns().size() > 1) {//多列
for (PrintLineData.ColumnsBean column : line.getColumns()) {
//加粗
boolean isBold = column.getBold() == 1;
esc.addSelectPrintModes(EscCommand.FONT.FONTA, isBold ? EscCommand.ENABLE.ON : EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);
//设置字体大小
setFontSize(esc, column.getFontSize());
esc.addSelectJustification(column.getTextAlignment());
esc.addText(column.getContent());
}
esc.addText("\n");
} else {
PrintLineData.ColumnsBean column = line.getColumns().get(0);
if ("barcode".equals(column.getContentType())) {//如果是条形码
if (!TextUtils.isEmpty(column.getContent())) {
//esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);
esc.addSelectJustification(column.getTextAlignment());
// esc.addSelectPrintingPositionForHRICharacters(EscCommand.HRI_POSITION.BELOW);//设置条码可识别字符位置在条码下方
esc.addSetBarcodeHeight((byte) 120); //设置条码高度为120点
esc.addSetBarcodeWidth((byte) 2); // 设置条码单元宽度为1
esc.addCODE128(esc.genCode128(column.getContent().trim()));
esc.addPrintAndLineFeed();
}
} else {
//加粗
boolean isBold = column.getBold() == 1;
esc.addSelectPrintModes(EscCommand.FONT.FONTA, isBold ? EscCommand.ENABLE.ON : EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);
setFontSize(esc, column.getFontSize());
esc.addSelectJustification(column.getTextAlignment());
esc.addText(column.getContent() + "\n");
}
}
}
}
esc.addPrintAndFeedLines((byte) 1);
esc.addCutPaper();
Vector<Byte> datas = esc.getCommand();
if (bluePrint) {
// 发送数据
if (PrinterManager.getInstance().getBleManager() != null) {
PrinterManager.getInstance().getBleManager().handlePrint(datas, "打印时间");
}
} else {
if (PrinterManager.getInstance().getNetManager(ip) != null) {
ThreadPool.getInstantiation().addTask(() -> {
PrinterManager.getInstance().getNetManager(ip).handlePrint(datas, "打印时间");
});
} else {
PrinterManager.getInstance().connectNet(ip, "网络打印机", datas, "打印时间");
}
}
}
}
/**
* 动态设置字体大小
*
* @param esc Escommond
* @param fontSize 字体大小(large,middle,以及正常字体)
*/
private static void setFontSize(EscCommand esc, String fontSize) {
switch (fontSize) {
case "large":
esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.ON, EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF);
break;
case "middle":
esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);
break;
default:
esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);
break;
}
}
}