admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java
@@ -2484,4 +2484,95 @@
      return null;
   }
   public static InputStream drawActivityShareImg(InputStream banner, float bannerRate, String title, String desc,
         InputStream erCode) {
      // 计算高度
      int width = 750;
      int height = 116;
      int bannerWidth = 640;
      int bannerHeight = (int) (bannerWidth / bannerRate);
      height += bannerHeight;
      height += 524;
      try {
         final BufferedImage targetImg = new BufferedImage(width, height, 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.decode("#FF6940"));
         g2d.fillRect(0, 0, width, height);
         // 画logo
         g2d.drawImage(zoomInImage(
               ImageIO.read(ImageUtil.class.getClassLoader().getResource("image/activityShare/icon_logo.png")), 59,
               59), 346, 17, null);
         // 画banner背景
         g2d.setColor(Color.decode("#FFFFFF"));
         g2d.fillRect(30, 94, 692, bannerHeight + 22);
         // 画banner
         g2d.drawImage(roundImage( zoomInImage(ImageIO.read(banner), bannerWidth, bannerHeight),20), 55, 116, null);
         // 画二维码背景
         g2d.drawImage(
               ImageIO.read(
                     ImageUtil.class.getClassLoader().getResource("image/activityShare/qrcode_content_bg.png")),
               27, bannerHeight + 116, null);
         // 画二维码
         g2d.drawImage(zoomInImage(ImageIO.read(erCode), 230, 230), 260, 116 + bannerHeight + 171, null);
         // 画文字
         g2d.setColor(Color.decode("#F03143"));
         String fontBoldPath = "/usr/share/fonts/yahei_bold.ttf";
         String os = System.getProperty("os.name");
         if (os.toLowerCase().startsWith("win")) {
            fontBoldPath = "C:/Users/Administrator/Desktop/fonts/yahei_bold.ttf";
         }
         String fontPath = "/usr/share/fonts/yahei.ttf";
         if (os.toLowerCase().startsWith("win")) {
            fontPath = "C:/Users/Administrator/Desktop/fonts/yahei.ttf";
         }
         Font boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(48.0f);
         g2d.setFont(boldFont);
         FontMetrics fm = g2d.getFontMetrics(boldFont);
         int textLength = fm.stringWidth(title);
         g2d.drawString(title, (width - textLength) / 2, 116 + bannerHeight + 50 + 45);
         textLength = fm.stringWidth(desc);
         g2d.drawString(desc, (width - textLength) / 2, 116 + bannerHeight + 50 + 50 + 45);
         Font font = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(24.0f);
         g2d.setFont(font);
         g2d.setColor(Color.decode("#666666"));
         fm = g2d.getFontMetrics(font);
         textLength = fm.stringWidth("长按识别二维码 查看详情");
         g2d.drawString("长按识别二维码 查看详情", (width - textLength) / 2, 116 + bannerHeight + 428+10);
         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;
   }
}