|
@@ -1,7 +1,9 @@
|
|
|
package com.gzlh.device.print.test;
|
|
|
|
|
|
import cn.hutool.core.codec.Base64Decoder;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.thread.ThreadUtil;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
@@ -16,6 +18,7 @@ import com.gzlh.device.led.factory.LedFactory;
|
|
|
import com.gzlh.device.led.utils.LedOptions;
|
|
|
import com.gzlh.device.print.bo.BeiHaiWeightReport;
|
|
|
import com.gzlh.utils.EpsonCommands;
|
|
|
+import com.gzlh.utils.WordHandlerUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
@@ -25,6 +28,7 @@ import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.Socket;
|
|
|
import java.nio.charset.Charset;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
/**
|
|
|
* 映美打印机
|
|
@@ -41,43 +45,56 @@ public class JMPrintThread implements Runnable {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
try {
|
|
|
- log.info("start print:{}",JSONUtil.toJsonStr(printBO));
|
|
|
+ log.info("start print:{}", JSONUtil.toJsonStr(printBO));
|
|
|
SystemObject.printFactory.handler(SysConfig.serialSetting.getPrint().getBrand()).close();
|
|
|
- Charset charset = Charset.forName("GBK");
|
|
|
- String title = SysConfig.serialSetting.getPrint().getPrintTitle() + "\n";
|
|
|
+ Charset charset = Charset.forName("GB2312");
|
|
|
+ String title = " "+SysConfig.serialSetting.getPrint().getPrintTitle() + "\n";
|
|
|
TcpIpOutputStream tcpIpOutputStream = new TcpIpOutputStream(SysConfig.serialSetting.getPrint().getIp(), SysConfig.serialSetting.getPrint().getPort());
|
|
|
EscPos escpos = new EscPos(tcpIpOutputStream);
|
|
|
- escpos.setCharsetName("GB2312");
|
|
|
ThreadUtil.sleep(2000);
|
|
|
Bitonal algorithm = new BitonalThreshold(160);
|
|
|
escpos.initializePrinter();
|
|
|
+ escpos.setCharsetName("GB2312");
|
|
|
+ byte[] zhFontSize = hexToBytes("1C2108");
|
|
|
+ byte[] titleData = EpsonCommands.mergerByteArray(
|
|
|
+ hexToBytes("1B6101"),
|
|
|
+ hexToBytes("1C210B"),
|
|
|
+ title.getBytes(charset),
|
|
|
+ hexToBytes("0D0A")
|
|
|
+ );
|
|
|
+ escpos.write(titleData, 0, titleData.length);
|
|
|
+ byte[] numberFontSize = hexToBytes("1B2110");
|
|
|
+ String text =
|
|
|
+ " 货主/货代: " + printBO.getWWB_CST_OWNER() + " \n" +
|
|
|
+ " 船名: " + printBO.getWWB_VLS_VCNNM() + "\n" +
|
|
|
+ " 船次: " + printBO.getWWB_VIW_VOYAGE() + " \n" +
|
|
|
+ " 唛头: " + printBO.getWB_MARKERNO() + " \n" +
|
|
|
+ " 货名: " + printBO.getWWB_GOODSNAME() + "\n" +
|
|
|
+ " 车号: " + printBO.getWWB_TRUCKNO() + "\n" +
|
|
|
+ " 毛重: " + printBO.getWWB_CAR_GW() + "吨\n" +
|
|
|
+ " 皮重: " + printBO.getWWB_CAR_WGT() + "吨\n" +
|
|
|
+ " 净重: " + printBO.getWWB_CAR_NW() + "吨\n" +
|
|
|
+ " 过磅地点: " + printBO.getWWB_PLACE_CODE() + "\n" +
|
|
|
+ " 二次过磅时间: " + printBO.getWWB_DATE_SEC() + "\n" +
|
|
|
+ " 客户备注: " + printBO.getTK_REMARK() + "\n" +
|
|
|
+ " 地磅备注: " + printBO.getWWB_REMARK();
|
|
|
+ int len = text.length();
|
|
|
+ byte[] printData = new byte[]{};
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+ String at = text.charAt(i) + "";
|
|
|
+ byte[] atByte = at.getBytes(charset);
|
|
|
+ if (NumberUtil.isNumber(at)) {
|
|
|
+ atByte=at.getBytes(StandardCharsets.UTF_8);
|
|
|
+ printData = EpsonCommands.mergerByteArray(printData,numberFontSize, atByte);
|
|
|
+ } else {
|
|
|
+ printData = EpsonCommands.mergerByteArray(printData,zhFontSize, atByte);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // byte[] str = text.getBytes(charset);
|
|
|
+ byte[] data = EpsonCommands.mergerByteArray(
|
|
|
+ EpsonCommands.printLineHeight((byte) 30), EpsonCommands.alignLeft(),
|
|
|
+ printData, EpsonCommands.printLineFeed((byte) 1));
|
|
|
|
|
|
- Style titleStyle = new Style();
|
|
|
- titleStyle.setBold(true);
|
|
|
- titleStyle.setFontSize(Style.FontSize._1, Style.FontSize._1);
|
|
|
- titleStyle.setJustification(EscPosConst.Justification.Center);
|
|
|
-
|
|
|
- escpos.writeLF(titleStyle,title);
|
|
|
-// escpos.write(titleData, 0, titleData.length);
|
|
|
- String text = "船名: " + printBO.getWWB_VLS_VCNNM() + "\n" +
|
|
|
- "船次: " + printBO.getWWB_VIW_VOYAGE() + " \n" +
|
|
|
- "仓单号: " + printBO.getWB_BILLNO() + " \n" +
|
|
|
- "货名: " + printBO.getWWB_GOODSNAME() + " \n" +
|
|
|
- "货主单位: " + printBO.getWWB_CST_OWNER() + "\n" +
|
|
|
- "车号: " + printBO.getWWB_TRUCKNO() + "\n" +
|
|
|
- "一次过磅时间: " + printBO.getWWB_DATE_FST() + "\n" +
|
|
|
- "毛重: " + printBO.getWWB_CAR_GW() + "吨\n" +
|
|
|
- "二次过磅时间: " + printBO.getWWB_DATE_SEC() + "\n" +
|
|
|
- "皮重: " + printBO.getWWB_CAR_WGT() + "吨\n" +
|
|
|
- "净重: " + printBO.getWWB_CAR_NW() + "吨\n" +
|
|
|
- "件数/重量/体积: " + printBO.getWWB_PKSPAC() + "/" + printBO.getWWB_WGT() + "/" + printBO.getWWB_GTVOL() + "\n" +
|
|
|
- "地点: " + printBO.getWWB_PLACE_CODE() + "\n" +
|
|
|
- "备注: " + printBO.getWWB_REMARK();
|
|
|
- byte[] str = text.getBytes(charset);
|
|
|
- byte[] data = EpsonCommands.mergerByteArray(EpsonCommands.fontSizeSetBig(3),
|
|
|
- EpsonCommands.printLineHeight((byte) 40),
|
|
|
- EpsonCommands.emphasizedSetting(false), EpsonCommands.alignLeft(),
|
|
|
- str, EpsonCommands.printLineFeed((byte) 1));
|
|
|
escpos.write(data, 0, data.length);
|
|
|
String base64Img = SysConfig.serialSetting.getPrint().getChapter();
|
|
|
if (StrUtil.isNotEmpty(base64Img)) {
|
|
@@ -85,24 +102,41 @@ public class JMPrintThread implements Runnable {
|
|
|
.read(new ByteArrayInputStream(Base64Decoder.decode(base64Img)));
|
|
|
EscPosImage escposImage = new EscPosImage(new CoffeeImageImpl(githubBufferedImage), algorithm);
|
|
|
BitImageWrapper imageWrapper = new BitImageWrapper();
|
|
|
- imageWrapper.setMode(BitImageWrapper.BitImageMode._24DotDoubleDensity_Default);
|
|
|
+ imageWrapper.setMode(BitImageWrapper.BitImageMode._8DotSingleDensity);
|
|
|
escpos.write(imageWrapper, escposImage);
|
|
|
}
|
|
|
- escpos.feed(5);
|
|
|
- escpos.cut(EscPos.CutMode.FULL);
|
|
|
+ byte[] footer = EpsonCommands.mergerByteArray(EpsonCommands.printLineFeed((byte) 1), EpsonCommands.feedPaperCut());
|
|
|
+ escpos.write(footer, 0, footer.length);
|
|
|
escpos.close();
|
|
|
log.info("print done======:{}", JSONUtil.toJsonStr(printBO));
|
|
|
- LedFactory ledFactory= SpringUtil.getBean(LedFactory.class);
|
|
|
+ LedFactory ledFactory = SpringUtil.getBean(LedFactory.class);
|
|
|
String line = SysConfig.serialSetting.getLed().getLine();
|
|
|
if (StrUtil.isEmpty(line)) {
|
|
|
line = "03";
|
|
|
}
|
|
|
LedOptions options = new LedOptions().setLine(line);
|
|
|
- ledFactory.handler(SysConfig.serialSetting.getLed().getBrand()).sendMsg("已成功打印小票,请取走小票",options);
|
|
|
+ ledFactory.handler(SysConfig.serialSetting.getLed().getBrand()).sendMsg("已成功打印小票,请取走小票", options);
|
|
|
} catch (IOException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public static String printHex(byte[] bytes) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (byte b : bytes) {
|
|
|
+ sb.append(String.format("%02X", b));
|
|
|
+ }
|
|
|
+ return sb.toString().toUpperCase();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] hexToBytes(String hex) {
|
|
|
+ int len = hex.length();
|
|
|
+ byte[] data = new byte[len / 2];
|
|
|
+ for (int i = 0; i < len; i += 2) {
|
|
|
+ data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
|
|
|
+ + Character.digit(hex.charAt(i + 1), 16));
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
}
|