From bc3e29c2cb8d569fe42f6c8458e7a62ef6cf6829 Mon Sep 17 00:00:00 2001 From: cuibaocheng Date: Fri, 4 Jun 2021 15:29:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=8C=85=E5=90=8D=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../buildingpictures/util/CreateImage.java | 186 ------------------ 1 file changed, 186 deletions(-) delete mode 100644 module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/CreateImage.java diff --git a/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/CreateImage.java b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/CreateImage.java deleted file mode 100644 index 3bfce09..0000000 --- a/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/CreateImage.java +++ /dev/null @@ -1,186 +0,0 @@ -package cn.com.tenlion.buildingpictures.util; - -import java.awt.Color; -import java.awt.Font; -import java.awt.Graphics; -import java.awt.Image; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - -import javax.imageio.ImageIO; - -/** - * 生成动态图片 - * @类 名 : CreateImage - * @功能描述 : TODO - * @作者信息 : CBC - * @创建时间 : 2017-9-10上午12:21:09 - * @修改备注 : - */ -public class CreateImage { - - /** 模板图片路径 */ - private static String TEMPLATEPAHT = ""; - - /** 模板印章路径 */ - private static String TEMPLATEPATHYZ = ""; - - static { - // 初始化健康证图片模板路径 - TEMPLATEPAHT = "D:\\准考证.png"; - // 初始化健康证图片模板印章路径 - TEMPLATEPATHYZ = "D:\\准考证章.png"; - } - - private static int getCount(String content, int fontSize) { - char[] c = content.toCharArray(); - double count = 0; - for(int i = 0; i < c.length; i ++) { - String len = Integer.toBinaryString(c[i]); - if(len.length() > 8) { - count++; - }else{ - count = (count + 0.5); - } - } - return (int) (fontSize * count); - } - - public static void main(String[] args) { - Map map = new HashMap(); - map.put("标题", new CreateImageBean("string", "安荣通2021年4期培训准考证", 32, 95, null, 1126, 40)); - map.put("报名地市", new CreateImageBean("string", "呼和浩特市", 210, 175, null, null)); - map.put("考生姓名", new CreateImageBean("string", "崔宝铖", 210, 250, null, null)); - map.put("性别", new CreateImageBean("string", "男", 210, 329, null, null)); - map.put("报考级别", new CreateImageBean("string", "考全级", 210, 402, null, null)); - map.put("工作单位", new CreateImageBean("string", "呼和浩特市新城区塔里公租房", 210, 480, null, null)); - map.put("考点地址", new CreateImageBean("string", "呼和浩特市新城区惠新苑", 210, 558, null, null)); - - map.put("报名序号", new CreateImageBean("string", "C00015222", 693, 175, null, null)); - map.put("证件号码", new CreateImageBean("string", "612727199303151514", 693, 250, null, null)); - map.put("档案号", new CreateImageBean("string", "NHX0025666325", 693, 329, null, null)); - map.put("报考专业", new CreateImageBean("string", "建筑施工安全", 693, 402, null, null)); - - map.put("人员头像", new CreateImageBean("image", "D:\\崔宝铖.jpg", 971, 133, 184, 220)); - - map.put("考试科目", new CreateImageBean("string", "全科的考试的测试", 34, 710, null, 347)); - map.put("准考证号", new CreateImageBean("string", "NHXAJ022365", 381, 710, null, 292)); - map.put("考试时间", new CreateImageBean("string", "2021年4月25日13点", 678, 710, null, 273)); - map.put("考场", new CreateImageBean("string", "1", 954, 710, null, 106)); - map.put("座位号", new CreateImageBean("string", "96", 1063, 710, null, 94)); - - map.put("盖章", new CreateImageBean("image", TEMPLATEPATHYZ, 855, 280, 350, 350)); - createImage(map, "D:\\test.png", Color.black, 25, true, "宋体"); - } - - /** - * @Title : createImage - * @功能描述 : TODO - * @设定文件 : @param map 需要填充的数据集合 - * @设定文件 : @param path 文件输出的路径 - * @设定文件 : @return - * @返回类型 : String 文件路径 - * @throws : - */ - public static String createImage(Map map, String path, Color color, Integer fontSize, Boolean fontBold, String typeface) { - String filepath = null; - try{ - // 加载模板图片 - BufferedImage image = ImageIO.read(new File(TEMPLATEPAHT)); - // 得到图片操作对象 - Graphics graphics = image.getGraphics(); - // 设置文字的颜色为黑色 - graphics.setColor(color); - // 设置文字的字体,大小 - graphics.setFont(new Font(typeface, (fontBold ? Font.BOLD : null), fontSize)); - for(Entry bean : map.entrySet()) { - CreateImageBean b = bean.getValue(); - if("string".equals(b.getType())) { - // 自定义字体大小 - if(b.getFontSize() != null) { - graphics.setFont(new Font(typeface, (fontBold ? Font.BOLD : null), b.getFontSize())); - } - if(b.getWidth() != null) { - int count = getCount(b.getContent(), (b.getFontSize() == null ? fontSize : b.getFontSize())); - int startX = ((b.getWidth()) - count) / 2; - // 将文字放入模板中 - graphics.drawString(b.getContent(), startX + b.getX(), b.getY()); - }else{ - // 将文字放入模板中 - graphics.drawString(b.getContent(), b.getX(), b.getY()); - } - if(b.getFontSize() != null) { - // 将字体大小切换回默认 - graphics.setFont(new Font(typeface, (fontBold ? Font.BOLD : null), fontSize)); - } - } - if("image".equals(b.getType())) { - File file = new File(b.getContent()); - // 对头像进行裁剪 - Image img = thumbnail(file, b.getHeight(), b.getWidth()); - // 将头像放入模板中 - graphics.drawImage(img, b.getX(), b.getY(), null); - } - } - // 将健康证图片存储到本地 - createImage(path, image); - }catch (Exception e) { - e.printStackTrace(); - } - return filepath; - } - - /** - * 将image对象存储到本地 - * @Title : createImage - * @功能描述 : TODO - * @设定文件 : @param fileLocation 本地路径 - * @设定文件 : @param image 图片对象 - * @返回类型 : void - * @throws : - */ - private static void createImage(String fileLocation, BufferedImage image) { - try { - String formatName = fileLocation.substring(fileLocation.lastIndexOf(".") + 1); - ImageIO.write(image, formatName , new File(fileLocation)); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * 图片压缩-按照固定宽高原图压缩 - * @Title : thumbnail - * @功能描述 : TODO - * @设定文件 : @param img 本地图片地址 - * @设定文件 : @param width 图片宽度 - * @设定文件 : @param height 图片高度 - * @设定文件 : @return - * @设定文件 : @throws IOException - * @返回类型 : Image - * @throws : - */ - public static Image thumbnail(File img, int width, int height) throws IOException { - BufferedImage BI = ImageIO.read(img); - Image image = BI.getScaledInstance(width, height, Image.SCALE_SMOOTH); - BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - Graphics g = tag.getGraphics(); - g.setColor(Color.RED); - g.drawImage(image, 0, 0, null); - g.dispose(); - return image; - } - -} - - - - - - - -