3333
This commit is contained in:
parent
827f8ddc92
commit
a515bb3085
108
src/main/java/com/example/caseData/service/CertService.java
Normal file
108
src/main/java/com/example/caseData/service/CertService.java
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
package com.example.caseData.service;
|
||||||
|
|
||||||
|
import com.example.caseData.exception.BusinessException;
|
||||||
|
import com.example.caseData.extend.aliyun.Oss;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.geom.Ellipse2D;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CertService {
|
||||||
|
// 生成证书
|
||||||
|
public String createCert(String avatarPath,
|
||||||
|
String sealPath,
|
||||||
|
String certificateNo,
|
||||||
|
String name,
|
||||||
|
String content){
|
||||||
|
try {
|
||||||
|
// 加载背景模板图片
|
||||||
|
BufferedImage background = ImageIO.read(new File("src/main/resources/static/cert/template.png"));
|
||||||
|
|
||||||
|
Graphics2D g2d = (Graphics2D) background.getGraphics();
|
||||||
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
// 设置字体
|
||||||
|
Font font = new Font("Arial", Font.PLAIN, 24);
|
||||||
|
g2d.setFont(font);
|
||||||
|
|
||||||
|
// 1. 添加证书编号
|
||||||
|
g2d.drawString(certificateNo, 180, 294);
|
||||||
|
|
||||||
|
// 2. 处理并添加头像
|
||||||
|
BufferedImage avatar = ImageIO.read(new File(avatarPath));
|
||||||
|
int size = 100; // 目标大小
|
||||||
|
BufferedImage circleAvatar = createCircularImage(avatar, size);
|
||||||
|
g2d.drawImage(circleAvatar, 290, 380, null);
|
||||||
|
|
||||||
|
// 3. 添加姓名
|
||||||
|
g2d.drawString(name, 330, 524);
|
||||||
|
|
||||||
|
// 4. 添加证书内容
|
||||||
|
g2d.drawString(content, 120, 574);
|
||||||
|
|
||||||
|
// 5. 添加日期
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
|
||||||
|
String currentDate = sdf.format(new Date());
|
||||||
|
g2d.drawString(currentDate, 390, 874);
|
||||||
|
|
||||||
|
// 6. 添加印章
|
||||||
|
BufferedImage seal = ImageIO.read(new File(sealPath));
|
||||||
|
seal = resize(seal, 100, 100); // 调整印章大小
|
||||||
|
g2d.drawImage(seal, 300, 750, null);
|
||||||
|
|
||||||
|
// 释放资源
|
||||||
|
g2d.dispose();
|
||||||
|
|
||||||
|
// === 将图片转为 byte[] ===
|
||||||
|
// ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
// ImageIO.write(background, "png", os);
|
||||||
|
// byte[] imageBytes = os.toByteArray();
|
||||||
|
// String fileName = "dev/static/images/" + "测试" + ".png";
|
||||||
|
// boolean uploaded = Oss.putObject(fileName, imageBytes);
|
||||||
|
// if (uploaded) {
|
||||||
|
// // 返回可访问的 URL(假设你有自定义域名)
|
||||||
|
// // 格式:http://你的域名/文件名
|
||||||
|
// return fileName;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 保存最终的证书图片
|
||||||
|
ImageIO.write(background, "png", new File("cert.png"));
|
||||||
|
System.out.println("Certificate generated successfully!");
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new BusinessException(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BufferedImage createCircularImage(BufferedImage originalImg, int size) {
|
||||||
|
BufferedImage outputImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
|
||||||
|
Graphics2D g2 = outputImage.createGraphics();
|
||||||
|
g2.setComposite(AlphaComposite.Src);
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
g2.setColor(Color.WHITE);
|
||||||
|
g2.fill(new Ellipse2D.Float(0, 0, size, size));
|
||||||
|
g2.setComposite(AlphaComposite.SrcAtop);
|
||||||
|
g2.drawImage(originalImg.getScaledInstance(size, size, Image.SCALE_SMOOTH), 0, 0, null);
|
||||||
|
g2.dispose();
|
||||||
|
return outputImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BufferedImage resize(BufferedImage img, int newW, int newH) {
|
||||||
|
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
|
||||||
|
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
|
||||||
|
Graphics2D g2d = dimg.createGraphics();
|
||||||
|
g2d.drawImage(tmp, 0, 0, null);
|
||||||
|
g2d.dispose();
|
||||||
|
return dimg;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
BIN
src/main/resources/static/cert/avt.png
Normal file
BIN
src/main/resources/static/cert/avt.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.8 KiB |
BIN
src/main/resources/static/cert/seal.png
Normal file
BIN
src/main/resources/static/cert/seal.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
BIN
src/main/resources/static/cert/template.png
Normal file
BIN
src/main/resources/static/cert/template.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 130 KiB |
Loading…
x
Reference in New Issue
Block a user