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.UUID; 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() { // 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(); String filename = UUID.randomUUID().toString() + System.currentTimeMillis(); File outputfile = new File(outputSrc+ filename +".jpg"); ImageIO.write(image, "jpg", outputfile); System.out.println(no +","+ name +","+ filename); } 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 = "Training course on TCM somatology"; String trans3 = "on 2024.01.04-05"; //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(); } } }