yinyitong-zhongyuyuan-dll-hang/src/main/java/com/dpkj/modules/autoReplyPrint/base/BaseImagePrint.java

90 lines
3.3 KiB
Java
Raw Normal View History

2025-01-17 15:15:23 +08:00
package com.dpkj.modules.autoReplyPrint.base;
import com.dpkj.common.exception.RRException;
import com.dpkj.modules.autoReplyPrint.utils.AutoReplyPrint;
import com.dpkj.modules.autoReplyPrint.utils.ImageUtils;
import com.sun.jna.Pointer;
import org.springframework.web.multipart.MultipartFile;
/**
* 图片打印基础实现
*
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
* @version 1.0
* @since 2025-01-17 14:37:23
*/
public abstract class BaseImagePrint {
/**
* 必须要实现这个获取句柄
2025-01-20 14:58:42 +08:00
* @param devName 设备名称/串口名称
2025-01-17 15:15:23 +08:00
* @return 窗口句柄
*/
public abstract Pointer getHandle(String devName);
public void printFromPath(String devName, int dstw, int dsth, String pszFile, int binaryzation_method, int compression_method) {
Pointer handle = getHandle(devName);
try {
// 开始打印图片
boolean printTag = AutoReplyPrint.INSTANCE.CP_Pos_PrintRasterImageFromFile(handle, dstw, dsth, pszFile, binaryzation_method, compression_method);
if( !printTag ){
throw new RRException("打印图片失败");
}
// 切纸
AutoReplyPrint.INSTANCE.CP_BlackMark_SetBlackMarkPaperCutPosition(handle, 0);
boolean cutPaper = AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(handle);
if ( !cutPaper){
throw new RRException("图片裁剪失败,请自行裁剪");
}
}catch (Exception e){
e.printStackTrace();
throw new RRException(e);
}finally {
if ( handle != null) AutoReplyPrint.INSTANCE.CP_Port_Close(handle);
}
}
public void printFromPathData(String devName, int dstw, int dsth, String pszFile, int binaryzation_method, int compression_method) {
// 获取图片
byte[] data = ImageUtils.imageToBytes(pszFile);
this.printFromData(devName, dstw, dsth, data, binaryzation_method, compression_method);
}
public void printFromData(String devName, int dstw, int dsth, byte[] data, int binaryzation_method, int compression_method) {
Pointer handle = getHandle(devName);
try {
// 开始打印图片
boolean printTag = AutoReplyPrint.INSTANCE.CP_Pos_PrintRasterImageFromData(handle, dstw, dsth, data, data.length, binaryzation_method, compression_method);
if( !printTag ){
throw new RRException("打印图片失败");
}
// 切纸
AutoReplyPrint.INSTANCE.CP_BlackMark_SetBlackMarkPaperCutPosition(handle, 0);
boolean cutPaper = AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(handle);
if ( !cutPaper){
throw new RRException("图片裁剪失败,请自行裁剪");
}
}catch (Exception e){
e.printStackTrace();
throw new RRException(e);
}finally {
if ( handle != null) AutoReplyPrint.INSTANCE.CP_Port_Close(handle);
}
}
public void printFromMultipartFile(String devName, int dstw, int dsth, MultipartFile file, int binaryzation_method, int compression_method){
byte[] data = ImageUtils.getByteToMultipartFile(file);
this.printFromData(devName, dstw, dsth, data, binaryzation_method, compression_method);
}
}