初始化
This commit is contained in:
commit
141566b563
|
@ -0,0 +1,33 @@
|
|||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
|
@ -0,0 +1,14 @@
|
|||
# 安装服务
|
||||
yinyitong-dll-stand.exe install
|
||||
|
||||
# 启动服务
|
||||
yinyitong-dll-stand.exe start
|
||||
|
||||
# 查看服务运行状态
|
||||
yinyitong-dll-stand.exe status
|
||||
|
||||
# 重启服务
|
||||
yinyitong-dll-stand.exe restart
|
||||
|
||||
# 停止服务
|
||||
yinyitong-dll-stand.exe stop
|
Binary file not shown.
|
@ -0,0 +1,8 @@
|
|||
<service>
|
||||
<id>yinyitong-dll-stand</id>
|
||||
<name>yinyitong-dll-stand</name>
|
||||
<description>银医通-台式机-DLL调用服务</description>
|
||||
|
||||
<executable>java</executable>
|
||||
<arguments>-jar %BASE%\yinyitong-dll-stand.jar</arguments>
|
||||
</service>
|
|
@ -0,0 +1,109 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.dpkj</groupId>
|
||||
<artifactId>银医通-澜沧中医院-DLL-台式机</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>ems-express-call-dll</name>
|
||||
<description>ems-express-call-dll</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.18</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<hutool.version>5.8.25</hutool.version>
|
||||
<jna.version>5.14.0</jna.version>
|
||||
<pdfbox.version>3.0.2</pdfbox.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-crypto</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
<!-- 调用DLL -->
|
||||
<dependency>
|
||||
<groupId>net.java.dev.jna</groupId>
|
||||
<artifactId>jna</artifactId>
|
||||
<version>${jna.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.java.dev.jna</groupId>
|
||||
<artifactId>jna-platform</artifactId>
|
||||
<version>${jna.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--PDF转换工具-->
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>pdfbox</artifactId>
|
||||
<version>${pdfbox.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>ems-express-call-dll</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<!--打包时将resource下的文件一起打包-->
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>**/**</include>
|
||||
</includes>
|
||||
<filtering>false</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>dev</id>
|
||||
<properties>
|
||||
|
||||
</properties>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>pro</id>
|
||||
<properties>
|
||||
|
||||
</properties>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,30 @@
|
|||
package com.dpkj;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws UnknownHostException {
|
||||
ConfigurableApplicationContext application = SpringApplication.run(Application.class, args);
|
||||
Environment env = application.getEnvironment();
|
||||
String ip = InetAddress.getLocalHost().getHostAddress();
|
||||
String port = env.getProperty("server.port");
|
||||
String path = StrUtil.trim(env.getProperty("server.servlet.context-path"));
|
||||
log.info("\n----------------------------------------------------------\n\t" +
|
||||
"Application is running! Access URLs:\n\t" +
|
||||
"Local: \t\thttp://localhost:" + port + path + "/\n\t" +
|
||||
"External: \thttp://" + ip + ":" + port + path + "/\n\t" +
|
||||
"----------------------------------------------------------");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.dpkj.common.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* @Date: 2024/4/28 14:55
|
||||
* @Description:
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "dpkj.file")
|
||||
public class FileConfig {
|
||||
|
||||
/**
|
||||
* 文件地址
|
||||
*/
|
||||
private String path;
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.dpkj.common.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* @Date: 2024/4/28 14:55
|
||||
* @Description:
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "dpkj.printer")
|
||||
public class PrinterConfig {
|
||||
|
||||
/**
|
||||
* 打印机连接方式 USB: usb连接 | BTMS:串口连接
|
||||
*/
|
||||
private String connectionType;
|
||||
|
||||
/**
|
||||
* 打印端口 串口连接下使用
|
||||
*/
|
||||
private String portName;
|
||||
|
||||
/**
|
||||
* 波特率 串口连接下使用
|
||||
*/
|
||||
private Integer baudRate;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.dpkj.common.constant;
|
||||
|
||||
public interface CommonConst {
|
||||
Integer SC_500 = 500;
|
||||
Integer SC_404 = 404;
|
||||
Integer SC_200 = 200;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.dpkj.common.vo;
|
||||
|
||||
import com.dpkj.common.constant.CommonConst;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 接口返回数据格式
|
||||
*/
|
||||
@Data
|
||||
public class Result<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 成功标志
|
||||
*/
|
||||
private boolean success = true;
|
||||
|
||||
/**
|
||||
* 返回处理消息
|
||||
*/
|
||||
private String message = "";
|
||||
|
||||
/**
|
||||
* 返回代码
|
||||
*/
|
||||
private Integer code = 0;
|
||||
|
||||
/**
|
||||
* 返回数据对象 data
|
||||
*/
|
||||
private T result;
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
private long timestamp = System.currentTimeMillis();
|
||||
|
||||
public Result() {
|
||||
}
|
||||
|
||||
public static <T> Result<T> ok() {
|
||||
return ok("", null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> ok(String msg) {
|
||||
return ok(msg, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> ok(T data) {
|
||||
return error("", data);
|
||||
}
|
||||
|
||||
public static <T> Result<T> ok(String msg, T data) {
|
||||
Result<T> r = new Result<T>();
|
||||
r.setSuccess(true);
|
||||
r.setCode(CommonConst.SC_200);
|
||||
r.setMessage(msg);
|
||||
r.setResult(data);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(String msg, T data) {
|
||||
return error(CommonConst.SC_500, msg, data);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(String msg) {
|
||||
return error(CommonConst.SC_500, msg, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(int code, String msg, T data) {
|
||||
Result<T> r = new Result<T>();
|
||||
r.setCode(code);
|
||||
r.setMessage(msg);
|
||||
r.setSuccess(false);
|
||||
r.setResult(data);
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package com.dpkj.modules.express.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.dpkj.common.config.FileConfig;
|
||||
import com.dpkj.common.vo.Result;
|
||||
import com.dpkj.modules.express.service.PrinterService;
|
||||
import com.dpkj.modules.express.utils.FileUtils;
|
||||
import com.dpkj.modules.express.utils.PrinterUtil;
|
||||
import com.dpkj.modules.express.vo.Print;
|
||||
import com.dpkj.modules.express.vo.PrinterStatus;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* @Date: 2024/4/28 13:58
|
||||
* @Description: 打印机
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("printer")
|
||||
public class PrinterController {
|
||||
@Autowired
|
||||
private PrinterService printerService;
|
||||
@Autowired
|
||||
private FileConfig fileConfig;
|
||||
|
||||
/**
|
||||
* 获取打印机状态
|
||||
*
|
||||
* @return com.dpkj.common.vo.Result
|
||||
* @author 萧道子 2024/4/28
|
||||
*/
|
||||
@GetMapping("status")
|
||||
public Result getStatus() {
|
||||
try {
|
||||
PrinterStatus val = printerService.getPrinterStatus();
|
||||
return Result.ok("获取成功", val);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.info("[printer][PrinterController.getStatus]获取打印机状态失败 {}", e.getMessage());
|
||||
return Result.error("打印机状态获取失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印文件
|
||||
*
|
||||
* @return com.dpkj.common.vo.Result
|
||||
* @author 萧道子 2024/4/29
|
||||
*/
|
||||
@PostMapping("print")
|
||||
public Result print(@RequestBody Print val) {
|
||||
try {
|
||||
String base64 = val.getBase64();
|
||||
if (StrUtil.isEmpty(base64)) throw new Exception("参数缺失!");
|
||||
|
||||
List<File> files = FileUtils.saveBase64ToBmp(base64);
|
||||
if (files.isEmpty()) throw new Exception("打印失败,面单未生成!");
|
||||
|
||||
File file = files.get(0);
|
||||
String filePath = file.getAbsolutePath();
|
||||
Integer num = printerService.printBmpByPath(filePath);// 打印
|
||||
FileUtil.del(file); // 删除文件
|
||||
|
||||
if (num != 0) throw new Exception("打印失败,请检查打印机是否连接正常!");
|
||||
|
||||
return Result.ok("打印成功");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.info("[printer][PrinterController.print] 打印面单 {}", e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.dpkj.modules.express.service;
|
||||
|
||||
import com.dpkj.modules.express.utils.PrinterUtil;
|
||||
import com.dpkj.modules.express.vo.PrinterStatus;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface PrinterService {
|
||||
|
||||
/**
|
||||
* 获取打印机状态
|
||||
*
|
||||
* @return com.dpkj.modules.express.vo.PrinterStatus
|
||||
* @author 萧道子 2024/4/28
|
||||
*/
|
||||
PrinterStatus getPrinterStatus();
|
||||
|
||||
/**
|
||||
* 通过路径打印BMP文件
|
||||
*
|
||||
* @param path :
|
||||
* @return java.lang.Integer
|
||||
* @author 萧道子 2024/4/29
|
||||
*/
|
||||
Integer printBmpByPath(String path) throws Exception;
|
||||
}
|
|
@ -0,0 +1,147 @@
|
|||
package com.dpkj.modules.express.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.dpkj.common.config.PrinterConfig;
|
||||
import com.dpkj.modules.express.service.PrinterService;
|
||||
import com.dpkj.modules.express.utils.PrinterUtil;
|
||||
import com.dpkj.modules.express.vo.PrinterStatus;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* @Date: 2024/4/28 14:34
|
||||
* @Description:
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PrinterServiceImpl implements PrinterService {
|
||||
@Resource
|
||||
private PrinterConfig printerConfig;
|
||||
|
||||
private PrinterUtil.MsPrintSdk printSDK = PrinterUtil.getPrintSDK();
|
||||
|
||||
public PrinterServiceImpl() throws PrinterUtil.PrinterRegistrationException {
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
log.info("[printer] 打印机初始化");
|
||||
initPrinter();
|
||||
}
|
||||
|
||||
private void initPrinter() {
|
||||
if (StrUtil.equals(printerConfig.getConnectionType(), "USB")) {
|
||||
// USB连接
|
||||
Integer n = printSDK.SetUsbportauto();
|
||||
log.info("[printer][SetUsbportauto] USB自动识别 {}", n);
|
||||
} else {
|
||||
// 串口连接
|
||||
String portName = printerConfig.getPortName();
|
||||
Integer baudRate = printerConfig.getBaudRate();
|
||||
Integer n = printSDK.SetPrintPort(portName, baudRate);
|
||||
log.info("[printer][SetPrintPort] 串口配置 {}", n);
|
||||
}
|
||||
Integer n = printSDK.SetInit();
|
||||
log.info("[printer][SetInit] 初始化打印机设置 {}", n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrinterStatus getPrinterStatus() {
|
||||
Integer num = printSDK.GetStatusspecial();
|
||||
PrinterStatus statusVo = getStatusVo(num);
|
||||
return statusVo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer printBmpByPath(String path) {
|
||||
initPrinter();
|
||||
|
||||
String formattedFilePath = path.replace("\\", "\\\\");
|
||||
int n = printSDK.PrintDiskimgfile(formattedFilePath);
|
||||
log.info("[printer][PrintDiskimgfile] 打印BMP文件 {}", n);
|
||||
|
||||
printMarkpositioncut();
|
||||
// printMarkcutpaper();
|
||||
printCutpaper();
|
||||
return n;
|
||||
}
|
||||
|
||||
private PrinterStatus getStatusVo(Integer num) {
|
||||
PrinterStatus val = new PrinterStatus().setValue(num);
|
||||
switch (num) {
|
||||
case 0:
|
||||
val.setText("打印机正常");
|
||||
break;
|
||||
case 1:
|
||||
val.setText("打印机未连接或未上电");
|
||||
break;
|
||||
case 2:
|
||||
val.setText("打印机和调用库不匹配");
|
||||
break;
|
||||
case 3:
|
||||
val.setText("当前使用打印机无特殊功能");
|
||||
break;
|
||||
case 4:
|
||||
val.setText("容纸器没有可靠上纸");
|
||||
break;
|
||||
case 5:
|
||||
val.setText("纸张堵在出票口,持续堆叠");
|
||||
break;
|
||||
case 6:
|
||||
val.setText("卡纸");
|
||||
break;
|
||||
case 7:
|
||||
val.setText("拽纸");
|
||||
break;
|
||||
case 8:
|
||||
val.setText("出纸传感器有纸");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测黑标进纸到切纸位置
|
||||
*/
|
||||
private Integer printMarkpositioncut() {
|
||||
int n = printSDK.PrintMarkpositioncut();
|
||||
log.info("[printer][PrintMarkpositioncut] 检测黑标进纸到切纸位置 {}", n);
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印切纸
|
||||
*/
|
||||
private Integer printCutpaper() {
|
||||
int n = printSDK.PrintCutpaper(0);
|
||||
log.info("[printer][PrintCutpaper] 打印切纸 {}", n);
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* 黑标切纸
|
||||
*/
|
||||
private Integer printMarkcutpaper() {
|
||||
int n = printSDK.PrintMarkcutpaper(0);
|
||||
log.info("[printer][printMarkcutpaper] 打印黑标切纸 {}", n);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测黑标进纸到打印位置
|
||||
*/
|
||||
private Integer printMarkpositionPrint() {
|
||||
int n = printSDK.PrintMarkpositionPrint();
|
||||
log.info("[printer][printMarkpositionPrint] 检测黑标进纸到打印位置 {}", n);
|
||||
return n;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
package com.dpkj.modules.express.utils;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.dpkj.common.config.FileConfig;
|
||||
import org.apache.pdfbox.Loader;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* @Date: 2024/4/29 14:47
|
||||
* @Description:
|
||||
*/
|
||||
@Component
|
||||
public class FileUtils {
|
||||
|
||||
private static FileConfig fileConfig;
|
||||
|
||||
@Autowired
|
||||
public void setFileConfig(FileConfig fileConfig) {
|
||||
this.fileConfig = fileConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* base64Pdf 转字节流
|
||||
*
|
||||
* @param base64Pdf :
|
||||
* @return java.util.List<java.awt.image.BufferedImage>
|
||||
* @author 萧道子 2024/4/29
|
||||
*/
|
||||
public static List<BufferedImage> convertBase64PdfToBufferedImage(String base64Pdf) throws IOException {
|
||||
List<BufferedImage> images = new ArrayList<>();
|
||||
base64Pdf = base64Pdf.replaceAll("[^A-Za-z0-9+/=]", "");
|
||||
byte[] base64Bytes = Base64.getDecoder().decode(base64Pdf);
|
||||
PDDocument document = Loader.loadPDF(base64Bytes);
|
||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||
for (int i = 0; i < document.getNumberOfPages(); i++) {
|
||||
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(i, 188);
|
||||
int h = bufferedImage.getHeight();
|
||||
int w = bufferedImage.getWidth();
|
||||
int x = 15;
|
||||
int y = 28;
|
||||
// int y = new BigDecimal(0.03).multiply(new BigDecimal(h)).setScale(0, BigDecimal.ROUND_UP).intValue();
|
||||
BufferedImage croppedImageTopOne = bufferedImage.getSubimage(x, y, w - x, h - y);
|
||||
images.add(croppedImageTopOne);
|
||||
}
|
||||
document.close();
|
||||
return images;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将BufferedImage 保存为 BMP文件
|
||||
*
|
||||
* @param images :
|
||||
* @return java.util.List<java.io.File>
|
||||
* @author 萧道子 2024/4/29
|
||||
*/
|
||||
public static List<File> saveBufferedImageToBmp(List<BufferedImage> images) throws IOException {
|
||||
List<File> fileList = new ArrayList<>();
|
||||
String path = fileConfig.getPath();
|
||||
// 创建文件夹
|
||||
FileUtil.mkdir(fileConfig.getPath());
|
||||
for (BufferedImage bufferedImage : images) {
|
||||
String filePath = path + "/" + IdUtil.simpleUUID() + ".bmp";
|
||||
filePath = FileUtil.normalize(filePath);
|
||||
File bmpFile = new File(filePath);
|
||||
// 使用ImageIO将BufferedImage保存为BMP文件
|
||||
ImageIO.write(bufferedImage, "BMP", bmpFile);
|
||||
fileList.add(bmpFile);
|
||||
}
|
||||
return fileList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将base64保存为Bmp文件
|
||||
*
|
||||
* @param base64 :
|
||||
* @return java.util.List<java.io.File>
|
||||
* @author 萧道子 2024/4/29
|
||||
*/
|
||||
public static List<File> saveBase64ToBmp(String base64) throws IOException {
|
||||
List<BufferedImage> bufferedImages = FileUtils.convertBase64PdfToBufferedImage(base64);
|
||||
List<File> files = FileUtils.saveBufferedImageToBmp(bufferedImages);
|
||||
return files;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,224 @@
|
|||
package com.dpkj.modules.express.utils;
|
||||
|
||||
import com.sun.jna.Library;
|
||||
import com.sun.jna.Native;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
public class PrinterUtil {
|
||||
|
||||
/**
|
||||
* 获取 MsPrintSdk 实例,同时注册 MsPrintSdk 控件。
|
||||
*
|
||||
* @return MsPrintSdk 实例
|
||||
* @throws PrinterRegistrationException 如果注册控件失败,抛出此异常
|
||||
*/
|
||||
public static MsPrintSdk getPrintSDK() throws PrinterRegistrationException {
|
||||
/* String ocxPath = "src/lib/Msprintsdk.ocx";
|
||||
ProcessBuilder processBuilder = new ProcessBuilder("regsvr32", "/s", ocxPath);
|
||||
try {
|
||||
Process process = processBuilder.start();
|
||||
int exitCode = process.waitFor();
|
||||
// if (exitCode != 0) {
|
||||
// throw new PrinterRegistrationException("Failed to register MsPrintSdk OCX. Exit code: " + exitCode);
|
||||
// }
|
||||
} catch (InterruptedException | IOException e) {
|
||||
throw new PrinterRegistrationException("Error occurred while registering MsPrintSdk OCX.", e);
|
||||
}*/
|
||||
|
||||
try {
|
||||
return Native.load("Msprintsdk.x64", MsPrintSdk.class);
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
log.info("[printer][PrinterUtil.getPrintSDK] SDK注册失败 {}", e.getMessage());
|
||||
throw new PrinterRegistrationException("Failed to load MsPrintSdk library: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void convertAndPassStringAsJCharArray(String filePath) {
|
||||
// 将字符串转换为char[]
|
||||
char[] chars = filePath.toCharArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义接口映射本地库中的函数。
|
||||
*/
|
||||
public interface MsPrintSdk extends Library {
|
||||
|
||||
/**
|
||||
* 设置打印端口和波特率。
|
||||
*
|
||||
* @param strPort 打印端口名
|
||||
* @param baudRate 波特率
|
||||
* @return 返回操作结果代码
|
||||
*/
|
||||
Integer SetPrintPort(String strPort, int baudRate);
|
||||
|
||||
/**
|
||||
* 设置打印连接方式、名称和值。
|
||||
*
|
||||
* @param iConnWay 连接方式
|
||||
* @param strName 名称
|
||||
* @param strValue 值
|
||||
* @return 返回操作结果代码
|
||||
*/
|
||||
Integer SetPrintConn(int iConnWay, String strName, String strValue);
|
||||
|
||||
/**
|
||||
* 设置 USB 端口自动识别。
|
||||
*
|
||||
* @return 返回操作结果代码
|
||||
*/
|
||||
Integer SetUsbportauto();
|
||||
|
||||
/**
|
||||
* 获取打印机状态。
|
||||
* 0 打印机正常
|
||||
* 1 打印机未连接或未上电
|
||||
* 2 打印机和调用库不匹配
|
||||
* 3 打印头打开
|
||||
* 4 切刀未复位
|
||||
* 5 打印头温度异常
|
||||
* 6 黑标错误,可能的情况为 黑标传感器坏 | 使用错误白纸 | 黑标不标准,浓度偏低
|
||||
* 7 纸尽
|
||||
* 8 纸将尽
|
||||
*
|
||||
* @return 返回打印机状态代码 0:成功 1:失败
|
||||
*/
|
||||
Integer GetStatus();
|
||||
|
||||
/**
|
||||
* 获取打印机特殊功能状态
|
||||
* 0 打印机正常
|
||||
* 1 打印机未连接或未上电
|
||||
* 2 打印机和调用库不匹配
|
||||
* 3 当前使用打印机无特殊功能
|
||||
* 4 容纸器没有可靠上纸
|
||||
* 5 纸张堵在出票口,持续堆叠
|
||||
* 6 卡纸,出纸口未堵的情况下,胶辊无法驱动纸持续前进了。 比如纸卷在胶辊上了、切刀堵住了纸的前进、纸张被拉住无法前进等
|
||||
* 7 拽纸,打印机感受到凭条被外力拖拽
|
||||
* 8 出纸传感器有纸
|
||||
*
|
||||
* @return 返回打印机状态代码 0:成功 1:失败
|
||||
*/
|
||||
Integer GetStatusspecial();
|
||||
|
||||
/**
|
||||
* 初始化打印机设置。
|
||||
*
|
||||
* @return 返回操作结果代码
|
||||
*/
|
||||
Integer SetInit();
|
||||
|
||||
/**
|
||||
* 打印二维码。
|
||||
*
|
||||
* @param s 二维码数据
|
||||
* @param i 参数 i
|
||||
* @param i1 参数 i1
|
||||
* @param i2 参数 i2
|
||||
*/
|
||||
void PrintQrcode(String s, int i, int i1, int i2);
|
||||
|
||||
/**
|
||||
* 设置左边距。
|
||||
*
|
||||
* @param i 左边距值
|
||||
*/
|
||||
void SetLeftmargin(int i);
|
||||
|
||||
/**
|
||||
* 打印字符串。
|
||||
*
|
||||
* @param s 字符串内容
|
||||
* @param i 参数 i
|
||||
* @return 返回操作结果代码
|
||||
*/
|
||||
Integer PrintString(String s, int i);
|
||||
|
||||
/**
|
||||
* 打印剩余的二维码。
|
||||
*
|
||||
* @return 返回操作结果代码
|
||||
*/
|
||||
Integer PrintRemainQR();
|
||||
|
||||
/**
|
||||
* 设置 设置汉字模式 模式。
|
||||
*
|
||||
* @param i 模式参数
|
||||
* @return 返回操作结果代码
|
||||
*/
|
||||
Integer SetReadZKmode(int i);
|
||||
|
||||
/**
|
||||
* 切纸。
|
||||
*
|
||||
* @param i 切纸参数
|
||||
* 0 全切
|
||||
* 1 半切
|
||||
* @return 返回操作结果代码
|
||||
*/
|
||||
Integer PrintCutpaper(int i);
|
||||
|
||||
/**
|
||||
* 设置 HT 座位信息。
|
||||
*
|
||||
* @param s 座位信息
|
||||
* @param i 参数 i
|
||||
*/
|
||||
void SetHTseat(String s, int i);
|
||||
|
||||
/**
|
||||
* 打印下一个 HT 数据。
|
||||
*/
|
||||
void PrintNextHT();
|
||||
|
||||
/**
|
||||
* 打印BMP
|
||||
*
|
||||
* @param strPath BMP文件路径
|
||||
*/
|
||||
Integer PrintDiskbmpfile(String strPath);
|
||||
|
||||
/**
|
||||
* 打印BMP
|
||||
*
|
||||
* @param strPath BMP文件路径
|
||||
*/
|
||||
Integer PrintDiskimgfile(String strPath);
|
||||
|
||||
|
||||
/**
|
||||
* 检测黑标进纸到打印位置
|
||||
*/
|
||||
Integer PrintMarkpositionPrint();
|
||||
|
||||
/**
|
||||
* 检测黑标进纸到切纸位置
|
||||
*/
|
||||
Integer PrintMarkpositioncut();
|
||||
|
||||
/**
|
||||
* 打印黑标切纸
|
||||
* <p>
|
||||
* 0 检测黑标全切
|
||||
* 1 不检测黑标半切
|
||||
*/
|
||||
Integer PrintMarkcutpaper(int iMode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义自定义异常类,用于表示注册控件时发生的错误
|
||||
*/
|
||||
public static class PrinterRegistrationException extends Exception {
|
||||
public PrinterRegistrationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public PrinterRegistrationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.dpkj.modules.express.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* @Date: 2024/4/29 17:03
|
||||
* @Description:
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class Print implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String base64;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.dpkj.modules.express.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* @Date: 2024/4/28 14:44
|
||||
* @Description:
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class PrinterStatus implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private int value;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String text;
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
dpkj:
|
||||
file:
|
||||
# 文件保存地址
|
||||
path: G:\Temp\img
|
||||
# 打印机配置
|
||||
printer:
|
||||
# 打印机连接方式 USB: usb连接 | BTMS:串口连接
|
||||
connection-type: USB
|
||||
# 打印端口 串口连接下使用
|
||||
port-name:
|
||||
# 波特率 串口连接下使用
|
||||
baud-rate:
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
dpkj:
|
||||
file:
|
||||
# 文件保存地址
|
||||
path: D:\Project\Express\upload
|
||||
# 打印机配置
|
||||
printer:
|
||||
# 打印机连接方式 USB: usb连接 | BTMS:串口连接
|
||||
connection-type: USB
|
||||
# 打印端口 串口连接下使用
|
||||
port-name:
|
||||
# 波特率 串口连接下使用
|
||||
baud-rate:
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
spring:
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 10MB
|
||||
max-request-size: 10MB
|
||||
resource:
|
||||
static-locations: classpath:/static/,classpath:/public/
|
||||
application:
|
||||
name: ems-express-bridge
|
||||
profiles:
|
||||
#active: '@profile.name@'
|
||||
active: pro
|
||||
|
||||
server:
|
||||
port: 5946
|
||||
servlet:
|
||||
context-path: /api
|
||||
tomcat:
|
||||
max-swallow-size: -1
|
||||
error:
|
||||
include-exception: true
|
||||
include-stacktrace: ALWAYS
|
||||
include-message: ALWAYS
|
||||
compression:
|
||||
enabled: true
|
||||
min-response-size: 1024
|
||||
mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/*
|
|
@ -0,0 +1,77 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration debug="false">
|
||||
<!--定义日志文件的存储地址 -->
|
||||
<property name="LOG_HOME" value="./logs/java"/>
|
||||
|
||||
<!--<property name="COLOR_PATTERN" value="%black(%contextName-) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta( %replace(%caller{1}){'\t|Caller.{1}0|\r\n', ''})- %gray(%msg%xEx%n)" />-->
|
||||
<!-- 控制台输出 -->
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern>-->
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{50}:%L) - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 按照每天生成日志文件 -->
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<!--日志文件输出的文件名 -->
|
||||
<FileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}.%i.log</FileNamePattern>
|
||||
<!--日志文件保留天数 -->
|
||||
<MaxHistory>30</MaxHistory>
|
||||
<maxFileSize>10MB</maxFileSize>
|
||||
</rollingPolicy>
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 生成 error html格式日志开始 -->
|
||||
<appender name="HTML" class="ch.qos.logback.core.FileAppender">
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<!--设置日志级别,过滤掉info日志,只输入error日志-->
|
||||
<level>ERROR</level>
|
||||
</filter>
|
||||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
|
||||
<layout class="ch.qos.logback.classic.html.HTMLLayout">
|
||||
<pattern>%p%d%msg%M%F{32}%L</pattern>
|
||||
</layout>
|
||||
</encoder>
|
||||
<file>${LOG_HOME}/error-log.html</file>
|
||||
</appender>
|
||||
<!-- 生成 error html格式日志结束 -->
|
||||
|
||||
<!-- 每天生成一个html格式的日志开始 -->
|
||||
<appender name="FILE_HTML" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<!--日志文件输出的文件名 -->
|
||||
<FileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}.%i.html</FileNamePattern>
|
||||
<!--日志文件保留天数 -->
|
||||
<MaxHistory>30</MaxHistory>
|
||||
<MaxFileSize>10MB</MaxFileSize>
|
||||
</rollingPolicy>
|
||||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
|
||||
<layout class="ch.qos.logback.classic.html.HTMLLayout">
|
||||
<pattern>%p%d%msg%M%F{32}%L</pattern>
|
||||
</layout>
|
||||
</encoder>
|
||||
</appender>
|
||||
<!-- 每天生成一个html格式的日志结束 -->
|
||||
|
||||
<!--myibatis log configure -->
|
||||
<logger name="com.apache.ibatis" level="TRACE"/>
|
||||
<logger name="java.sql.Connection" level="DEBUG"/>
|
||||
<logger name="java.sql.Statement" level="DEBUG"/>
|
||||
<logger name="java.sql.PreparedStatement" level="DEBUG"/>
|
||||
|
||||
<!-- 日志输出级别 -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
<appender-ref ref="FILE"/>
|
||||
<appender-ref ref="HTML"/>
|
||||
<appender-ref ref="FILE_HTML"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,13 @@
|
|||
package com.dpkj.ems;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class EmsExpressBridgeApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package com.dpkj.ems.utils;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import com.dpkj.Application;
|
||||
import com.dpkj.common.config.FileConfig;
|
||||
import com.dpkj.modules.express.service.PrinterService;
|
||||
import com.dpkj.modules.express.utils.FileUtils;
|
||||
import com.dpkj.modules.express.utils.PrinterUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
|
||||
class PrinterUtilTest {
|
||||
|
||||
@Autowired
|
||||
private PrinterService printerService;
|
||||
@Autowired
|
||||
private FileConfig fileConfig;
|
||||
|
||||
@Test
|
||||
void getPrintSDK() throws PrinterUtil.PrinterRegistrationException {
|
||||
PrinterUtil.MsPrintSdk sdk = PrinterUtil.getPrintSDK();
|
||||
int i = sdk.SetUsbportauto();
|
||||
Console.log(i);
|
||||
|
||||
int i1 = sdk.SetInit();
|
||||
Console.log(i1);
|
||||
|
||||
int i2 = sdk.GetStatusspecial();
|
||||
Console.log(i2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void t2() throws IOException {
|
||||
String base64Path = "G:\\Temp\\text\\base64.txt";
|
||||
String base64 = FileUtil.readString(FileUtil.file(base64Path), StandardCharsets.UTF_8);
|
||||
|
||||
String bpmPath = "G:\\Temp\\img";
|
||||
List<BufferedImage> bufferedImages = FileUtils.convertBase64PdfToBufferedImage(base64);
|
||||
FileUtils.saveBufferedImageToBmp(bufferedImages);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void t3() throws Exception {
|
||||
String base64Path = "G:\\Temp\\text\\base64.txt";
|
||||
String base64 = FileUtil.readString(FileUtil.file(base64Path), StandardCharsets.UTF_8);
|
||||
|
||||
// String bpmPath = "G:\\Temp\\img";
|
||||
String bpmPath = fileConfig.getPath();
|
||||
List<BufferedImage> bufferedImages = FileUtils.convertBase64PdfToBufferedImage(base64);
|
||||
List<File> files = FileUtils.saveBufferedImageToBmp(bufferedImages);
|
||||
|
||||
/*for (File file : files) {
|
||||
String filePath = file.getAbsolutePath();
|
||||
printerService.printBmpByPath(filePath);
|
||||
}*/
|
||||
}
|
||||
|
||||
@Test
|
||||
void t4() throws IOException {
|
||||
String base64Path = "G:\\Temp\\text\\base64.txt";
|
||||
String base64 = FileUtil.readString(FileUtil.file(base64Path), StandardCharsets.UTF_8);
|
||||
|
||||
List<BufferedImage> bufferedImages = FileUtils.convertBase64PdfToBufferedImage(base64);
|
||||
System.out.println(bufferedImages);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.dpkj.ems.utils;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.dpkj.modules.express.utils.FileUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
class PrinterUtilTest2 {
|
||||
|
||||
@Test
|
||||
void t4() throws IOException {
|
||||
String base64Path = "G:\\Temp\\text\\base64.txt";
|
||||
String base64 = FileUtil.readString(FileUtil.file(base64Path), StandardCharsets.UTF_8);
|
||||
|
||||
List<BufferedImage> bufferedImages = FileUtils.convertBase64PdfToBufferedImage(base64);
|
||||
System.out.println(bufferedImages);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue