yujian
2020-05-18 607ce2c2ae229176f4a8a4171088f73729ef4567
fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java
@@ -195,7 +195,8 @@
         // 二维码图框
         g2d.setColor(Color.WHITE);
         InputStream codeFrame = ImageUtil.class.getClassLoader().getResourceAsStream("image/share/qr_code_frame_xcx.png");
         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;
@@ -210,7 +211,8 @@
         }
         
         if (portraitStream == null) {
            portraitStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/official_default_head.jpg");
            portraitStream = ImageUtil.class.getClassLoader()
                  .getResourceAsStream("image/official_default_head.jpg");
         }
         BufferedImage portraitImg = ImageIO.read(portraitStream);
         BufferedImage zoomInImage = ImageUtil.zoomInImage(portraitImg, 84, 84);
@@ -284,7 +286,8 @@
         // 1、画商品主图
         InputStream picStream = HttpUtil.getAsInputStream(mainPic);
//         InputStream picStream  = ImageUtil.class.getClassLoader().getResourceAsStream("image/share/test_pic1.jpg");
         // InputStream picStream =
         // ImageUtil.class.getClassLoader().getResourceAsStream("image/share/test_pic1.jpg");
         g2d.drawImage(ImageIO.read(picStream), 0, 0, x, y, null);
         
         // 2、画来源图标
@@ -331,7 +334,6 @@
         } else {
            g2d.drawString(title.substring(0, length2), spacing, y + 45 + row * 36);
         }
         
         int baoYouLength = spacing;
         if (!goods.isHasCoupon()) {
@@ -386,7 +388,6 @@
            g2d.drawImage(ImageIO.read(ziying), baoYouLength, y + 150, 60, 30, null);
         }
         
         // 销量
         String sale = null;
         int salesType = goods.getSalesType();
@@ -408,7 +409,6 @@
         g2d.setColor(new Color(153, 153, 153));
         g2d.drawString(sale, spacing + 605 - textLength, y + 125);
         
         // 下单提示
         InputStream downTip = null;   
         if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
@@ -418,9 +418,9 @@
         }
         g2d.drawImage(ImageIO.read(downTip), spacing, y + 200, 382, 203, null);
         
         // 二维码图框
         InputStream codeFrame = ImageUtil.class.getClassLoader().getResourceAsStream("image/share/qr_code_frame01.png");
         InputStream codeFrame = ImageUtil.class.getClassLoader()
               .getResourceAsStream("image/share/qr_code_frame01.png");
         g2d.drawImage(ImageIO.read(codeFrame), spacing + 405, y + 200, 200, 203, null);
         
         // 画二维码
@@ -442,26 +442,32 @@
         portraitImg = ImageUtil.roundImage(portraitImg, 10);
         g2d.drawImage(portraitImg, pPX, pPY, portraitSize, portraitSize, null);
         
//         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         // g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
         // RenderingHints.VALUE_ANTIALIAS_ON);
//         g2d.setColor(new Color(255,231, 224));
//         g2d.fillRoundRect(spacing, y + 420, 607, 79, 10, 10);
//         
//         InputStream banliStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/banlikuaisheng.png");
//         g2d.drawImage(ImageIO.read(banliStream), spacing + 20, y + 440, 170, 38, null);
         // InputStream banliStream =
         // ImageUtil.class.getClassLoader().getResourceAsStream("image/banlikuaisheng.png");
         // g2d.drawImage(ImageIO.read(banliStream), spacing + 20, y + 440,
         // 170, 38, null);
//         
//         inviteCode = "邀请码:" + inviteCode;
//         Font font26d28 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(26.28f);
         // Font font26d28 = Font.createFont(Font.PLAIN, new
         // File(fontPath)).deriveFont(26.28f);
//         FontMetrics fmcode = g2d.getFontMetrics(font26d28);
//         int codeLength = fmcode.stringWidth(inviteCode);
//         // 邀请码背景
//         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         // g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
         // RenderingHints.VALUE_ANTIALIAS_ON);
//         g2d.setColor(new Color(229,71, 27));
//         g2d.fillRoundRect(spacing + (600 - (int)(codeLength * 1.1)), y + 440, 10 + codeLength, 37, 10, 10);
         // g2d.fillRoundRect(spacing + (600 - (int)(codeLength * 1.1)), y +
         // 440, 10 + codeLength, 37, 10, 10);
//         // 邀请码文字
//         g2d.setFont(font26d28);
//         g2d.setColor(new Color(255, 255, 255));
//         g2d.drawString(inviteCode, spacing + (600 - (int)(codeLength * 1.1) + 5), y + 468);
         // g2d.drawString(inviteCode, spacing + (600 - (int)(codeLength *
         // 1.1) + 5), y + 468);
         
         g2d.dispose();
@@ -2385,4 +2391,97 @@
      out.close();
   }
   public static InputStream drawAlipayImage(InputStream bg, String recieveTime, String money, String orderNo,
         String ttf) {
      try {
         String fontPath = "/usr/share/fonts/PingFang_Medium.ttf";
         String os = System.getProperty("os.name");
         if (os.toLowerCase().startsWith("win")) {
            fontPath = ttf;
         }
         String fontBoldPath = "/usr/share/fonts/PingFang_Heavy_0.ttf";
         if (os.toLowerCase().startsWith("win")) {
            fontBoldPath = "D:/PingFang_Heavy_0.ttf";
         }
         int px = 1080; // 图片宽度
         int py = 2340; // 图片高度
         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.decode("#333333"));
         g2d.fillRect(0, 0, px, py);
         BufferedImage bgImage = ImageIO.read(bg);
         g2d.drawImage(bgImage, null, 0, 0);
         Font font = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(87.0f);
         g2d.setFont(font);
         // 画金额
         FontMetrics fm = g2d.getFontMetrics(font);
         fm.stringWidth(money);
         g2d.drawString(money, (px - fm.stringWidth(money)) / 2, 359 + 65);
         // 画创建时间
         // 画订单
         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 drawSimpleImage(InputStream bg, int width, int height, InputStream imgStream,
         int imgWidth, int imgHeight, int px, int py) {
      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("#333333"));
         g2d.fillRect(0, 0, px, py);
         BufferedImage bgImage = ImageIO.read(bg);
         g2d.drawImage(bgImage, null, 0, 0);
         BufferedImage img = zoomInImage(ImageIO.read(imgStream), imgWidth, imgHeight);
         g2d.drawImage(img, px, py, 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;
   }
}