yujian
2019-05-20 c4d752a1d340652d9c7d2abb5abff65cdee40d69
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();
   }
}