小feng微信:

微信号:QF_qingfeng1024
image.png

业务需求

在日常的工作和生活中,我们经常遇到需要电子证书的功能,比如考试办法奖状进行奖励,考核办法证书,版权申请颁发版权证书等等。
为此,我们通过Graphics2D实现了基础的证书生成案例,当然也会有其他方式进行生成,此处我们是通过springboot+Graphics2D实现的在线证书生成的功能,希望对大家有帮助。

效果图

image.png
image.png

核心代码

组织证书生成的信息

  1. /**
  2. * @title toCreateCert
  3. * @description 测试-证书生成
  4. * @author Administrator
  5. * @updateTime 2022/4/25 0025 0:25
  6. */
  7. @RequestMapping("/toCreateCert")
  8. public ModelAndView toCreateCert(HttpServletRequest request, HttpSession session) {
  9. PageData pd = new PageData();
  10. pd.put("name","王宝强");
  11. pd.put("code","SD202103010001");
  12. pd.put("start_date", DateTimeUtil.getBeforeDaty(10));
  13. pd.put("end_date", DateTimeUtil.getDate());
  14. pd.put("kc_name","青锋微课堂-青锋后台系统开源产品培训");
  15. pd.put("class_time","48");
  16. ModelAndView mv = new ModelAndView();
  17. mv.addObject("pd",pd);
  18. mv.setViewName("web/base/cert");
  19. return mv;
  20. }

创建证书方法

  1. /**
  2. * @title createCert
  3. * @description
  4. * 参数:开始日期:start_date、结束日期:start_date,课程名称:kc_name,学时:class_time
  5. * 姓名:name,证书编号:code
  6. * @author Administrator
  7. * @updateTime 2022/4/25 0025 0:24
  8. */
  9. @RequestMapping(value = "/createCert", method = RequestMethod.GET)
  10. public void createCert(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
  11. PageData pd = new PageData(request);
  12. //证书模板路径
  13. String tempPath = session.getServletContext().getRealPath("/") + "/resources/images/cert.jpg";
  14. //生成后的存储路径
  15. String path = ParaUtil.common+"cert/"+ GuidUtil.getGuid()+".png";
  16. //字体文件路径-微软雅黑
  17. String fontPath = session.getServletContext().getRealPath("/") + "/resources/fonts/msyh.ttf";
  18. // String fontPath = ParaUtil.localName + "/resources/fonts/msyh.ttf";
  19. //第一行数据
  20. String line = pd.get("start_date").toString()+"至"+pd.get("end_date").toString()+"参加"+pd.get("kc_name")+
  21. "学习,计"+pd.get("class_time")+"学时。";
  22. //月份
  23. SimpleDateFormat sdf = new SimpleDateFormat("MM");
  24. String month = sdf.format(new Date());
  25. //电子章
  26. String url = session.getServletContext().getRealPath("/") + "/resources/images/qingfeng_dzz.png";;
  27. InputStream inputStream=new FileInputStream(url);
  28. compositePicture(tempPath, ParaUtil.localName+path,pd.get("name").toString()+":",pd.get("code").toString(),line, DateTimeUtil.getCurrentYear(),month,fontPath,inputStream);
  29. pd.put("show_cert_path", ParaUtil.cloudfile+path);
  30. pd.put("cert_path",path);
  31. Json json = new Json();
  32. json.setSuccess(true);
  33. json.setData(pd);
  34. json.setMsg("生成证书成功。");
  35. this.writeJson(response,json);
  36. }
  37. /**
  38. * @title: createCert
  39. * @description:
  40. * 参数:开始日期:start_date、结束日期:start_date,课程名称:kc_name,学时:class_time
  41. * 姓名:name,证书编号:code
  42. * @author: qingfeng
  43. * @date: 2021/3/6 0006 11:36
  44. */
  45. @RequestMapping(value = "/createCert", method = RequestMethod.GET)
  46. public void createCert(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
  47. PageData pd = new PageData(request);
  48. String tempPath = session.getServletContext().getRealPath("/") + "/resources/images/cert.jpg";
  49. String path = ParaUtil.common+"cert/"+ GuidUtil.getGuid()+".png";
  50. String fontPath = session.getServletContext().getRealPath("/") + "/resources/fonts/msyh.ttf";
  51. // String fontPath = ParaUtil.localName + "/resources/fonts/msyh.ttf";
  52. String line = pd.get("start_date").toString()+"至"+pd.get("end_date").toString()+"参加"+pd.get("kc_name")+
  53. "学习,计"+pd.get("class_time")+"学时。";
  54. //月份
  55. SimpleDateFormat sdf = new SimpleDateFormat("MM");
  56. String month = sdf.format(new Date());
  57. //电子章
  58. String url = session.getServletContext().getRealPath("/") + "/resources/images/qingfeng_dzz.png";;
  59. InputStream inputStream=new FileInputStream(url);
  60. compositePicture(tempPath, ParaUtil.localName+path,pd.get("name").toString()+":",pd.get("code").toString(),line, DateTimeUtil.getCurrentYear(),month,fontPath,inputStream);
  61. pd.put("show_cert_path", ParaUtil.cloudfile+path);
  62. pd.put("cert_path",path);
  63. Json json = new Json();
  64. json.setSuccess(true);
  65. json.setData(pd);
  66. json.setMsg("生成证书成功。");
  67. this.writeJson(response,json);
  68. }

生成证书的方法

image.png

  1. /**
  2. * @title 生成证书
  3. * @description 生成证书
  4. * @author Administrator
  5. * @updateTime 2022/4/25 0025 0:26
  6. */
  7. public static String compositePicture(String path,String outPath, String title,String code,String line, String year, String month,String fontPath,InputStream inputStream) {
  8. try {
  9. // 加载背景图片
  10. BufferedImage imageLocal = ImageIO.read(new File(path));
  11. int srcImgWidth = imageLocal.getWidth(null);
  12. int srcImgHeight = imageLocal.getHeight(null);
  13. System.out.println("srcImgWidth:"+srcImgWidth+",srcImgHeight:"+srcImgHeight);
  14. // 以背景图片为模板
  15. Graphics2D g = imageLocal.createGraphics();
  16. // 消除文字锯齿
  17. g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  18. InputStream is = new FileInputStream(new File(fontPath));
  19. // 在模板上添加用户二维码(地址,左边距,上边距,图片宽度,图片高度,未知)
  20. // Font font1 = new Font("微软雅黑", Font.PLAIN, 50);// 添加字体的属性设置-普通字体
  21. Font font1 = Font.createFont(Font.TRUETYPE_FONT, is);
  22. font1 = font1.deriveFont(Font.PLAIN,50);
  23. g.setFont(font1);
  24. Color color1 = new Color(133,95,43);
  25. g.setColor(color1);
  26. // 姓名
  27. g.drawString(title, 182, 460);
  28. // Font font2 = new Font("微软雅黑", Font.PLAIN, 30);// 添加字体的属性设置
  29. InputStream is1 = new FileInputStream(new File(fontPath));
  30. Font font2 = Font.createFont(Font.TRUETYPE_FONT, is1);
  31. font2 = font2.deriveFont(Font.PLAIN,30);
  32. g.setFont(font2);
  33. Color color2 = new Color(133,95,43);
  34. g.setColor(color2);
  35. // 证书编码-code
  36. g.drawString(code, 1130, 382);
  37. // 年
  38. g.drawString(year, 962, 926);
  39. // 月
  40. g.drawString(month, 1078, 926);
  41. // class_time
  42. // Font font3 = new Font("微软雅黑", Font.PLAIN, 38);// 添加字体的属性设置
  43. InputStream is2 = new FileInputStream(new File(fontPath));
  44. Font font3 = Font.createFont(Font.TRUETYPE_FONT, is2);
  45. font3 = font3.deriveFont(Font.PLAIN,38);
  46. g.setFont(font3);
  47. String one_line = line.substring(0,36);
  48. String two_line = line.substring(36);
  49. g.drawString(one_line, 260, 550);
  50. g.drawString(two_line, 180, 610);
  51. // 加盖电子章
  52. BufferedImage imageCode = ImageIO.read(inputStream);
  53. g.drawImage(imageCode, 920, imageLocal.getHeight() - 400, 260, 260, null);
  54. // 完成模板修改
  55. g.dispose();
  56. // 判断新文件的地址路径是否存在,如果不存在就创建一个
  57. File outputfile = new File(outPath);
  58. if (!outputfile.getParentFile().exists()) {
  59. outputfile.getParentFile().mkdirs();
  60. }
  61. // 生成新的合成过的用户二维码并写入新图片
  62. ImageIO.write(imageLocal, "png", outputfile);
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. return outPath;
  67. }