admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java
@@ -31,15 +31,19 @@
import org.yeshi.utils.DateLunarUtil;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.MoneyBigDecimalUtil;
import org.yeshi.utils.TimeUtil;
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.goods.facade.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
import com.yeshi.fanli.vo.user.PullNewRuleNumVO;
import com.yeshi.fanli.vo.user.PullNewRuleVO;
public class ImageUtil {
@@ -1416,7 +1420,7 @@
         // 画第一张
         for (int i = 0; i < goodsList.size(); i++) {
            TaoBaoGoodsBrief goods = goodsList.get(i);
            BigDecimal couplePrice = TaoBaoUtil.getAfterUseCouplePrice(goods);
            BigDecimal couplePrice = TaoBaoUtil.getCouponPrice(goods);
            // 第一张图需要有价格信息
            if (i == 0) {
               int topX = 50;
@@ -1954,7 +1958,7 @@
      g2d.drawImage(picImage, 50, 207, null);
      // 画价格
      BigDecimal money = TaoBaoUtil.getAfterUseCouplePrice(goods);
      BigDecimal money = TaoBaoUtil.getCouponPrice(goods);
      money = MoneyBigDecimalUtil.getWithNoZera(money);
      g2d.setColor(new Color(240, 66, 66));
@@ -3009,4 +3013,223 @@
      }
   }
   /**
    * 拉新海报
    * @param qrcode
    * @param portraitStream
    * @param code
    * @param rule
    * @return
    */
   public static InputStream drawPullNewPoster(InputStream qrcode, InputStream portraitStream, String code, PullNewRuleVO rule) {
      try {
         String fontPath = "/usr/share/fonts/yahei.ttf";
         String fontBoldPath = "/usr/share/fonts/yahei_bold.ttf";
         String os = System.getProperty("os.name");
         if (os.toLowerCase().startsWith("win")) {
            fontPath = "D:/yahei.ttf";
            fontBoldPath = "D:/yahei_bold.ttf";
         }
         Font font24 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(24.0f);
         Font font34 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(34.0f);
         Font boldFont24 = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(24.0f);
         int px = 750; // 图片宽度
         int topPicH = 529;
         int ruleheight = getRuleHeight(boldFont24, rule);
         int py = topPicH + ruleheight + +226 + 27 * 2;
         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(new Color(255, 54, 104));
         g2d.fillRect(0, 0, px, py);
         // 顶部图片
         InputStream headStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/invite/pull_new.png");
         g2d.drawImage(ImageIO.read(headStream), 0, 0, px, 529, null);
         // 规则白色背景 562
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2d.setColor(new Color(255, 255, 255));
         g2d.fillRoundRect(20, 529, 710, ruleheight, 50, 50);
         // 六月活动规则
         g2d.setFont(font34);
         g2d.setColor(new Color(51, 51, 51));
         g2d.drawString(rule.getRuleName(), 306 - 20, topPicH + 56);
         int y_line = topPicH + 86;
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2d.setColor(new Color(224, 224, 224));
         // 笔画的轮廓(画笔宽度/线宽为1px)
         BasicStroke bs1 = new BasicStroke(1);
         g2d.setStroke(bs1);
         g2d.drawLine(22, y_line, 728, y_line);
         // 右边距
         int spacing = 43 + 20;
         // 文字间距
         int fontH = 24;
         int spacingH = 14;
         int y_temp = topPicH + 110 + fontH;
         int fontNum = 10;
         int maxWidth = 590;
         // 新人奖励红色文字
         String newTitle = rule.getNewTitle();
         if (!StringUtil.isNullOrEmpty(newTitle)) {
            g2d.setFont(boldFont24);
            g2d.setColor(new Color(229, 0, 92));
            g2d.drawString(rule.getNewRule(), spacing, y_temp);
            g2d.setFont(font24);
            g2d.setColor(new Color(51, 51, 51));
            while (newTitle.length() > 0) {
               y_temp += spacingH + fontH;
               int lengthNew = ImageUtil.getTextLengthByWidth(g2d, font24, newTitle, maxWidth, fontNum);
               g2d.drawString(newTitle.substring(0, lengthNew), spacing, y_temp);
               newTitle = newTitle.substring(lengthNew);
            }
            y_temp = y_temp + fontH +  50;
         }
         g2d.setFont(boldFont24);
         g2d.setColor(new Color(229, 0, 92));
         g2d.drawString(rule.getInviterRule(), spacing, y_temp);
         g2d.setFont(font24);
         g2d.setColor(new Color(51, 51, 51));
         String inviterTitle = rule.getInviterTitle();
         while (inviterTitle.length() > 0) {
            y_temp += spacingH + fontH;
            int lengthNew = ImageUtil.getTextLengthByWidth(g2d, font24, inviterTitle, maxWidth, fontNum);
            g2d.drawString(inviterTitle.substring(0, lengthNew), spacing, y_temp);
            inviterTitle = inviterTitle.substring(lengthNew);
         }
         FontMetrics fm = g2d.getFontMetrics(font24);
         List<PullNewRuleNumVO> list = rule.getList();
         for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
               y_temp += fontH + 38;
            } else {
               y_temp += fontH + spacingH;
            }
            PullNewRuleNumVO numVO = list.get(i);
            int codeLength = fm.stringWidth(numVO.getRewardDesc());
            g2d.drawString(numVO.getNumRange(), 75, y_temp);
            g2d.drawString(numVO.getRewardDesc(), px - 78 - codeLength, y_temp);
         }
         // 底部内容
         y_temp += 27 + 27;
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2d.setColor(new Color(255, 255, 255));
         g2d.fillRoundRect(20, y_temp, 710, 226, 50, 50);
         InputStream bottomStream = ImageUtil.class.getClassLoader()
               .getResourceAsStream("image/invite/blks_ico.png");
         g2d.drawImage(ImageIO.read(bottomStream), 98, y_temp + 53, 315, 71, null);
         Font font26 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(26.28f);
         FontMetrics fmcode = g2d.getFontMetrics(font26);
         String inviteCode = "邀请码:" + code;
         int codeLength = fmcode.stringWidth(inviteCode);
         // 背景色
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2d.setColor(new Color(229, 0, 92));
         g2d.fillRoundRect(98 + 157 - codeLength / 2, y_temp + 138, codeLength + 10, 37, 10, 10);
         // 邀请码
         g2d.setColor(new Color(255, 255, 255));
         g2d.drawString(inviteCode, 98 + 157 - codeLength / 2 + 10, y_temp + 138 + 27);
         // 二维码图框
         InputStream codeFrame = ImageUtil.class.getClassLoader()
               .getResourceAsStream("image/invite/qr_code_frame.png");
         g2d.drawImage(ImageIO.read(codeFrame), 497 + 20, y_temp + 15, 200, 203, null);
         // 画二维码
         int codeSize = 175;
         int pX = 497 + 26;
         int pY = y_temp + 20;
         g2d.drawImage(ImageIO.read(qrcode), pX + 8, pY + 8, codeSize, codeSize, null);
         int portraitSize = 200 * 5 / 23 - 5;
         int pPX = pX + codeSize / 2 - portraitSize / 2 + 10;
         int pPY = pY + codeSize / 2 - portraitSize / 2;
         // 头像白色边框
         g2d.setColor(Color.WHITE);
         g2d.fillRoundRect(pPX - 2, pPY - 2, portraitSize + 4, portraitSize + 4, 5, 5);
         g2d.setRenderingHints(mapH);
         // 画头像
         BufferedImage portraitImg = ImageIO.read(portraitStream);
         portraitImg = ImageUtil.zoomInImage(portraitImg, portraitSize, portraitSize);
         portraitImg = ImageUtil.roundImage(portraitImg, 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) {
         LogHelper.errorDetailInfo(e);
      }
      return null;
   }
   private static int getRuleHeight(Font font24, PullNewRuleVO rule) {
      int px = 200; // 图片宽度
      int py = 200; // 图片高度
      BufferedImage targetImg = new BufferedImage(px, py, BufferedImage.TYPE_INT_RGB);
      final Graphics2D g2d = (Graphics2D) targetImg.getGraphics();
      // 文字间距
      int fontH = 24;
      int spacingH = 14;
      int height = 110 + fontH;
      int fontNum = 10;
      int maxWidth = 590;
      // 新人奖励规则
      String newTitle = rule.getNewTitle();
      if (!StringUtil.isNullOrEmpty(newTitle)) {
         while (newTitle.length() > 0) {
            height += spacingH + fontH;
            int lengthNew = ImageUtil.getTextLengthByWidth(g2d, font24, newTitle, maxWidth, fontNum);
            newTitle = newTitle.substring(lengthNew);
         }
         height = height + fontH+ 50;
      }
      // 邀请人奖励红字
      String inviterTitle = rule.getInviterTitle();
      while (inviterTitle.length() > 0) {
         height += spacingH + fontH;
         int lengthNew = ImageUtil.getTextLengthByWidth(g2d, font24, inviterTitle, maxWidth, fontNum);
         inviterTitle = inviterTitle.substring(lengthNew);
      }
      // 奖励方式
      List<PullNewRuleNumVO> list = rule.getList();
      for (int i = 0; i < list.size();i ++) {
         if (i == 0) {
            height += fontH + 38;
         } else {
            height += fontH + spacingH;
         }
      }
      // 底部空白
      height += 25;
      return height;
   }
}