feat(printer): 优化拼图打印偏移处理逻辑

- 添加白边框并向上偏移内容以避免打印机偏移
- 替换原有的单纯向上偏移方法
- 弃用 shiftImageUp 方法,新增 addBorderAndShiftUp 方法
- 更新临时文件命名及清理逻辑
- 修改日志记录内容以反映新的处理方式
This commit is contained in:
2025-11-22 00:07:18 +08:00
parent 18bf51487d
commit 9278d4479f
3 changed files with 143 additions and 17 deletions

View File

@@ -471,16 +471,18 @@ public class FaceServiceImpl implements FaceService {
sfpContent.setTemplateCoverUrl(template.getCoverImage());
sfpContent.setGoodsType(3);
sfpContent.setSort(0);
IsBuyRespVO isBuyRespVO = orderBiz.isBuy(face.getScenicId(), face.getMemberId(), faceId, 5, records.getFirst().getTemplateId());
if (isBuyRespVO.isBuy()) {
sfpContent.setIsBuy(1);
} else {
sfpContent.setIsBuy(0);
if (optionalRecord.isPresent()) {
IsBuyRespVO isBuyRespVO = orderBiz.isBuy(face.getScenicId(), face.getMemberId(), faceId, 5, optionalRecord.get().getTemplateId());
if (isBuyRespVO.isBuy()) {
sfpContent.setIsBuy(1);
} else {
sfpContent.setIsBuy(0);
}
}
PriceCalculationRequest calculationRequest = new PriceCalculationRequest();
ProductItem productItem = new ProductItem();
productItem.setProductType(ProductType.PHOTO_LOG);
productItem.setProductId(records.getFirst().getTemplateId().toString());
productItem.setProductId(template.getId().toString());
productItem.setPurchaseCount(1);
productItem.setScenicId(face.getScenicId().toString());
calculationRequest.setProducts(Collections.singletonList(productItem));

View File

@@ -864,19 +864,19 @@ public class PrinterServiceImpl implements PrinterService {
log.error("获取景区配置失败,使用原始照片进行打印。景区ID: {}, 照片ID: {}", item.getScenicId(), item.getId(), e);
}
} else if (item.getSourceId() != null && item.getSourceId() == 0) {
// 拼图:向上移动40像素以避免打印机偏移
// 拼图:添加白边框并向上偏移以避免打印机偏移
try {
// 生成唯一的处理标识符,避免多线程环境下的文件冲突
String processId = item.getId() + "_" + UUID.randomUUID().toString();
File originalFile = new File("puzzle_" + processId + ".png");
File shiftedFile = new File("puzzle_" + processId + "_shifted.png");
File processedFile = new File("puzzle_" + processId + "_processed.png");
try {
// 下载原图
HttpUtil.downloadFile(item.getCropUrl(), originalFile);
// 向上偏移40像素
ImageUtils.shiftImageUp(originalFile, shiftedFile, 40);
// 添加白边框(左右20px,上下30px)并向上偏移15px
ImageUtils.addBorderAndShiftUp(originalFile, processedFile, 20, 30, 15);
// 上传处理后的图片
IStorageAdapter adapter;
@@ -892,21 +892,21 @@ public class PrinterServiceImpl implements PrinterService {
adapter = StorageFactory.use("assets-ext");
}
String shiftedUrl = adapter.uploadFile(null, shiftedFile, StorageConstant.PHOTO_WATERMARKED_PATH, shiftedFile.getName());
adapter.setAcl(StorageAcl.PUBLIC_READ, StorageConstant.PHOTO_WATERMARKED_PATH, shiftedFile.getName());
String processedUrl = adapter.uploadFile(null, processedFile, StorageConstant.PHOTO_WATERMARKED_PATH, processedFile.getName());
adapter.setAcl(StorageAcl.PUBLIC_READ, StorageConstant.PHOTO_WATERMARKED_PATH, processedFile.getName());
printUrl = shiftedUrl;
log.info("拼图照片向上偏移40像素成功,照片ID: {}, 新URL: {}", item.getId(), shiftedUrl);
printUrl = processedUrl;
log.info("拼图照片添加白边框并向上偏移成功,照片ID: {}, 新URL: {}", item.getId(), processedUrl);
} catch (Exception e) {
log.error("拼图照片向上偏移处理失败,使用原始照片进行打印。照片ID: {}", item.getId(), e);
log.error("拼图照片处理失败,使用原始照片进行打印。照片ID: {}", item.getId(), e);
} finally {
// 清理临时文件
if (originalFile != null && originalFile.exists()) {
originalFile.delete();
}
if (shiftedFile != null && shiftedFile.exists()) {
shiftedFile.delete();
if (processedFile != null && processedFile.exists()) {
processedFile.delete();
}
}
} catch (Exception e) {