From c4d752a1d340652d9c7d2abb5abff65cdee40d69 Mon Sep 17 00:00:00 2001 From: yujian <yujian@123.com> Date: 星期一, 20 五月 2019 09:13:56 +0800 Subject: [PATCH] 邀请素材管理、邀请素材画图 --- fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 177 insertions(+), 0 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java index 69ca7ad..f9ac319 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java @@ -22,12 +22,15 @@ import java.math.BigDecimal; import java.net.MalformedURLException; import java.net.URL; +import java.util.Date; import java.util.HashMap; import java.util.List; import javax.imageio.ImageIO; +import org.yeshi.utils.DateLunarUtil; import org.yeshi.utils.HttpUtil; +import org.yeshi.utils.entity.DateInfo; import com.yeshi.fanli.entity.bus.activity.RecommendActivityTaoBaoGoods; import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; @@ -1172,4 +1175,178 @@ return new int[] { width, height }; } + + /** + * 缁樺埗閭�璇烽棶鍊欏浘 + * @param bgPigInStream 鑳屾櫙鍥� + * @param portraitInStream 鐢ㄦ埛澶村儚 + * @param erCodeInStream 浜岀淮鐮� + * @param targetPath 璺緞 + * @param inviteCode 閭�璇风爜 + * @param content 鏂囧瓧鍐呭 + * @param date 鏄剧ず鏃ュ巻 + * @throws IOException + */ + public static void drawInviteToGreet(InputStream bgPigInStream, InputStream portraitInStream, + InputStream erCodeInStream, String targetPath, String inviteCode, String content, Date date) + throws IOException { + // 鑳屾櫙鍥剧墖 + BufferedImage bgImage = ImageIO.read(bgPigInStream); + final BufferedImage targetImg = new BufferedImage(bgImage.getWidth(), bgImage.getHeight(), + BufferedImage.TYPE_INT_RGB); + + HashMap<Key, Object> mapH = new HashMap<Key, Object>(); + mapH.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// 鎶楅敮榻柯狅紙鎶楅敮榻挎�诲紑鍏筹級聽聽 + mapH.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);// 鏂囧瓧鎶楅敮榻柯� + + // 鐢� 鑳屾櫙鍥剧墖 + final Graphics2D g2d = targetImg.createGraphics(); + g2d.drawImage(bgImage, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null); + g2d.setRenderingHints(mapH); + + // 鐢� 钂欏眰 + BufferedImage mengCeng = ImageIO + .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/invite/mc.png")); + g2d.drawImage(mengCeng, 0, bgImage.getHeight() / 2, bgImage.getWidth(), bgImage.getHeight() / 2, null); + + int width = bgImage.getWidth(); + int height = bgImage.getHeight(); + + int pX = width - 250; + int pY = height - 290; // 320 + int size = 210; // 230 + // 鐢� 浜岀淮鐮� + BufferedImage qrCodeImage = ImageIO.read(erCodeInStream); + qrCodeImage = ImageUtil.qrCodeImage(g2d, qrCodeImage, pX, pY, size, size); // 浜岀淮鐮侀暱瀹� + // 230*230 + + // 鐢� 澶村儚 + int portraitSize = size * 5 / 23; + int pPX = pX + size / 2 - portraitSize / 2; + int pPY = pY + size / 2 - portraitSize / 2; + BufferedImage portraitImg = ImageIO.read(portraitInStream); + portraitImg = ImageUtil.portraitImg(g2d, portraitImg, pPX, pPY, portraitSize, portraitSize);// 澶村儚闀垮 + + // 鏂囧瓧鐧借壊 + Color color = new Color(255, 255, 255); + String os = System.getProperty("os.name"); + String fontBoldPath = "/usr/share/fonts/yahei.ttf"; + if (os.toLowerCase().startsWith("win")) { + fontBoldPath = "D:/yahei.ttf"; + } + + Font boldFont = null; + // 鐢婚個璇风爜(閭�璇风爜鐨勫ぇ灏忎笌瀛椾綋鍥哄畾) + if (!StringUtil.isNullOrEmpty(inviteCode)) { + String st = "閭�璇风爜锛�" + inviteCode; + try { + boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(32.0f); + } catch (FontFormatException e) { + e.printStackTrace(); + } + g2d.setFont(boldFont); + g2d.setColor(new Color(255, 255, 255)); + + FontMetrics fm = g2d.getFontMetrics(boldFont); + int textLength = fm.stringWidth(st); + + int ax = (width - textLength) - 30; + int ay = bgImage.getHeight() - 50 + boldFont.getSize() / 2 - 5; + + g2d.setColor(new Color(255, 255, 255)); + g2d.drawString(st, ax, ay); + } + + // 宸﹁竟璺� + int padding_left = 30; + + // 鏃ュ巻 + try { + if (date == null) { + date = new Date(); + } + + boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); + g2d.setFont(boldFont); + g2d.setColor(color); + g2d.setRenderingHints(mapH); + + DateInfo dateInfo = DateLunarUtil.getDateInfo(date); + // 鍐滃巻 + g2d.drawString(dateInfo.getLunar_month() + dateInfo.getLunar_day(), padding_left, height - 80); + // 澶╁共鍦版敮 + g2d.drawString(dateInfo.getAgenary_month() + " " + dateInfo.getAgenary_day(), padding_left, height - 140); + + // 褰撳墠鏃ユ湡 + Font boldFontMax = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(100.0f); + g2d.setFont(boldFontMax); + + if (dateInfo.getDay() < 10) { + g2d.drawString("0" + dateInfo.getDay().toString(), padding_left, height - 200); + } else { + g2d.drawString(dateInfo.getDay().toString(), padding_left, height - 200); + } + + String theDate = ""; + if (dateInfo.getMonth() < 10) { + theDate = "/0"; + } else { + theDate = "/"; + } + theDate += dateInfo.getMonth() + "/" + dateInfo.getYear() + " " + dateInfo.getWeekDay(); + g2d.setFont(boldFont); + g2d.drawString(theDate, padding_left + boldFontMax.getSize() + 15, height - 200); + + } catch (Exception e) { + e.printStackTrace(); + } + + if (!StringUtil.isNullOrEmpty(content)) { + // 鏂囧瓧鍐呭 + try { + boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); + g2d.setFont(boldFont); + g2d.setColor(color); + g2d.setRenderingHints(mapH); + + int lineHeight = 70; + int lineTextLength = 19; + + if (content.length() <= lineTextLength) { + g2d.drawString(content, padding_left, height - 350); + } else { + String text = ""; + int mod = content.length() % lineTextLength; + if (mod == 0) { + text = content.substring(content.length() - lineTextLength, content.length()); + content = content.substring(0, content.length() - lineTextLength); + } else { + text = content.substring(content.length() - mod, content.length()); + content = content.substring(0, content.length() - mod); + } + g2d.drawString(text, padding_left, height - 350); + + int i = 1; + while (true) { + if (content.length() <= 17) { + g2d.drawString(content, padding_left, height - 350 - lineHeight * i); + break; + } else { + text = content.substring(content.length() - lineTextLength, content.length()); + content = content.substring(0, content.length() - lineTextLength); + g2d.drawString(text, padding_left, height - 350 - lineHeight * i); + i++; + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + OutputStream out = new FileOutputStream(new File(targetPath)); + ImageIO.write(targetImg, "JPEG", out); + out.flush(); + out.close(); + } } -- Gitblit v1.8.0