From f4a0f2acc63d7785eab108419a4e16f5f688cb95 Mon Sep 17 00:00:00 2001 From: yujian <yujian@163.com> Date: 星期六, 18 一月 2020 12:06:27 +0800 Subject: [PATCH] 用户注册信息 --- fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java | 855 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 826 insertions(+), 29 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 f9ac319..5fe96b0 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java @@ -1,5 +1,6 @@ package com.yeshi.fanli.util; +import java.awt.AlphaComposite; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; @@ -33,11 +34,412 @@ import org.yeshi.utils.entity.DateInfo; import com.yeshi.fanli.entity.bus.activity.RecommendActivityTaoBaoGoods; +import com.yeshi.fanli.entity.bus.user.UserInfo; +import com.yeshi.fanli.entity.goods.CommonGoods; import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; import com.yeshi.fanli.log.LogHelper; import com.yeshi.fanli.util.taobao.TaoBaoUtil; +import com.yeshi.fanli.vo.goods.GoodsDetailVO; public class ImageUtil { + + // 鐢诲晢鍝佸垎浜浘 + public static InputStream drawGoodsShareXCX(InputStream qrcode, UserInfo user, GoodsDetailVO goods) { + try { + String fontPath = "/usr/share/fonts/PingFang_Medium.ttf"; + String os = System.getProperty("os.name"); + if (os.toLowerCase().startsWith("win")) { + fontPath = "D:/PingFang_Medium.ttf"; + } + + String fontBoldPath = "/usr/share/fonts/PingFang_Heavy_0.ttf"; + if (os.toLowerCase().startsWith("win")) { + fontBoldPath = "D:/PingFang_Heavy_0.ttf"; + } + Font font24 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(24.0f); + Font font26 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(26.0f); + Font boldFont36 = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); + + int px = 640; // 鍥剧墖瀹藉害 + int py = 1061; // 鍥剧墖楂樺害 + final BufferedImage targetImg = new BufferedImage(px, py, 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 = (Graphics2D) targetImg.getGraphics(); + g2d.setRenderingHints(mapH); + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, px, py); + + int x = 640; + int y = 640; + int spacing = 20; // 鍙宠竟璺� + + // 1銆佺敾鍟嗗搧涓诲浘 + InputStream picStream = HttpUtil.getAsInputStream(goods.getPicUrl().replace("https://", "http://")); + g2d.drawImage(ImageIO.read(picStream), 0, 0, x, y, null); + + // 2銆佺敾鏉ユ簮鍥炬爣 + int length = 10; + int row1 = 260; + int iconWidth = 72; + String source = ""; + InputStream icon = null; + int goodsType = goods.getGoodsType(); + if (goodsType == Constant.SOURCE_TYPE_TAOBAO) { + source = "娣樺疂"; + if (goods.getShopType() == 10) { + icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tb.png"); + } else { + icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tm.png"); + } + } else if (goodsType == Constant.SOURCE_TYPE_JD) { + source = "浜笢"; + icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_jd.png"); + } else if (goodsType == Constant.SOURCE_TYPE_PDD) { + source = "鎷煎澶�"; + length = 9; + row1 = row1 - 26; + iconWidth = iconWidth + 28; + icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_pdd.png"); + } else { + return null; + } + g2d.drawImage(ImageIO.read(icon), spacing, y + 20, iconWidth, 34, null); + + // 3銆佸晢鍝佹爣棰樺唴瀹� + int row = 0; + String title = goods.getTitle(); + length = ImageUtil.getTextLengthByWidth(g2d, font26, title, row1, length); + g2d.setFont(font26); + g2d.setColor(new Color(0, 0, 0)); + g2d.drawString(title.substring(0, length), spacing + iconWidth + 5, y + 45 + row * 36); + + title = title.substring(length); + row++; + // 鍒ゆ柇鏍囬鍐呭鏄惁鐢诲畬,鏈�澶氬彲鐢讳笁琛� + while (title.length() > 0 && row < 3) { + length = ImageUtil.getTextLengthByWidth(g2d, font26, title, 320, 11); + g2d.drawString(title.substring(0, length), spacing, y + 45 + row * 36); + title = title.substring(length); + row++; + } + + if (!goods.isHasCoupon()) { + // 鏃犲埜 + g2d.setColor(new Color(229, 0, 92)); + g2d.drawString("楼 ", spacing, y + 175); + g2d.setFont(boldFont36); + g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()).toString(), spacing + 21, y + 175); + } else { + // 鍒稿悗浠� + g2d.setFont(font26); + g2d.setColor(new Color(229, 0, 92)); + g2d.drawString("鍒稿悗浠� 楼 ", spacing, y + 175); + g2d.setFont(boldFont36); + g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getCouponPrice()).toString(), spacing + 116, + y + 175); + + // 娓犻亾鍘熶环 + g2d.setColor(new Color(153, 153, 153)); + g2d.setFont(font24); + g2d.drawString(goods.getPriceName() + " 楼 " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()), + spacing, y + 210); + + // 鍒稿瓧 + InputStream quan = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_quan.png"); + g2d.drawImage(ImageIO.read(quan), spacing, y + 240, 47, 40, null); + // 鍒搁潰棰� + String quanAmount = "楼 " + goods.getCouponInfo().getAmount(); + FontMetrics fm = g2d.getFontMetrics(font26); + int textLength = fm.stringWidth(quanAmount); + + g2d.setFont(font26); + g2d.setColor(new Color(229, 0, 92)); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.drawRoundRect(spacing + 42, y + 240, textLength + 20, 39, 10, 10); + g2d.drawString(quanAmount, spacing + 55, y + 270); + + } + + // 鑷惀 銆佸寘閭� + if (goods.getShopType() == 21) { + InputStream ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_ziying.png"); + g2d.drawImage(ImageIO.read(ziying), spacing + 300, y + 150, 60, 30, null); + } else if (goods.isBaoyou()) { + InputStream ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_baoyou.png"); + g2d.drawImage(ImageIO.read(ziying), spacing + 300, y + 150, 60, 30, null); + } + + // 閿�閲� + String sale = null; + int salesType = goods.getSalesType(); + if (salesType == 1) { + sale = "鏈堥攢"; + } else if (salesType == 2) { + sale = "2灏忔椂閿�閲�"; + } else if (salesType == 3) { + sale = "浠婃棩閿�閲�"; + } else if (salesType == 4) { + sale = "鎬婚攢閲�"; + } else { + sale = "鏈堥攢"; + } + sale = sale + " " + goods.getSalesCount(); + FontMetrics fm = g2d.getFontMetrics(font24); + int textLength = fm.stringWidth(sale); + g2d.setFont(font24); + g2d.setColor(new Color(153, 153, 153)); + g2d.drawString(sale, spacing + 360 - textLength, y + 210); + + // 浜岀淮鐮佸浘妗� + g2d.setColor(Color.WHITE); + InputStream codeFrame = ImageUtil.class.getClassLoader().getResourceAsStream("image/share/qr_code_frame_xcx.png"); + g2d.drawImage(ImageIO.read(codeFrame), spacing + 405, y + 20, 200, 220, null); + // 鐢讳簩缁寸爜 + int codeSize = 170; + g2d.drawImage(ImageIO.read(qrcode), spacing + 420, y + 38, codeSize, codeSize, null); + + // 澶村儚 + InputStream portraitStream = null; + try { + portraitStream = HttpUtil.getAsInputStream(user.getPortrait()); + } catch (Exception e) { + e.printStackTrace(); + } + + if (portraitStream == null) { + portraitStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/official_default_head.jpg"); + } + BufferedImage portraitImg = ImageIO.read(portraitStream); + BufferedImage zoomInImage = ImageUtil.zoomInImage(portraitImg, 84, 84); + BufferedImage roundImage = ImageUtil.roundImage(zoomInImage, 84); + g2d.drawImage(roundImage, spacing, y + 310, 84, 84, null); + + g2d.setFont(font24); + g2d.setColor(new Color(0, 0, 0)); + g2d.drawString(user.getNickName(), spacing + 84 + 17, y + 335); + + g2d.setFont(font24); + g2d.setColor(new Color(153, 153, 153)); + g2d.drawString(source + "鍟嗗搧99%閮芥湁浼樻儬鎴栬繑鍒�", spacing + 84 + 17, y + 385); + + g2d.dispose(); + + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + ImageIO.write(targetImg, "JPEG", aos); + return new ByteArrayInputStream(aos.toByteArray()); + } catch (Exception e) { + e.printStackTrace(); + try { + LogHelper.errorDetailInfo(e); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + return null; + } + + /** + * 鍗曚釜鍟嗗搧鍒嗕韩鍥� + * + * @param qrcode 浜岀淮鐮佹祦 + * @param portrait 澶村儚娴� + * @param goods 鍟嗗搧淇℃伅 + * @return + */ + public static InputStream drawGoodsShareSingle(InputStream qrcode, InputStream portrait, GoodsDetailVO goods) { + try { + String fontPath = "/usr/share/fonts/PingFang_Medium.ttf"; + String os = System.getProperty("os.name"); + if (os.toLowerCase().startsWith("win")) { + fontPath = "D:/PingFang_Medium.ttf"; + } + + String fontBoldPath = "/usr/share/fonts/PingFang_Heavy_0.ttf"; + if (os.toLowerCase().startsWith("win")) { + fontBoldPath = "D:/PingFang_Heavy_0.ttf"; + } + Font font24 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(24.0f); + Font font26 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(26.0f); + Font boldFont36 = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); + + int px = 640; // 鍥剧墖瀹藉害 + int py = 1061; // 鍥剧墖楂樺害 + if (!goods.isHasCoupon()) { + py = py - 131; + } + + final BufferedImage targetImg = new BufferedImage(px, py, 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 = (Graphics2D) targetImg.getGraphics(); + g2d.setRenderingHints(mapH); + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, px, py); + + int x = 640; + int y = 640; + int spacing = 20; // 鍙宠竟璺� + + // 1銆佺敾鍟嗗搧涓诲浘 + InputStream picStream = HttpUtil.getAsInputStream(goods.getPicUrl().replace("https://", "http://")); + g2d.drawImage(ImageIO.read(picStream), 0, 0, x, y, null); + + // 2銆佺敾鏉ユ簮鍥炬爣 + int length = 10; + int row1 = 260; + int iconWidth = 72; + InputStream icon = null; + int goodsType = goods.getGoodsType(); + if (goodsType == Constant.SOURCE_TYPE_TAOBAO) { + if (goods.getShopType() == 10) { + icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tb.png"); + } else { + icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tm.png"); + } + } else if (goodsType == Constant.SOURCE_TYPE_JD) { + icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_jd.png"); + } else if (goodsType == Constant.SOURCE_TYPE_PDD) { + length = 9; + row1 = row1 - 26; + iconWidth = iconWidth + 28; + icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_pdd.png"); + } else { + return null; + } + g2d.drawImage(ImageIO.read(icon), spacing, y + 20, iconWidth, 34, null); + + // 3銆佸晢鍝佹爣棰樺唴瀹� + int row = 0; + String title = goods.getTitle(); + length = ImageUtil.getTextLengthByWidth(g2d, font26, title, row1, length); + g2d.setFont(font26); + g2d.setColor(new Color(0, 0, 0)); + g2d.drawString(title.substring(0, length), spacing + iconWidth + 5, y + 45 + row * 36); + + title = title.substring(length); + row++; + // 鍒ゆ柇鏍囬鍐呭鏄惁鐢诲畬,鏈�澶氬彲鐢讳笁琛� + while (title.length() > 0 && row < 3) { + length = ImageUtil.getTextLengthByWidth(g2d, font26, title, 320, 11); + g2d.drawString(title.substring(0, length), spacing, y + 45 + row * 36); + title = title.substring(length); + row++; + } + + String code_frame = "image/share/qr_code_frame.png"; + if (!goods.isHasCoupon()) { + // 鏃犲埜 + code_frame = "image/share/qr_code_frame_no_coupon.png"; + + g2d.setColor(new Color(229, 0, 92)); + g2d.drawString("楼 ", spacing, y + 175); + g2d.setFont(boldFont36); + g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()).toString(), spacing + 21, y + 175); + } else { + // 鍒稿悗浠� + g2d.setFont(font26); + g2d.setColor(new Color(229, 0, 92)); + g2d.drawString("鍒稿悗浠� 楼 ", spacing, y + 175); + g2d.setFont(boldFont36); + g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getCouponPrice()).toString(), spacing + 116, + y + 175); + + // 娓犻亾鍘熶环锛堝湪鍞环锛� + g2d.setColor(new Color(153, 153, 153)); + g2d.setFont(font24); + g2d.drawString(goods.getPriceName() + " 楼 " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()), + spacing, y + 210); + + // 鍒稿瓧 + InputStream quan = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_quan.png"); + g2d.drawImage(ImageIO.read(quan), spacing, y + 240, 47, 40, null); + // 鍒搁潰棰� + String quanAmount = "楼 " + goods.getCouponInfo().getAmount(); + FontMetrics fm = g2d.getFontMetrics(font26); + int textLength = fm.stringWidth(quanAmount); + g2d.setFont(font26); + g2d.setColor(new Color(229, 0, 92)); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.drawRoundRect(spacing + 42, y + 240, textLength + 20, 39, 10, 10); + g2d.drawString(quanAmount, spacing + 55, y + 270); + + // 鎻愮ず璇� + InputStream tipStream = ImageUtil.class.getClassLoader() + .getResourceAsStream("image/share/tip_angle.png"); + g2d.drawImage(ImageIO.read(tipStream), spacing, y + 300, 601, 101, null); + } + + // 鑷惀 銆佸寘閭� + if (goods.getShopType() == 21) { + InputStream ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_ziying.png"); + g2d.drawImage(ImageIO.read(ziying), spacing + 300, y + 150, 60, 30, null); + } else if (goods.isBaoyou()) { + InputStream ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_baoyou.png"); + g2d.drawImage(ImageIO.read(ziying), spacing + 300, y + 150, 60, 30, null); + } + + // 閿�閲� + String sale = null; + int salesType = goods.getSalesType(); + if (salesType == 1) { + sale = "鏈堥攢"; + } else if (salesType == 2) { + sale = "2灏忔椂閿�閲�"; + } else if (salesType == 3) { + sale = "浠婃棩閿�閲�"; + } else if (salesType == 4) { + sale = "鎬婚攢閲�"; + } else { + sale = "鏈堥攢"; + } + sale = sale + " " + goods.getSalesCount(); + FontMetrics fm = g2d.getFontMetrics(font24); + int textLength = fm.stringWidth(sale); + g2d.setFont(font24); + g2d.setColor(new Color(153, 153, 153)); + g2d.drawString(sale, spacing + 360 - textLength, y + 210); + + // 浜岀淮鐮佸浘妗� + g2d.setColor(Color.WHITE); + InputStream codeFrame = ImageUtil.class.getClassLoader().getResourceAsStream(code_frame); + g2d.drawImage(ImageIO.read(codeFrame), spacing + 405, y + 20, 200, 261, null); + + // 鐢� 浜岀淮鐮� + int codeSize = 170; + g2d.drawImage(ImageIO.read(qrcode), spacing + 420, y + 38, codeSize, codeSize, null); + + // 鐢讳簩缁寸爜涓� 澶村儚 + int portraitSize = 40; + int pPX = spacing + 425 + codeSize / 2 + -portraitSize / 2; + int pPY = y + 35 + codeSize / 2 - portraitSize / 2; + BufferedImage portraitImg = ImageIO.read(portrait); + portraitImg = ImageUtil.roundImage(portraitImg, 10); + // 澶村儚鐧借壊杈规 + g2d.fillRoundRect(pPX - 4, pPY - 4, portraitSize + 8, portraitSize + 8, 10, 10); + // 澶村儚 + g2d.drawImage(portraitImg, pPX, pPY, portraitSize, portraitSize, null); + + // 缁撴潫 + g2d.dispose(); + + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + ImageIO.write(targetImg, "JPEG", aos); + return new ByteArrayInputStream(aos.toByteArray()); + } catch (Exception e) { + e.printStackTrace(); + try { + LogHelper.errorDetailInfo(e); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + return null; + } // 鐢诲晢鍝佸垎浜浘 public static InputStream drawGoodsShareImg(InputStream qrcodeStream, InputStream portrait, @@ -199,7 +601,6 @@ font = font.deriveFont(30.0f); g2d.setFont(font); g2d.setColor(new Color(229, 0, 93)); - // g2d.drawString("杩斿埄鍒告彁閱掓偍", 250 + 17, 1080 + 17); g2d.setColor(new Color(102, 102, 102)); g2d.drawString("闀挎寜璇嗗埆浜岀淮鐮侀鍙栦紭鎯犲埜", 250 + 17, 1130 + 17); @@ -449,7 +850,6 @@ font = font.deriveFont(50.0f); g2d.setFont(font); g2d.setColor(new Color(229, 0, 93)); - // g2d.drawString("杩斿埄鍒告彁閱掓偍", 312, 1118 + 40); g2d.setColor(new Color(102, 102, 102)); g2d.drawString("闀挎寜璇嗗埆浜岀淮鐮侀鍙栦紭鎯犲埜", 312, 1118 + 77); @@ -716,7 +1116,6 @@ font = font.deriveFont(50.0f); g2d.setFont(font); g2d.setColor(new Color(229, 0, 93)); - // g2d.drawString("杩斿埄鍒告彁閱掓偍", 312, 1118 + 40); g2d.setColor(new Color(102, 102, 102)); g2d.drawString("闀挎寜璇嗗埆浜岀淮鐮佸厤璐归鍒�", 312, 1150); @@ -750,6 +1149,274 @@ } } + return null; + } + + /** + * 缁樺埗澶х殑鍟嗗搧鍔ㄦ�佸垎浜浘 + * + * @param qrcodeStream + * @param portrait + * @param goods + * @return + */ + public static InputStream drawGoodsSharePicture(InputStream qrcodeStream, InputStream portrait, + List<CommonGoods> goodsList) { + + String fontPath = "/usr/share/fonts/PingFang_Medium.ttf"; + String os = System.getProperty("os.name"); + if (os.toLowerCase().startsWith("win")) { + fontPath = "D:/PingFang_Medium.ttf"; + } + + String fontBoldPath = "/usr/share/fonts/PingFang_Heavy_0.ttf"; + if (os.toLowerCase().startsWith("win")) { + fontBoldPath = "D:/PingFang_Heavy_0.ttf"; + } + + final BufferedImage targetImg = new BufferedImage(1420, 1334, 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 = (Graphics2D) targetImg.getGraphics(); + g2d.setRenderingHints(mapH); + + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, 1420, 1334); + Font font = null; + try { + font = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(30.0f); + } catch (FontFormatException e1) { + e1.printStackTrace(); + } catch (IOException e1) { + e1.printStackTrace(); + } + + Font boldFont = null; + try { + boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(50.0f); + } catch (FontFormatException e2) { + e2.printStackTrace(); + } catch (IOException e2) { + e2.printStackTrace(); + } + g2d.setFont(font); + + try { + boolean index = true; + // 鐢荤涓�寮� + for (int i = 0; i < goodsList.size(); i++) { + CommonGoods goods = goodsList.get(i); + BigDecimal couplePrice = CommonGoodsUtil.getAfterUseCouplePrice(goods); + // 绗竴寮犲浘闇�瑕佹湁浠锋牸淇℃伅 + if (i == 0 || index) { + int topX = 50; + int topY = 50; + + // 鐢诲ぇ鍥� 璧峰鐐瑰潗鏍囦负锛�50,50锛� + int widht = 650; + int height = 650; + InputStream goodsPicture = TaoBaoHttpUtil + .getAsInputStream(goods.getPicture().replace("_.webp", "").replace("_220x220", "")); + if (goodsPicture == null) + continue; + + BufferedImage picImage = ImageIO.read(goodsPicture); + picImage = ImageUtil.zoomInImage(picImage, widht, height); + g2d.drawImage(picImage, topX, topX, null); + + // 鐢昏竟妗� + g2d.setColor(new Color(224, 224, 224)); + g2d.setStroke(new BasicStroke(1.0f)); + g2d.drawRect(topX - 1, topY - 1, widht + 1, widht + 1); + + // 鐢婚�忔槑鑳屾櫙 + g2d.setColor(new Color(255, 255, 255, 210)); + g2d.fillRect(10, height - 50, widht + 40, 100); + + // 鐢诲埜鍚庝环閲戦 + boldFont = boldFont.deriveFont(42.0f); + g2d.setFont(boldFont); + g2d.setColor(new Color(229, 0, 92)); + + g2d.drawString("楼", 70 + 10, height); + g2d.drawString(couplePrice + "", 70 + 45, height); + + // 鐢诲師浠� + g2d.setColor(new Color(102, 102, 102)); + font = font.deriveFont(24.0f); + g2d.setFont(font); + + String zkPrice = new BigDecimal((couplePrice + "").replace("楼", "")).add(goods.getCouponAmount()) + .toString(); + g2d.drawString("楼 " + zkPrice, 70 + 15, height + 30); + + // 鐢诲垹闄ょ嚎 + FontMetrics fm = g2d.getFontMetrics(font); + int textLength = fm.stringWidth(zkPrice); + g2d.setStroke(new BasicStroke(2.0f)); + g2d.drawLine(70 + 20, height + 21, 90 + 10 + textLength, height + 21); + + BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()); + if (withNoZera != null && withNoZera.compareTo(new BigDecimal(0)) > 0) { + g2d.setRenderingHints(mapH); + + // 鍒稿�煎緱闀垮害 + int size = withNoZera.toString().length() * 22; + int place = withNoZera.toString().length() * 22; + + // 鍒哥孩鑹插簳閮� + int quanBGWidht = widht - 100 - place; + int quanBGHeight = height - 33; + g2d.setColor(new Color(229, 0, 92)); + g2d.fillRoundRect(quanBGWidht, quanBGHeight, 130 + size, 70, 10, 10); + + // 鐢诲埜瀛� + boldFont = boldFont.deriveFont(40.0f); + g2d.setFont(boldFont); + g2d.setColor(new Color(255, 255, 255)); + g2d.drawString("鍒�", quanBGWidht + 8, quanBGHeight + 48); + + // 鍒哥櫧鑹插簳閮� + g2d.fillRoundRect(quanBGWidht + 55, quanBGHeight + 4, 70 + size, 62, 10, 10); + + // 鐢诲埜闈㈠�� + g2d.setFont(boldFont); + g2d.setColor(new Color(229, 0, 92)); + g2d.drawString("楼 " + withNoZera + "", quanBGWidht + 70, quanBGHeight + 48); + } + + index = false; + + } else { + // 璁$畻宸︿笂瑙掑潗鏍� + int topX = 0; + int topY = 0; + if (i == 1) + topX = 50 + (315 + 20) * 0; + else if (i == 2 || i % 2 != 0)// 2,3,5,7 + topX = 50 + (315 + 20) * (i % 2 + 1); + else if (i % 2 == 0)// 4,6,8 + topX = 50 + (315 + 20) * 3; + + if (i == 1 || i == 2) + topY = 50 + 650 + 20; + else if (i == 3 || i == 4) + topY = 50; + else if (i == 5 || i == 6) + topY = 50 + (315 + 20) * 1; + else if (i == 7 || i == 8) + topY = 50 + (315 + 20) * 2; + + // 鐢诲ぇ鍥� + InputStream goodsPicture = TaoBaoHttpUtil + .getAsInputStream(goods.getPicture().replace("_.webp", "")); + if (goodsPicture == null) + continue; + + BufferedImage picImage = ImageIO.read(goodsPicture); + picImage = ImageUtil.zoomInImage(picImage, 315, 315); + g2d.drawImage(picImage, topX, topY, null); + + g2d.setColor(new Color(224, 224, 224)); + // 鐢昏竟妗� + g2d.setStroke(new BasicStroke(1.0f)); + g2d.drawRect(topX - 1, topY - 1, 316, 316); + + // 鐢诲埜 + BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()); + if (withNoZera != null && withNoZera.compareTo(new BigDecimal(0)) > 0) { +// // 鐢婚�忔槑鑳屾櫙 +// g2d.setColor(new Color(255, 255, 255, 210)); +// g2d.fillRect(topX + 8, topY + 240, 300, 65); + + g2d.setRenderingHints(mapH); + + // 鍒稿�煎緱闀垮害 + int size = withNoZera.toString().length() * 22; + int place = withNoZera.toString().length() * 22; + +// // 鍒哥孩鑹插簳閮� + int quanBGWidht = topX + 170 - place; + int quanBGHeight = topY + 220; + g2d.setColor(new Color(229, 0, 92)); + g2d.fillRoundRect(quanBGWidht, quanBGHeight, 130 + size, 70, 10, 10); + + // 鐢诲埜瀛� + boldFont = boldFont.deriveFont(40.0f); + g2d.setFont(boldFont); + g2d.setColor(new Color(255, 255, 255)); + g2d.drawString("鍒�", quanBGWidht + 8, quanBGHeight + 48); + + // 鍒哥櫧鑹插簳閮� + g2d.fillRoundRect(quanBGWidht + 55, quanBGHeight + 4, 70 + size, 62, 10, 10); + + // 鐢诲埜闈㈠�� + g2d.setFont(boldFont); + g2d.setColor(new Color(229, 0, 92)); + g2d.drawString("楼 " + withNoZera + "", quanBGWidht + 70, quanBGHeight + 48); + } + } + } + + g2d.setColor(new Color(247, 247, 247)); + g2d.fillRect(50, 1070, 1320, 214); + + // 鐢讳簩缁寸爜 + + BufferedImage qrcodeImage = ImageIO.read(qrcodeStream); + qrcodeImage = ImageUtil.zoomInImage(qrcodeImage, 170, 170); + + g2d.drawImage(qrcodeImage, 70, 1092, null); + + // 鐢诲ご鍍� + if (portrait != null) { + BufferedImage portraitImg = ImageIO.read(portrait); + // 鏀剧缉澶у皬 + portraitImg = ImageUtil.zoomInImage(portraitImg, 40, 40); + // 鍦嗚 + portraitImg = ImageUtil.roundImage(portraitImg, 10); + + g2d.drawImage(portraitImg, 70 + (qrcodeImage.getWidth() - portraitImg.getWidth()) / 2, + 1092 + (qrcodeImage.getHeight() - portraitImg.getHeight()) / 2, null); + } + + font = font.deriveFont(50.0f); + g2d.setFont(font); + g2d.setColor(new Color(229, 0, 93)); + + g2d.setColor(new Color(102, 102, 102)); + g2d.drawString("闀挎寜璇嗗埆浜岀淮鐮佸厤璐归鍒�", 312, 1150); + + g2d.setColor(new Color(102, 102, 102)); + g2d.drawString("鍏�", 1000, 1150); + + g2d.setColor(new Color(229, 0, 93)); + g2d.drawString(goodsList.size() + "", 1055, 1150); + + g2d.setColor(new Color(102, 102, 102)); + g2d.drawString("涓晢鍝�", 1090, 1150); + + // 鎻愮ず璇� + BufferedImage tips = ImageIO + .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/share/tips1.png")); + tips = ImageUtil.zoomInImage(tips, 850, 65); + g2d.drawImage(tips, 312, 1190, null); + g2d.dispose(); + + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + ImageIO.write(targetImg, "JPEG", aos); + InputStream is = new ByteArrayInputStream(aos.toByteArray()); + return is; + + } catch (Exception e) { + try { + LogHelper.errorDetailInfo(e); + } catch (Exception e1) { + e1.printStackTrace(); + } + } return null; } @@ -994,7 +1661,7 @@ // 閭�璇峰ソ鍙嬪浘鐗� public static void inviteFriendImg(InputStream urlInputStream, InputStream portraitInputStream, - InputStream erCodeInputStream, String targetPath,String inviteCode) throws IOException { + InputStream erCodeInputStream, String targetPath, String inviteCode) throws IOException { inviteFriendImg(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, 260, 908, 230, inviteCode); } @@ -1004,14 +1671,10 @@ * @param portraitInputStream * @param erCodeInputStream * @param targetPath - * @param pX - * -浜岀淮鐮佺殑妯潗鏍� - * @param pY - * -浜岀淮鐮佺殑绾靛潗鏍� - * @param size - * -浜岀淮鐮佺殑灏哄 - * @param inviteCode - * -閭�璇风爜 + * @param pX -浜岀淮鐮佺殑妯潗鏍� + * @param pY -浜岀淮鐮佺殑绾靛潗鏍� + * @param size -浜岀淮鐮佺殑灏哄 + * @param inviteCode -閭�璇风爜 * @throws IOException */ // 閭�璇峰ソ鍙嬪浘鐗� @@ -1094,7 +1757,7 @@ return originalImage; } - static BufferedImage roundImage(BufferedImage srcImage, int cornerRadius) { // 鍗婂緞 + public static BufferedImage roundImage(BufferedImage srcImage, int cornerRadius) { // 鍗婂緞 int width = srcImage.getWidth(); int height = srcImage.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); @@ -1134,7 +1797,7 @@ return stateInt; } - static int getTextLengthByWidth(Graphics2D g2d, Font font, String content, int maxWidth, int startPos) { + public static int getTextLengthByWidth(Graphics2D g2d, Font font, String content, int maxWidth, int startPos) { FontMetrics fm = g2d.getFontMetrics(font); for (int i = startPos; i < content.length(); i++) { if (fm.stringWidth(content.substring(0, i)) >= maxWidth) @@ -1175,16 +1838,141 @@ return new int[] { width, height }; } - + /** + * 澶村儚鍔犵櫧搴曞浘 閭�璇峰ソ鍙嬪浘鐗� + * + * @param urlInputStream + * @param portraitInputStream + * @param erCodeInputStream + * @param targetPath + * @param inviteCode + * @throws IOException + */ + public static void inviteFriendImgWhite(InputStream urlInputStream, InputStream portraitInputStream, + InputStream erCodeInputStream, String targetPath, String inviteCode) throws IOException { + inviteFriendImgWhite(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, 260, 908, 230, + inviteCode); + } + + /** + * 澶村儚鍔犵櫧搴曞浘 閭�璇峰ソ鍙嬪浘鐗� -鑷畾涔変簩缁寸爜浣嶇疆 + * + * @param urlInputStream + * @param portraitInputStream + * @param erCodeInputStream + * @param targetPath + * @param inviteCode + * @throws IOException + */ + public static void inviteFriendImgWhitecustom(InputStream urlInputStream, InputStream portraitInputStream, + InputStream erCodeInputStream, String targetPath, Integer pX, Integer pY, Integer size, String inviteCode) + throws IOException { + inviteFriendImgWhite(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, pX, pY, size, + inviteCode); + } + + /** + * 閭�璇峰ソ鍙嬪浘鐗囧姞涓婂ご鍍忓姞涓婄櫧搴� + */ + public static void inviteFriendImgWhite(InputStream urlInputStream, InputStream portraitInputStream, + InputStream erCodeInputStream, String targetPath, Integer pX, Integer pY, Integer size, String inviteCode) + throws IOException { + + if (pX == null) { + pX = 260; + } + + if (pY == null) { + pY = 908; + } + + if (size == null) { + size = 230; + } + + BufferedImage bgImage = ImageIO.read(urlInputStream); + 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 qrCodeImage = ImageIO.read(erCodeInputStream); + qrCodeImage = ImageUtil.qrCodeImage(g2d, qrCodeImage, pX, pY, size, size); + + int portraitSize = size * 5 / 23; + int pPX = pX + size / 2 - portraitSize / 2; + int pPY = pY + size / 2 - portraitSize / 2; + + // 澶村儚鐧借壊杈规 + g2d.fillRoundRect(pPX - 4, pPY - 4, portraitSize + 8, portraitSize + 8, 5, 5); + g2d.setRenderingHints(mapH); + // 鐢� 澶村儚 + BufferedImage portraitImg = ImageIO.read(portraitInputStream); + portraitImg = ImageUtil.zoomInImage(portraitImg, portraitSize, portraitSize); + portraitImg = ImageUtil.roundImage(portraitImg, 10); + g2d.drawImage(portraitImg, pPX, pPY, portraitSize, portraitSize, null); + + // 鐢婚個璇风爜(閭�璇风爜鐨勫ぇ灏忎笌瀛椾綋鍥哄畾) + if (!StringUtil.isNullOrEmpty(inviteCode)) { + + int ax = pX - (inviteCode.length() - 4) * 13; + int ay = pY + size + 118; + + int width_diap = 235 + (int) (24.6 * (inviteCode.length() - 4)); + g2d.setColor(Color.white); + // 鐢诲嚭涓�涓煩褰� + g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.65f)); // 閫忔槑搴� + g2d.fillRect(ax, ay - 38, width_diap, 50); + // 鍓嶅崐鍦� + g2d.fillArc(ax - 25, ay - 38, 50, 50, 90, 180); + // 鍚庡崐鍦� + g2d.fillArc(ax + width_diap - 25, ay - 38, 50, 50, 270, 180); + + g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 1f)); + String st = "閭�璇风爜锛�" + inviteCode; + String os = System.getProperty("os.name"); + String fontBoldPath = "/usr/share/fonts/PingFang_Medium.ttf"; + if (os.toLowerCase().startsWith("win")) { + fontBoldPath = "C:/PingFang_Medium.ttf"; + } + + Font boldFont = null; + try { + boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); + } catch (FontFormatException e) { + e.printStackTrace(); + } + g2d.setFont(boldFont); + g2d.getFontMetrics(boldFont); + g2d.setColor(new Color(0, 0, 0)); + + g2d.setColor(new Color(0, 0, 0)); + g2d.drawString(st, ax, ay); + } + + OutputStream out = new FileOutputStream(new File(targetPath)); + ImageIO.write(targetImg, "JPEG", out); + out.flush(); + out.close(); + } + /** * 缁樺埗閭�璇烽棶鍊欏浘 - * @param bgPigInStream 鑳屾櫙鍥� - * @param portraitInStream 鐢ㄦ埛澶村儚 - * @param erCodeInStream 浜岀淮鐮� - * @param targetPath 璺緞 - * @param inviteCode 閭�璇风爜 - * @param content 鏂囧瓧鍐呭 - * @param date 鏄剧ず鏃ュ巻 + * + * @param bgPigInStream 鑳屾櫙鍥� + * @param portraitInStream 鐢ㄦ埛澶村儚 + * @param erCodeInStream 浜岀淮鐮� + * @param targetPath 璺緞 + * @param inviteCode 閭�璇风爜 + * @param content 鏂囧瓧鍐呭 + * @param date 鏄剧ず鏃ュ巻 * @throws IOException */ public static void drawInviteToGreet(InputStream bgPigInStream, InputStream portraitInStream, @@ -1194,7 +1982,7 @@ 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);// 鏂囧瓧鎶楅敮榻柯� @@ -1203,7 +1991,8 @@ final Graphics2D g2d = targetImg.createGraphics(); g2d.drawImage(bgImage, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null); g2d.setRenderingHints(mapH); - + g2d.setBackground(Color.WHITE); + // 鐢� 钂欏眰 BufferedImage mengCeng = ImageIO .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/invite/mc.png")); @@ -1217,22 +2006,29 @@ int size = 210; // 230 // 鐢� 浜岀淮鐮� BufferedImage qrCodeImage = ImageIO.read(erCodeInStream); - qrCodeImage = ImageUtil.qrCodeImage(g2d, qrCodeImage, pX, pY, size, size); // 浜岀淮鐮侀暱瀹� + qrCodeImage = 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);// 澶村儚闀垮 - // 鏂囧瓧鐧借壊 + // 澶村儚鐧借壊杈规 + g2d.fillRoundRect(pPX - 4, pPY - 4, portraitSize + 8, portraitSize + 8, 5, 5); + g2d.setRenderingHints(mapH); + + BufferedImage portraitImg = ImageIO.read(portraitInStream); + portraitImg = ImageUtil.zoomInImage(portraitImg, portraitSize, portraitSize); + portraitImg = ImageUtil.roundImage(portraitImg, 10); + g2d.drawImage(portraitImg, pPX, pPY, portraitSize, portraitSize, null); + + // 瀛椾綋鐧借壊 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"; + fontBoldPath = "C:/yahei.ttf"; } Font boldFont = null; @@ -1349,4 +2145,5 @@ out.flush(); out.close(); } + } -- Gitblit v1.8.0