证书工具

This commit is contained in:
wuchunlei
2024-01-03 16:25:47 +08:00
parent 40f2f46da0
commit 8254514f8c

View File

@@ -0,0 +1,164 @@
package com.peanut.common.utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CertificateUtil extends Thread{
String inputSrc;
String outputSrc;
String no;
String name;
String time;
String des;
String trans;
String trans2;
String trans3;
String endy;
String endm;
String endd;
public CertificateUtil(String inputSrc,String outputSrc,String no,String name,String time,
String des,String trans,String trans2,String trans3,String endy,String endm,String endd){
this.inputSrc=inputSrc;
this.outputSrc = outputSrc;
this.no = no;
this.name = name;
this.time = time;
this.des = des;
this.trans = trans;
this.trans2 = trans2;
this.trans3 = trans3;
this.endy = endy;
this.endm = endm;
this.endd = endd;
}
@Override
public void run() {
Date startTime = new Date();
System.out.println(name+"-开始!");
try {
Image src = ImageIO.read(new File(inputSrc));
// 获取图片的高和宽
int wideth = src.getWidth(null);
int height = src.getHeight(null);
// 新增一个图片缓冲
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(src, 0, 0, wideth, height, null);
// 设置字体颜色颜色也可以直接new定义rgba例如new Color(17, 16, 44)
g.setColor(new Color(51,82,132));
// size字体大小Font.BOLD字体加粗
g.setFont(new Font("Arial", Font.BOLD, 80));
FontMetrics metrics = g.getFontMetrics();
int nox = (wideth - metrics.stringWidth(no))/2;
int noy = 1405 - metrics.getHeight();//1305
g.drawString(no, nox, noy);
// 设置字体颜色颜色也可以直接new定义rgba例如new Color(17, 16, 44)
g.setColor(Color.BLACK);
// size字体大小Font.BOLD字体加粗
g.setFont(new Font("宋体", Font.BOLD, 150));
FontMetrics metricss = g.getFontMetrics();
int namex = (wideth - metricss.stringWidth(name))/2;
int namey = 2055 - metricss.getHeight();//1935
g.drawString(name, namex, namey);
g.setFont(new Font("宋体", Font.BOLD, 115));
FontMetrics times = g.getFontMetrics();
int timex = (wideth - times.stringWidth(time))/2;
int timey = 2370 - times.getHeight();//2270
g.drawString(time, timex, timey);
FontMetrics dess = g.getFontMetrics();
int desx = (wideth - dess.stringWidth(des))/2;
int desy = 2635 - dess.getHeight();//2535
g.drawString(des, desx, desy);
g.setFont(new Font("Arial", Font.BOLD, 75));
FontMetrics transs = g.getFontMetrics();
int transx = (wideth - transs.stringWidth(trans))/2;
int transy = 2735 - transs.getHeight();//2685
g.drawString(trans, transx, transy);
FontMetrics trans2s = g.getFontMetrics();
int trans2x = (wideth - trans2s.stringWidth(trans2))/2;
g.drawString(trans2, trans2x, transy+120);
FontMetrics trans3s = g.getFontMetrics();
int trans3x = (wideth - trans3s.stringWidth(trans3))/2;
g.drawString(trans3, trans3x, transy+240);
g.setFont(new Font("宋体", Font.BOLD, 80));
g.drawString(endy,1800,3440);
g.drawString(endm,2090,3440);
g.drawString(endd,2250,3440);
// 释放资源
g.dispose();
File outputfile = new File(outputSrc+name+".png");
ImageIO.write(image, "png", outputfile);
Date endTime = new Date();
System.out.println(endTime.getTime()-startTime.getTime() + "");
System.out.println(name+"-生成完成!");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception{
String nameflie = "F:\\aimg\\name.txt"; //人名,一列
String inputSrc = "F:\\aimg\\c.png"; //图片模板
String outputSrc = "F:\\aimg\\形质病\\"; //生成地址
String no = "No.XZB202401";
int nostart = 0;
String time = "于2024年1月4日至5日";
String des = "圆满完成中医形质病学培训班";
String trans = "Successful completion of the";
String trans2 = "Chinese Medicine Morphology and Disease Training";
String trans3 = "course on 2024 1.4-5";
//Traditional Chinese Medicine Morphology and Disease Training Course
String endy = "2024";
String endm = "1";
String endd = "6";
// 1、创建服务创建线程池
ExecutorService service = Executors.newFixedThreadPool(30);
try (BufferedReader br = new BufferedReader(new FileReader(nameflie))) {
String line;
while ((line = br.readLine()) != null) {
// 对每一行进行处理
nostart++;
CertificateUtil c1 = new CertificateUtil(inputSrc,outputSrc,no+String.format("%03d",nostart),line,
time,des,trans,trans2,trans3,endy,endm,endd);
service.execute(c1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}