Drawing2D工具类写入图片

This commit is contained in:
dzc committed 2025-03-25 20:34:31 +08:00
1 parent f405da26b4
commit 3bab33f7d8
3 files changed
+67 -15

No files matched your search

@@ -1,5 +1,7 @@
package com.bsd.pjcloud.hard.api.utils;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
@@ -8,6 +10,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@Slf4j
public class Drawing2D {
/**
@@ -26,8 +29,13 @@ public class Drawing2D {
private final int canvasHeight = 200;
private int currentWidth = canvasWidth;
private int currentHeight = canvasHeight;
private final int canvasImageWidth = 300;
private final int canvasImageHeight = 300;
private final int horizontalPadding = 20;
private final String fontFamily = "黑体";
@@ -92,7 +100,8 @@ public class Drawing2D {
textPadding();
computedLineHeight();
Point point = centerPosition(str);
graphics.drawString(str, point.x, getCurrentY());
int y = getCurrentY();
graphics.drawString(str, point.x, y);
textPadding();
return this;
}
@@ -144,6 +153,27 @@ public class Drawing2D {
return this;
}
public Drawing2D drawImage(InputStream inputStream) {
try {
BufferedImage image = ImageIO.read(inputStream);
if (image == null) return this;
// 获取图片的原始宽度和高度
int originalWidth = image.getWidth();
int originalHeight = image.getHeight();
// 计算缩放比例,确保宽度不超过 canvasImageWidth
double scaleFactor = Math.min(1.0, (double) canvasImageWidth / originalWidth);
int scaledWidth = (int) (originalWidth * scaleFactor);
int scaledHeight = (int) (originalHeight * scaleFactor);
Point point = centerPosition(new Point(scaledWidth, scaledHeight));
currentY += scaledHeight;
computedHeight();
graphics.drawImage(image, point.x, point.y, scaledWidth, scaledHeight, null);
} catch (Exception e) {
log.error("绘制图片错误error:|{}|task:|{}|", e.getMessage(), e.getStackTrace());
}
return this;
}
public InputStream getInputStream() throws IOException {
drawingWhiteSide();
graphics.dispose();
@@ -178,6 +208,12 @@ public class Drawing2D {
return new Point(x, y);
}
private Point centerPosition(Point point) {
// 中心X坐标
int x = (canvasWidth - point.x) / 2;
return new Point(x, currentY);
}
private void drawingWhiteSide() {
int borderSize = 15;
BufferedImage borderedCanvas = new BufferedImage(currentWidth + borderSize * 2, currentHeight + borderSize * 2, BufferedImage.TYPE_INT_RGB);
@@ -23,18 +23,13 @@ import com.bsd.pjcloud.hard.api.constant.HardPrintStatusEnum;
import com.bsd.pjcloud.hard.api.constant.HardTypeEnum;
import com.bsd.pjcloud.hard.api.constant.order.HardOrderTypeEnum;
import com.bsd.pjcloud.hard.api.dto.HardInvoiceDto;
import com.bsd.pjcloud.hard.api.dto.HardManagerDto;
import com.bsd.pjcloud.hard.api.dto.order.HardOrderDto;
import com.bsd.pjcloud.hard.api.entity.HardInvoiceEntity;
import com.bsd.pjcloud.hard.api.entity.HardManagerEntity;
import com.bsd.pjcloud.hard.api.entity.order.HardCarEntity;
import com.bsd.pjcloud.hard.api.entity.order.HardOrderEntity;
import com.bsd.pjcloud.hard.api.utils.Drawing2D;
import com.bsd.pjcloud.hard.api.utils.HardUtil;
import com.bsd.pjcloud.hard.api.vo.HardInvoiceVo;
import com.bsd.pjcloud.hard.api.vo.HardManagerVo;
import com.bsd.pjcloud.hard.api.vo.order.HardAfterSaleVo;
import com.bsd.pjcloud.hard.api.vo.order.HardOrderVo;
import com.bsd.pjcloud.hard.mapper.HardInvoiceMapper;
import com.bsd.pjcloud.hard.mapper.HardManagerMapper;
import com.bsd.pjcloud.hard.mapper.order.HardOrderMapper;
@@ -42,7 +37,6 @@ import com.bsd.pjcloud.hard.service.HardCarService;
import com.bsd.pjcloud.hard.service.HardInvoiceService;
import com.bsd.pjcloud.hard.service.HardManagerService;
import com.bsd.pjcloud.hard.service.HardOrderService;
import com.bsd.pjcloud.hard.service.impl.order.HardAfterSaleServiceImpl;
import com.bsd.pjcloud.mqtt.api.feign.RemoteMqttEventService;
import com.bsd.pjcloud.mqtt.dto.hard.HardPrintMsg;
import com.bsd.pjcloud.wallet.api.feign.RemoteMchService;
@@ -50,7 +44,6 @@ import com.bsd.pjcloud.wallet.api.utils.WalletDataUtil;
import com.bsd.pjcloud.wallet.api.vo.MchManageVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -18,6 +18,7 @@ import com.bsd.pjcloud.common.core.util.BeanUtils;
import com.bsd.pjcloud.common.core.util.CollectionUtils;
import com.bsd.pjcloud.common.core.util.QRCodeImageGenerator;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.hard.api.utils.Drawing2D;
import com.bsd.pjcloud.wallet.api.dto.MchManageDto;
import com.bsd.pjcloud.wallet.api.dto.WalletTypeDto;
import com.bsd.pjcloud.wallet.api.entity.MchManageEntity;
@@ -249,16 +250,18 @@ public class MchManageServiceImpl extends ServiceImpl<MchManageMapper, MchManage
MchCollectionCodeVo mchCollectionCode = new MchCollectionCodeVo();
mchCollectionCode.setId(String.valueOf(mchDetail.getId()));
mchCollectionCode.setName(mchDetail.getName());
String coeContent = Base64.encode(JSONUtil.toJsonStr(mchCollectionCode));
byte[] bytes = QRCodeImageGenerator.generateQrCodeImage(coeContent, 1024);
MultipartFile multipartFile = new MockMultipartFile("file", mchCollectionCode.getFileName() + ".png", "image/png", new ByteArrayInputStream(bytes));
R upload = remoteFileService.innerUpload(multipartFile, null, null, null);
Map data = (Map) upload.getData();
String url = (String) data.get("url");
String string = generateMchOrder(mchDetail);
// String coeContent = Base64.encode(JSONUtil.toJsonStr(mchCollectionCode));
// byte[] bytes = QRCodeImageGenerator.generateQrCodeImage(coeContent, 1024);
// MultipartFile multipartFile = new MockMultipartFile("file", mchCollectionCode.getFileName() + ".png", "image/png", new ByteArrayInputStream(bytes));
// R upload = remoteFileService.innerUpload(multipartFile, null, null, null);
// Map data = (Map) upload.getData();
// String url = (String) data.get("url");
MchManageEntity updateEntity = new MchManageEntity();
updateEntity.setId(mchDetail.getId());
updateEntity.setCollectionCode(url);
updateEntity.setCollectionCode(string);
updateById(updateEntity);
} catch (Exception e) {
e.printStackTrace();
@@ -266,6 +269,26 @@ public class MchManageServiceImpl extends ServiceImpl<MchManageMapper, MchManage
}
}
private String generateMchOrder(MchManageEntity mchDetail) {
try {
MchCollectionCodeVo mchCollectionCode = new MchCollectionCodeVo();
mchCollectionCode.setId(String.valueOf(mchDetail.getId()));
mchCollectionCode.setName(mchDetail.getName());
String coeContent = Base64.encode(JSONUtil.toJsonStr(mchCollectionCode));
byte[] bytes = QRCodeImageGenerator.generateQrCodeImage(coeContent, 300);
if (ObjectUtil.isEmpty(bytes)) return null;
Drawing2D drawing = new Drawing2D();
byte[] mchCodeByte = drawing.drawImage(new ByteArrayInputStream(bytes)).setTitleTextStyle().drawingCenterText(mchDetail.getName()).getBytes();
MultipartFile multipartFile = new MockMultipartFile("file", mchCollectionCode.getFileName() + ".png", "image/png", new ByteArrayInputStream(mchCodeByte));
R upload = remoteFileService.innerUpload(multipartFile, null, null, null);
Map data = (Map) upload.getData();
return (String) data.get("url");
} catch (Exception e) {
log.error("|{}|新增商户,生成收款码错误dto:|{}|error:|{}|", TAG, JSON.toJSONString(mchDetail), e.getMessage());
return null;
}
}
private boolean existName(String name, String types, Long id) {
Long count = baseMapper.selectCount(new LambdaQueryWrapper<MchManageEntity>()
.eq(ObjectUtil.isNotEmpty(name), MchManageEntity::getName, name)