diff --git a/mallinkCApi/src/main/java/com/iformall/utils/ImgIdeaUtil.java b/mallinkCApi/src/main/java/com/iformall/utils/ImgIdeaUtil.java new file mode 100644 index 000000000..16ebb8d46 --- /dev/null +++ b/mallinkCApi/src/main/java/com/iformall/utils/ImgIdeaUtil.java @@ -0,0 +1,175 @@ +package com.iformall.utils; + +import net.sourceforge.pinyin4j.PinyinHelper; +import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; +import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; +import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; +import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; +import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.ResourceUtils; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.InputStream; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +public class ImgIdeaUtil { + + private static final Logger logger = LoggerFactory.getLogger(ImgIdeaUtil.class); + + //模板图片 + private static final String imgTempPath = "imgTemp/gertificate.jpg"; + //字体路径 + private static final String fontPath = "opt/HWLS.TTF"; + + /** + * 生成图片证书 + * @return + */ + public static byte[] imgGertificate(String name, String englishName, LocalDateTime date){ + if(StringUtils.isBlank(name)){ + logger.error("名字为空,无法生成证书"); + return null; + } + if(name.length() == 2){ + name = name.replaceAll("(\\S)", "$0 "); + } + if(StringUtils.isBlank(englishName)){ + englishName = getUpEname(name); + } + if(date == null){ + date = LocalDateTime.now(); + } + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dMMM yyyy", Locale.UK); + String dateFormat = date.format(formatter); + + try { + + InputStream backImgUrl = ClassLoader.getSystemResourceAsStream(imgTempPath); + //InputStream backImgUrl = ImgIdeaUtil.class.getClassLoader().getResourceAsStream(imgTempPath); + + Font font = Font.createFont(Font.TRUETYPE_FONT, ResourceUtils.getFile("classpath:"+fontPath)); + BufferedImage backImg = ImageIO.read(backImgUrl); + Graphics g = backImg.getGraphics(); + + //图片加中文名 + Font fTxtBottomName = font.deriveFont(Font.PLAIN, 170); + //Font fTxtBottomName = new Font("华文隶书", Font.PLAIN, 170); + FontMetrics metricsName = g.getFontMetrics(fTxtBottomName); + int xName = (backImg.getWidth() - metricsName.stringWidth(name)) / 2; + Color myColorTxtBottomName = Color.BLACK; //颜色 + g.setColor(myColorTxtBottomName); + g.setFont(fTxtBottomName); + g.drawString(name, xName , 1150);//g.drawString(文字, x 位置, y 位置); + + //图片加英文名 + Font fTxtBottomEname = font.deriveFont(Font.PLAIN, 120); + //Font fTxtBottomEname = new Font("华文隶书", Font.PLAIN, 120); + FontMetrics metricsEname = g.getFontMetrics(fTxtBottomEname); + int xEname = (backImg.getWidth() - metricsEname.stringWidth(englishName)) / 2; + Color myColorTxtBottomEname = Color.BLACK; + g.setColor(myColorTxtBottomEname); + g.setFont(fTxtBottomEname); + g.drawString(englishName, xEname, 1300); + + //图片加日期 + Font fTxtBottomDate = font.deriveFont(Font.PLAIN, 90); + //Font fTxtBottomDate = new Font("华文隶书", Font.PLAIN, 90); + Color myColorTxtBottomDate = Color.BLACK; + g.setColor(myColorTxtBottomDate); + g.setFont(fTxtBottomDate); + g.drawString(dateFormat, 440, 2100); + + //调这个方法就是开始这个整合 (可以多张图片,多个文字整合成一张图片,只有把他们放在这方法里面就行) + g.dispose(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ImageIO.write(backImg, "png", out); + + return out.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + logger.error("生成证书异常", e.getMessage()); + } + return null; + } + + /** + * //姓、名的第一个字母需要为大写 + * @param name + * @return + */ + public static String getUpEname(String name) { + char[] strs = name.toCharArray(); + String newname = null; + + if (strs.length == 2) { + newname = toUpCase(getEname("" + strs[0])) + " " + toUpCase(getEname("" + strs[1])); + } else if (strs.length == 3){ + newname = toUpCase(getEname("" + strs[0])) + " " + + toUpCase(getEname("" + strs[1] + strs[2])); + }else if (strs.length == 4){ + newname = toUpCase(getEname("" + strs[0] + strs[1])) + " " + + toUpCase(getEname("" + strs[2] + strs[3])); + } else{ + newname = toUpCase(getEname(name)); + } + return newname; + } + + + /** + * //将中文转换为英文 + * @param name + * @return + */ + public static String getEname(String name){ + HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat(); + pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); + pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); + pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V); + String s = ""; + try { + s = PinyinHelper.toHanyuPinyinString(name, pyFormat, ""); + } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) { + badHanyuPinyinOutputFormatCombination.printStackTrace(); + } + return s; + } + /** + * 首字母大写 + * @param str + * @return + */ + private static String toUpCase(String str) { + StringBuffer newstr = new StringBuffer(); + newstr.append((str.substring(0, 1)).toUpperCase()).append( + str.substring(1, str.length())); + return newstr.toString(); + } + + +// public static void main(String[] args) { +// String serverUploadImgUrl = "C:/Users/xiaohu/Desktop/img"; // 图片保存路径 +// try { +// byte[] bytes = imgGertificate("郑方元", null, null); +// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); +// BufferedImage bi1 =ImageIO.read(bais); +// +// ImageIO.write(bi1, "png", new File(serverUploadImgUrl+"/0.png")); +// +// } catch (Exception e) { +// e.printStackTrace(); +// } +// System.out.println("结束"); +// } +} diff --git a/mallinkCApi/src/main/resources/imgTemp/gertificate.jpg b/mallinkCApi/src/main/resources/imgTemp/gertificate.jpg new file mode 100644 index 000000000..6766d3149 Binary files /dev/null and b/mallinkCApi/src/main/resources/imgTemp/gertificate.jpg differ diff --git a/mallinkCApi/src/main/resources/opt/HWLS.TTF b/mallinkCApi/src/main/resources/opt/HWLS.TTF new file mode 100644 index 000000000..bd038fd18 Binary files /dev/null and b/mallinkCApi/src/main/resources/opt/HWLS.TTF differ diff --git a/pom.xml b/pom.xml index c423a0294..eee3ca238 100644 --- a/pom.xml +++ b/pom.xml @@ -368,6 +368,13 @@ 1.11.423 + + + com.belerweb + pinyin4j + 2.5.0 + +