admin
2018-12-10 5f01b1bf63bd3dd242fc29e012e15c37fc03a190
fanli/src/main/java/com/yeshi/fanli/util/ImageUtil.java
@@ -445,10 +445,10 @@
         font = font.deriveFont(50.0f);
         g2d.setFont(font);
         g2d.setColor(new Color(229, 0, 93));
         g2d.drawString("返利券提醒您", 312, 1118 + 40);
//         g2d.drawString("返利券提醒您", 312, 1118 + 40);
         g2d.setColor(new Color(102, 102, 102));
         g2d.drawString("长按识别二维码领取优惠券", 312, 1118 + 105);
         g2d.drawString("长按识别二维码领取优惠券", 312, 1118 + 77);
         g2d.dispose();
@@ -471,6 +471,257 @@
      return null;
   }
   /**
    * 绘制大的商品动态分享图
    *
    * @param qrcodeStream
    * @param portrait
    * @param goods
    * @return
    */
   public static InputStream drawGoodsShareBigImg(InputStream qrcodeStream, InputStream portrait,
         List<TaoBaoGoodsBrief> 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 {
         g2d.setColor(new Color(153, 153, 153));
         // 画第一张
         for (int i = 0; i < goodsList.size(); i++) {
            TaoBaoGoodsBrief goods = goodsList.get(i);
            BigDecimal couplePrice = TaoBaoUtil.getAfterUseCouplePrice(goods);
            // 第一张图需要有价格信息
            if (i == 0) {
               int topX = 50;
               int topY = 50;
               // 画大图 起始点坐标为(50,50)
               InputStream goodsPicture = TaoBaoHttpUtil
                     .getAsInputStream(goods.getPictUrl().replace("_.webp", "").replace("_220x220", ""));
               BufferedImage picImage = ImageIO.read(goodsPicture);
               picImage = zoomInImage(picImage, 650, 650);
               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, 651, 651);
               // 画透明背景
               g2d.setColor(new Color(255, 255, 255, 210));
               g2d.fillRect(50, 600, 650, 100);
               // 画金额
               g2d.setColor(new Color(240, 0, 102));
               boldFont = boldFont.deriveFont(22.0f);
               g2d.setFont(boldFont);
               g2d.drawString("¥", 70, 650);
               boldFont = boldFont.deriveFont(42.0f);
               g2d.setFont(boldFont);
               g2d.drawString(couplePrice+"", 90, 650);
               // 画原价
               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, 650 + 33);
               FontMetrics fm = g2d.getFontMetrics(font);
               int textLength = fm.stringWidth(zkPrice);
               // 画删除线
               g2d.setStroke(new BasicStroke(2.0f));
               g2d.drawLine(70 + 20, 674, 90 + 10 + textLength, 674);
               // 画券右侧
               BufferedImage quanRight = ImageIO
                     .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/fanli_quan_right.png"));
               quanRight = zoomInImage(quanRight, 12, 60);
               g2d.setColor(new Color(241, 66, 66));
               g2d.drawImage(quanRight, topX + 630 - quanRight.getWidth(), topY + 630 - quanRight.getHeight(),
                     null);
               // 画券的内容
               g2d.setColor(new Color(229, 0, 93));
               String quanString = " " + MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()) + "元券 ";
               font = font.deriveFont(36.0f);
               g2d.setFont(font);
               fm = g2d.getFontMetrics(font);
               textLength = fm.stringWidth(quanString);
               g2d.fillRect(topX + 630 - quanRight.getWidth() - textLength, topY + 630 - quanRight.getHeight(),
                     textLength, quanRight.getHeight());
               g2d.setColor(Color.WHITE);
               g2d.drawString(quanString, topX + 630 - quanRight.getWidth() - textLength,
                     topY + 630 + 42 - quanRight.getHeight());
               // 画券左侧
               BufferedImage quanLeft = ImageIO
                     .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/fanli_quan_left.png"));
               quanLeft = zoomInImage(quanLeft, 12, 60);
               g2d.drawImage(quanLeft, topX + 630 - quanRight.getWidth() - textLength - quanLeft.getWidth(),
                     topY + 630 - quanRight.getHeight(), null);
            } 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.getPictUrl().replace("_.webp", ""));
               BufferedImage picImage = ImageIO.read(goodsPicture);
               picImage = 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);
               // 画券
               // 画券右侧
               BufferedImage quanRight = ImageIO
                     .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/fanli_quan_right.png"));
               quanRight = zoomInImage(quanRight, 8, 40);
               g2d.setColor(new Color(241, 66, 66));
               g2d.drawImage(quanRight, topX + 315 - quanRight.getWidth(), topY + 315 - quanRight.getHeight(),
                     null);
               // 画券的内容
               g2d.setColor(new Color(229, 0, 93));
               String quanString = " " + MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()) + "元券 ";
               font = font.deriveFont(24.0f);
               g2d.setFont(font);
               FontMetrics fm = g2d.getFontMetrics(font);
               int textLength = fm.stringWidth(quanString);
               g2d.fillRect(topX + 315 - quanRight.getWidth() - textLength, topY + 315 - quanRight.getHeight(),
                     textLength, quanRight.getHeight());
               g2d.setColor(Color.WHITE);
               g2d.setFont(font);
               g2d.drawString(quanString, topX + 315 - quanRight.getWidth() - textLength,
                     topY + 315 + 28 - quanRight.getHeight());
               // 画券左侧
               BufferedImage quanLeft = ImageIO
                     .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/fanli_quan_left.png"));
               quanLeft = zoomInImage(quanLeft, 8, 40);
               g2d.drawImage(quanLeft, topX + 315 - quanRight.getWidth() - textLength - quanLeft.getWidth(),
                     topY + 315 - quanRight.getHeight(), null);
            }
         }
         g2d.setColor(new Color(247, 247, 247));
         g2d.fillRect(50, 1070, 1320, 214);
         // 画二维码
         BufferedImage qrcodeImage = ImageIO.read(qrcodeStream);
         qrcodeImage = zoomInImage(qrcodeImage, 170, 170);
         g2d.drawImage(qrcodeImage, 70, 1092, null);
         // 画头像
         if (portrait != null) {
            BufferedImage portraitImg = ImageIO.read(portrait);
            portraitImg = zoomInImage(portraitImg, 40, 40);
            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.drawString("返利券提醒您", 312, 1118 + 40);
         g2d.setColor(new Color(102, 102, 102));
         g2d.drawString("长按识别二维码领取优惠券", 312, 1118 + 77);
         g2d.dispose();
         // OutputStream out = new ByteArrayOutputStream(); new
         // FileOutputStream(new File(targetPath));
         // ImageIO.write(targetImg, "JPEG", out);
         // out.flush();
         // out.close();
         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;
   }
   // 画商品分享图
   public static InputStream drawGoodsShareImgHCJ(InputStream qrcodeStream, InputStream portrait,