| | |
| | | package com.yeshi.fanli.util;
|
| | |
|
| | | import java.awt.BasicStroke;
|
| | | import java.awt.Color;
|
| | | import java.awt.Font;
|
| | | import java.awt.FontFormatException;
|
| | | import java.awt.FontMetrics;
|
| | | import java.awt.Graphics;
|
| | | import java.awt.Graphics2D;
|
| | | import java.awt.RenderingHints;
|
| | | import java.awt.RenderingHints.Key;
|
| | | import java.awt.geom.RoundRectangle2D;
|
| | | import java.awt.image.BufferedImage;
|
| | | import java.io.ByteArrayInputStream;
|
| | | import java.io.ByteArrayOutputStream;
|
| | | import java.io.File;
|
| | | import java.io.FileInputStream;
|
| | | import java.io.FileOutputStream;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStream;
|
| | | import java.io.OutputStream;
|
| | | 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.goods.CommonGoods;
|
| | | import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
| | | import com.yeshi.fanli.log.LogHelper;
|
| | | import com.yeshi.fanli.util.taobao.TaoBaoUtil;
|
| | |
|
| | | public class ImageUtil {
|
| | |
|
| | | // 画商品分享图
|
| | | public static InputStream drawGoodsShareImg(InputStream qrcodeStream, InputStream portrait,
|
| | | TaoBaoGoodsBrief goods) {
|
| | |
|
| | | 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(720, 1280, 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, 720, 1280);
|
| | | 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();
|
| | | }
|
| | |
|
| | | g2d.setFont(font);
|
| | | try {
|
| | | g2d.setColor(new Color(153, 153, 153));
|
| | | // 画来源图标
|
| | | InputStream userTypeIcon = null;
|
| | | if (goods.getUserType() == 0)// 淘宝
|
| | | {
|
| | | userTypeIcon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tb.png");
|
| | | } else {
|
| | | userTypeIcon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tm.png");
|
| | | }
|
| | |
|
| | | g2d.drawImage(ImageIO.read(userTypeIcon), 50, 88, null);
|
| | |
|
| | | // 商品标题
|
| | | String title = goods.getTitle();
|
| | | int row = 0;
|
| | | int length = 0;
|
| | | // 画第一排
|
| | | length = getTextLengthByWidth(g2d, font, title, 500, 10);
|
| | | g2d.drawString(title.substring(0, length), 50 + 65, 112 + row * 40);
|
| | | title = title.substring(length);
|
| | | row++;
|
| | |
|
| | | // 判断是否画完,最多画2排
|
| | | while (title.length() > 0 && row < 2) {
|
| | | length = getTextLengthByWidth(g2d, font, title, 335, 10);
|
| | | g2d.drawString(title.substring(0, length), 50, 112 + row * 40);
|
| | | title = title.substring(length);
|
| | | row++;
|
| | | }
|
| | |
|
| | | // 画商品主图
|
| | | InputStream goodsPicture = TaoBaoHttpUtil.getAsInputStream(goods.getPictUrl());
|
| | | BufferedImage picImage = ImageIO.read(goodsPicture);
|
| | | picImage = zoomInImage(picImage, 620, 620);
|
| | | g2d.drawImage(picImage, 50, 190, null);
|
| | | Font boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(50.0f);
|
| | | // 画价格
|
| | | // 有券
|
| | | if (!StringUtil.isNullOrEmpty(goods.getCouponInfo())) {
|
| | | BigDecimal finalPrice = goods.getZkPrice();
|
| | | if (goods.getCouponStartFee().compareTo(goods.getZkPrice()) <= 0
|
| | | && goods.getZkPrice().compareTo(goods.getCouponAmount()) >= 0) {
|
| | | finalPrice = goods.getZkPrice().subtract(goods.getCouponAmount());
|
| | | }
|
| | |
|
| | | g2d.setColor(new Color(229, 0, 93));
|
| | | g2d.drawString("券后价 ¥", 57, 875);
|
| | | g2d.setColor(new Color(229, 0, 93));
|
| | | g2d.setFont(boldFont);
|
| | | g2d.drawString(finalPrice.toString(), 85 + 110, 875);
|
| | |
|
| | | // 画券右侧
|
| | | BufferedImage quanRight = ImageIO
|
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/fanli_quan_right.png"));
|
| | | g2d.setColor(new Color(241, 66, 66));
|
| | | g2d.drawImage(quanRight, 670 - quanRight.getWidth(), 852, null);
|
| | |
|
| | | // 画券的内容
|
| | | g2d.setColor(new Color(229, 0, 93));
|
| | |
|
| | | String quanString = " " + MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()) + "元券 ";
|
| | |
|
| | | boldFont = boldFont.deriveFont(44.0f);
|
| | | g2d.setFont(boldFont);
|
| | | FontMetrics fm = g2d.getFontMetrics(boldFont);
|
| | | int textLength = fm.stringWidth(quanString);
|
| | |
|
| | | g2d.fillRect(670 - quanRight.getWidth() - textLength, 852, textLength, 80);
|
| | |
|
| | | g2d.setColor(Color.WHITE);
|
| | |
|
| | | g2d.setFont(boldFont);
|
| | | g2d.drawString(quanString, 670 - quanRight.getWidth() - textLength, 927 - 19);
|
| | |
|
| | | // 画券左侧
|
| | | BufferedImage quanLeft = ImageIO
|
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/fanli_quan_left.png"));
|
| | | g2d.drawImage(quanLeft, 670 - quanRight.getWidth() - textLength - quanLeft.getWidth(), 852, null);
|
| | |
|
| | | String zkPriceName = "";
|
| | | if (goods.getUserType() == 0)
|
| | | zkPriceName = "淘宝价 ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice());
|
| | | else
|
| | | zkPriceName = "天猫价 ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice());
|
| | |
|
| | | g2d.setColor(new Color(153, 153, 153));
|
| | | font = font.deriveFont(30.0f);
|
| | | g2d.setFont(font);
|
| | | g2d.drawString(zkPriceName, 56, 930);
|
| | |
|
| | | } else// 无券
|
| | | {
|
| | | g2d.setColor(new Color(229, 0, 93));
|
| | | g2d.drawString("¥", 60, 870);
|
| | | g2d.setColor(new Color(229, 0, 93));
|
| | | BigDecimal finalPrice = goods.getZkPrice();
|
| | | g2d.setFont(boldFont);
|
| | | g2d.drawString(finalPrice.toString(), 85, 870);
|
| | | }
|
| | |
|
| | | g2d.setColor(new Color(247, 247, 247));
|
| | | g2d.fillRect(56, 1015, 608, 204);
|
| | |
|
| | | // 画二维码
|
| | |
|
| | | BufferedImage qrcodeImage = ImageIO.read(qrcodeStream);
|
| | | qrcodeImage = zoomInImage(qrcodeImage, 170, 170);
|
| | |
|
| | | g2d.drawImage(qrcodeImage, 56 + 17, 1015 + 17, null);
|
| | |
|
| | | // 画头像
|
| | | if (portrait != null) {
|
| | | BufferedImage portraitImg = ImageIO.read(portrait);
|
| | | portraitImg = zoomInImage(portraitImg, 40, 40);
|
| | | g2d.drawImage(portraitImg, 56 + 17 + (qrcodeImage.getWidth() - portraitImg.getWidth()) / 2,
|
| | | 1015 + 17 + (qrcodeImage.getHeight() - portraitImg.getHeight()) / 2, null);
|
| | | }
|
| | |
|
| | | font = font.deriveFont(30.0f);
|
| | | g2d.setFont(font);
|
| | | g2d.setColor(new Color(229, 0, 93));
|
| | |
|
| | | g2d.setColor(new Color(102, 102, 102));
|
| | | g2d.drawString("长按识别二维码领取优惠券", 250 + 17, 1130 + 17);
|
| | |
|
| | | 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;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 绘制大的商品动态分享图
|
| | | * |
| | | * @param qrcodeStream
|
| | | * @param portrait
|
| | | * @param goods
|
| | | * @return
|
| | | */
|
| | | public static InputStream drawActivityGoodsShareBigImg(InputStream qrcodeStream, InputStream portrait,
|
| | | List<RecommendActivityTaoBaoGoods> 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++) {
|
| | | RecommendActivityTaoBaoGoods goods = goodsList.get(i);
|
| | |
|
| | | // 第一张图需要有价格信息
|
| | | 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(goods.getQuanPrice(), 90, 650);
|
| | |
|
| | | // 画原价
|
| | | g2d.setColor(new Color(102, 102, 102));
|
| | | font = font.deriveFont(24.0f);
|
| | | g2d.setFont(font);
|
| | |
|
| | | String zkPrice = new BigDecimal(goods.getQuanPrice().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.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;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 绘制大的商品动态分享图
|
| | | * |
| | | * @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);
|
| | |
|
| | | String quanString = "";
|
| | | BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount());
|
| | |
|
| | | if (!withNoZera.toString().endsWith("0")) {
|
| | |
|
| | | // 画券右侧
|
| | | 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));
|
| | |
|
| | | quanString = " " + withNoZera + "元券 ";
|
| | |
|
| | | 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);
|
| | |
|
| | | // 画券
|
| | |
|
| | | String quanString = "";
|
| | | BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount());
|
| | |
|
| | | if (!withNoZera.toString().endsWith("0")) {
|
| | |
|
| | | // 画券右侧
|
| | | 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));
|
| | |
|
| | | quanString = " " + withNoZera + "元券 ";
|
| | |
|
| | | 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);
|
| | | // 圆角
|
| | | portraitImg = roundImage(portraitImg, 10);
|
| | |
|
| | | 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.setColor(new Color(102, 102, 102));
|
| | | g2d.drawString("长按识别二维码免费领券", 312, 1150);
|
| | |
|
| | | g2d.setColor(new Color(102, 102, 102));
|
| | | g2d.drawString("共", 1000, 1150);
|
| | |
|
| | | g2d.setColor(new Color(229, 0, 93));
|
| | | g2d.drawString(goodsList.size() + "", 1055, 1150);
|
| | |
|
| | | g2d.setColor(new Color(102, 102, 102));
|
| | | g2d.drawString("个商品", 1090, 1150);
|
| | |
|
| | | // 提示语
|
| | | BufferedImage tips = ImageIO
|
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/share/tips1.png"));
|
| | | tips = zoomInImage(tips, 850, 65);
|
| | | g2d.drawImage(tips, 312, 1190, null);
|
| | |
|
| | | g2d.dispose();
|
| | |
|
| | | 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;
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 绘制大的商品动态分享图
|
| | | * |
| | | * @param qrcodeStream
|
| | | * @param portrait
|
| | | * @param goods
|
| | | * @return
|
| | | */
|
| | | public static InputStream drawGoodsSharePicture(InputStream qrcodeStream, InputStream portrait,
|
| | | List<CommonGoods> 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 {
|
| | | // 画第一张
|
| | | for (int i = 0; i < goodsList.size(); i++) {
|
| | | CommonGoods goods = goodsList.get(i);
|
| | | BigDecimal couplePrice = CommonGoodsUtil.getAfterUseCouplePrice(goods);
|
| | | // 第一张图需要有价格信息
|
| | | if (i == 0) {
|
| | | int topX = 50;
|
| | | int topY = 50;
|
| | | |
| | | |
| | | // 画大图 起始点坐标为(50,50)
|
| | | int widht = 650;
|
| | | int height = 650;
|
| | | InputStream goodsPicture = TaoBaoHttpUtil.getAsInputStream(goods.getPicture().replace("_.webp", "").replace("_220x220", ""));
|
| | | BufferedImage picImage = ImageIO.read(goodsPicture);
|
| | | picImage = ImageUtil.zoomInImage(picImage, widht, height);
|
| | | 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, widht + 1, widht + 1);
|
| | | |
| | | // 画透明背景
|
| | | g2d.setColor(new Color(255, 255, 255, 210));
|
| | | g2d.fillRect(10, height - 50, widht + 40, 100);
|
| | | |
| | | |
| | | // 画券后价金额
|
| | | boldFont = boldFont.deriveFont(42.0f);
|
| | | g2d.setFont(boldFont);
|
| | | g2d.setColor(new Color(229, 0, 92));
|
| | | |
| | | g2d.drawString("¥", 70 + 10, height);
|
| | | g2d.drawString(couplePrice + "", 70 + 45, height);
|
| | |
|
| | | // 画原价
|
| | | 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 + 15, height + 30);
|
| | |
|
| | | // 画删除线
|
| | | FontMetrics fm = g2d.getFontMetrics(font);
|
| | | int textLength = fm.stringWidth(zkPrice);
|
| | | g2d.setStroke(new BasicStroke(2.0f));
|
| | | g2d.drawLine(70 + 20, height + 21, 90 + 10 + textLength, height + 21);
|
| | |
|
| | | BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount());
|
| | | if (withNoZera != null && withNoZera.compareTo(new BigDecimal(0)) > 0) {
|
| | | g2d.setRenderingHints(mapH);
|
| | | |
| | | // 券值得长度
|
| | | int size = withNoZera.toString().length() * 22;
|
| | | int place = withNoZera.toString().length() * 22;
|
| | | |
| | | // 券红色底部
|
| | | int quanBGWidht = widht-100 - place;
|
| | | int quanBGHeight = height - 33;
|
| | | g2d.setColor(new Color(229, 0, 92));
|
| | | g2d.fillRoundRect(quanBGWidht, quanBGHeight, 130 + size, 70, 10, 10);
|
| | | |
| | | // 画券字
|
| | | boldFont = boldFont.deriveFont(40.0f);
|
| | | g2d.setFont(boldFont);
|
| | | g2d.setColor(new Color(255, 255, 255));
|
| | | g2d.drawString("券",quanBGWidht + 8, quanBGHeight + 48);
|
| | | |
| | | // 券白色底部
|
| | | g2d.fillRoundRect(quanBGWidht + 55, quanBGHeight + 4 , 70 + size, 62, 10, 10);
|
| | | |
| | | // 画券面值
|
| | | g2d.setFont(boldFont);
|
| | | g2d.setColor(new Color(229, 0, 92));
|
| | | g2d.drawString("¥ " + withNoZera + "", quanBGWidht + 70, quanBGHeight + 48);
|
| | | }
|
| | |
|
| | | } 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.getPicture().replace("_.webp", ""));
|
| | | BufferedImage picImage = ImageIO.read(goodsPicture);
|
| | | picImage = ImageUtil.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);
|
| | |
|
| | | // 画券
|
| | | BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount());
|
| | | if (withNoZera != null && withNoZera.compareTo(new BigDecimal(0)) > 0) {
|
| | | // // 画透明背景
|
| | | // g2d.setColor(new Color(255, 255, 255, 210));
|
| | | // g2d.fillRect(topX + 8, topY + 240, 300, 65);
|
| | | |
| | | g2d.setRenderingHints(mapH);
|
| | | |
| | | // 券值得长度
|
| | | int size = withNoZera.toString().length() * 22;
|
| | | int place = withNoZera.toString().length() * 22;
|
| | | |
| | | // // 券红色底部
|
| | | int quanBGWidht = topX + 170 - place;
|
| | | int quanBGHeight = topY + 220;
|
| | | g2d.setColor(new Color(229, 0, 92));
|
| | | g2d.fillRoundRect(quanBGWidht, quanBGHeight, 130 + size, 70, 10, 10);
|
| | | |
| | | // 画券字
|
| | | boldFont = boldFont.deriveFont(40.0f);
|
| | | g2d.setFont(boldFont);
|
| | | g2d.setColor(new Color(255, 255, 255));
|
| | | g2d.drawString("券",quanBGWidht + 8, quanBGHeight + 48);
|
| | | |
| | | // 券白色底部
|
| | | g2d.fillRoundRect(quanBGWidht + 55, quanBGHeight + 4 , 70 + size, 62, 10, 10);
|
| | | |
| | | // 画券面值
|
| | | g2d.setFont(boldFont);
|
| | | g2d.setColor(new Color(229, 0, 92));
|
| | | g2d.drawString("¥ " + withNoZera + "", quanBGWidht + 70, quanBGHeight + 48);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | g2d.setColor(new Color(247, 247, 247));
|
| | | g2d.fillRect(50, 1070, 1320, 214);
|
| | |
|
| | | // 画二维码
|
| | |
|
| | | BufferedImage qrcodeImage = ImageIO.read(qrcodeStream);
|
| | | qrcodeImage = ImageUtil.zoomInImage(qrcodeImage, 170, 170);
|
| | |
|
| | | g2d.drawImage(qrcodeImage, 70, 1092, null);
|
| | |
|
| | | // 画头像
|
| | | if (portrait != null) {
|
| | | BufferedImage portraitImg = ImageIO.read(portrait);
|
| | | // 放缩大小
|
| | | portraitImg = ImageUtil.zoomInImage(portraitImg, 40, 40);
|
| | | // 圆角
|
| | | portraitImg = ImageUtil.roundImage(portraitImg, 10);
|
| | |
|
| | | 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.setColor(new Color(102, 102, 102));
|
| | | g2d.drawString("长按识别二维码免费领券", 312, 1150);
|
| | |
|
| | | g2d.setColor(new Color(102, 102, 102));
|
| | | g2d.drawString("共", 1000, 1150);
|
| | |
|
| | | g2d.setColor(new Color(229, 0, 93));
|
| | | g2d.drawString(goodsList.size() + "", 1055, 1150);
|
| | |
|
| | | g2d.setColor(new Color(102, 102, 102));
|
| | | g2d.drawString("个商品", 1090, 1150);
|
| | |
|
| | | // 提示语
|
| | | BufferedImage tips = ImageIO
|
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/share/tips1.png"));
|
| | | tips = ImageUtil.zoomInImage(tips, 850, 65);
|
| | | g2d.drawImage(tips, 312, 1190, null);
|
| | | g2d.dispose();
|
| | |
|
| | | 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,
|
| | | TaoBaoGoodsBrief goods) throws Exception {
|
| | |
|
| | | 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";
|
| | | }
|
| | |
|
| | | Font font = null;
|
| | | Font boldFont = null;
|
| | |
|
| | | font = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(28.0f);
|
| | |
|
| | | boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(28.0f);
|
| | |
|
| | | final BufferedImage targetImg = new BufferedImage(720, 1280, 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, 720, 1280);
|
| | |
|
| | | g2d.setFont(font.deriveFont(28.0f));
|
| | |
|
| | | // 画广告语
|
| | | InputStream adTitle = ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_share_title_icon.png");
|
| | | if (adTitle != null) {
|
| | | BufferedImage adTitleImage = ImageIO.read(adTitle);
|
| | | g2d.drawImage(adTitleImage, 192, 101, null);
|
| | | }
|
| | |
|
| | | // 画商品主图
|
| | | InputStream goodsPicture = TaoBaoHttpUtil.getAsInputStream(goods.getPictUrl().replace("https://", "http://"));
|
| | | BufferedImage picImage = ImageIO.read(goodsPicture);
|
| | | picImage = zoomInImage(picImage, 620, 620);
|
| | | g2d.drawImage(picImage, 50, 207, null);
|
| | |
|
| | | // 画价格
|
| | | BigDecimal money = TaoBaoUtil.getAfterUseCouplePrice(goods);
|
| | | money = MoneyBigDecimalUtil.getWithNoZera(money);
|
| | | g2d.setColor(new Color(240, 66, 66));
|
| | |
|
| | | g2d.setFont(boldFont.deriveFont(30.0f));
|
| | | g2d.drawString("¥", 62, 900);
|
| | |
|
| | | g2d.setFont(boldFont.deriveFont(56.0f));
|
| | | g2d.drawString(money.toString(), 90, 900);
|
| | |
|
| | | g2d.setFont(font.deriveFont(56.0f));
|
| | | g2d.setColor(new Color(153, 153, 153));
|
| | |
|
| | | if (!StringUtil.isNullOrEmpty(goods.getCouponInfo())) {
|
| | | // 画天猫价或者淘宝价
|
| | | String zkPriceName = "";
|
| | | if (goods.getUserType() == 0)
|
| | | zkPriceName = "淘宝价 ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice());
|
| | | else
|
| | | zkPriceName = "天猫价 ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice());
|
| | |
|
| | | font = font.deriveFont(30.0f);
|
| | | g2d.setFont(font);
|
| | | g2d.drawString(zkPriceName, 56, 955);
|
| | |
|
| | | // 画券右侧
|
| | | BufferedImage quanRight = ImageIO
|
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_quan_right.png"));
|
| | | g2d.setColor(new Color(241, 66, 66));
|
| | | g2d.drawImage(quanRight, 670 - quanRight.getWidth(), 867, null);
|
| | |
|
| | | // 画券的内容
|
| | | g2d.setColor(new Color(241, 66, 66));
|
| | |
|
| | | String quanString = " ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()) + " 券 ";
|
| | |
|
| | | font = font.deriveFont(44.0f);
|
| | | g2d.setFont(font);
|
| | | FontMetrics fm = g2d.getFontMetrics(font);
|
| | | int textLength = fm.stringWidth(quanString);
|
| | |
|
| | | g2d.fillRect(670 - quanRight.getWidth() - textLength, 872, textLength, 80);
|
| | |
|
| | | g2d.setColor(Color.WHITE);
|
| | |
|
| | | g2d.setFont(font);
|
| | | g2d.drawString(quanString, 670 - quanRight.getWidth() - textLength, 927);
|
| | |
|
| | | // 画券左侧
|
| | | BufferedImage quanLeft = ImageIO
|
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_quan_left.png"));
|
| | | g2d.drawImage(quanLeft, 670 - quanRight.getWidth() - textLength - quanLeft.getWidth(), 867, null);
|
| | | }
|
| | | InputStream userTypeIcon;
|
| | | if (goods.getUserType() == 0)// 淘宝
|
| | | {
|
| | | userTypeIcon = ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_icon_tb.png");
|
| | | } else {
|
| | | userTypeIcon = ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_icon_tm.png");
|
| | | }
|
| | |
|
| | | g2d.drawImage(ImageIO.read(userTypeIcon), 63, 1022, null);
|
| | |
|
| | | String title = goods.getTitle();
|
| | | // 商品标题
|
| | | font = font.deriveFont(30.0f);
|
| | | g2d.setFont(font);
|
| | |
|
| | | int length = getTextLengthByWidth(g2d, font, title, 290, 8);
|
| | | g2d.setColor(new Color(153, 153, 153));
|
| | |
|
| | | int row = 0;
|
| | | // 画第一排
|
| | | g2d.drawString(title.substring(0, length), 20 + 50 + 46, 1044 + row * 40);
|
| | |
|
| | | if (length < title.length())
|
| | | title = title.substring(length);
|
| | | else
|
| | | title = "";
|
| | | // 判断是否画完
|
| | | row++;
|
| | | while (title.length() > 0) {
|
| | | length = getTextLengthByWidth(g2d, font, title, 330, 10);
|
| | | g2d.drawString(title.substring(0, length), 63, 1044 + row * 40);
|
| | | title = title.substring(length);
|
| | | row++;
|
| | | }
|
| | |
|
| | | // 画边框
|
| | | InputStream erCodeSide = ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_ercode_side.png");
|
| | | BufferedImage erCodeSideImage = ImageIO.read(erCodeSide);
|
| | | erCodeSideImage = zoomInImage(erCodeSideImage, 200, 200);
|
| | | g2d.drawImage(erCodeSideImage, 466, 986, null);
|
| | |
|
| | | // 画二维码
|
| | |
|
| | | BufferedImage qrcodeImage = ImageIO.read(qrcodeStream);
|
| | | qrcodeImage = zoomInImage(qrcodeImage, 170, 170);
|
| | |
|
| | | g2d.drawImage(qrcodeImage, 481, 1000, null);
|
| | |
|
| | | // 画头像
|
| | | if (portrait != null) {
|
| | | BufferedImage portraitImg = ImageIO.read(portrait);
|
| | | portraitImg = zoomInImage(portraitImg, 30, 30);
|
| | | g2d.drawImage(portraitImg, 481 + (qrcodeImage.getWidth() - portraitImg.getWidth()) / 2,
|
| | | 1000 + (qrcodeImage.getHeight() - portraitImg.getHeight()) / 2, null);
|
| | | }
|
| | |
|
| | | font = font.deriveFont(22.0f);
|
| | | g2d.setFont(font);
|
| | | g2d.setColor(new Color(241, 66, 66));
|
| | |
|
| | | FontMetrics fm = g2d.getFontMetrics(font);
|
| | | int textLength = fm.stringWidth("海草街提醒您");
|
| | | g2d.drawString("海草街提醒您", 481 + (qrcodeImage.getWidth() - textLength) / 2, 1220);
|
| | |
|
| | | g2d.setColor(new Color(102, 102, 102));
|
| | |
|
| | | fm = g2d.getFontMetrics(font);
|
| | | textLength = fm.stringWidth("长按识别二维码");
|
| | | g2d.drawString("长按识别二维码", 481 + (qrcodeImage.getWidth() - textLength) / 2, 1255);
|
| | |
|
| | | g2d.dispose();
|
| | |
|
| | | ByteArrayOutputStream aos = new ByteArrayOutputStream();
|
| | | ImageIO.write(targetImg, "JPEG", aos);
|
| | | InputStream is = new ByteArrayInputStream(aos.toByteArray());
|
| | | return is;
|
| | | }
|
| | |
|
| | | private static int[] computeCropPosition(BufferedImage source, int width, int height) {
|
| | | int[] cropParams = new int[4];
|
| | | int owidth = source.getWidth();
|
| | | int oheight = source.getHeight();
|
| | | if (oheight * width - owidth * height > 0) {
|
| | | cropParams[0] = 0;
|
| | | cropParams[1] = (oheight - height) / 2;
|
| | | cropParams[2] = owidth;
|
| | | cropParams[3] = cropParams[2] * height / width;
|
| | | } else {
|
| | | cropParams[0] = (owidth - width) / 2;
|
| | | cropParams[1] = 0;
|
| | | cropParams[3] = oheight;
|
| | | cropParams[2] = cropParams[3] * width / height;
|
| | | }
|
| | |
|
| | | return cropParams;
|
| | | }
|
| | |
|
| | | public static BufferedImage crop(BufferedImage source, int startX, int startY, int w, int h) {
|
| | | int width = source.getWidth();
|
| | | int height = source.getHeight();
|
| | |
|
| | | if (startX <= -1) {
|
| | | startX = 0;
|
| | | }
|
| | | if (startY <= -1) {
|
| | | startY = 0;
|
| | | }
|
| | | if (w <= -1) {
|
| | | w = width - 1;
|
| | | }
|
| | | if (h <= -1) {
|
| | | h = height - 1;
|
| | | }
|
| | | BufferedImage result = new BufferedImage(w, h, source.getType());
|
| | | for (int y = startY; y < h + startY; y++) {
|
| | | for (int x = startX; x < w + startX; x++) {
|
| | | int rgb = source.getRGB(x, y);
|
| | | result.setRGB(x - startX, y - startY, rgb);
|
| | | }
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | public static BufferedImage zoomInImage(BufferedImage originalImage, int width, int height) {
|
| | | int type = originalImage.getType();
|
| | | if (type == 0)
|
| | | type = 5;
|
| | | BufferedImage newImage = new BufferedImage(width, height, type);
|
| | | Graphics g = newImage.getGraphics();
|
| | | g.drawImage(originalImage, 0, 0, width, height, null);
|
| | | g.dispose();
|
| | | return newImage;
|
| | | }
|
| | |
|
| | | // 邀请好友图片
|
| | | public static void inviteFriendImg(InputStream urlInputStream, InputStream portraitInputStream,
|
| | | InputStream erCodeInputStream, String targetPath,String inviteCode) throws IOException {
|
| | | inviteFriendImg(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, 260, 908, 230, inviteCode);
|
| | | }
|
| | |
|
| | | /**
|
| | | * |
| | | * @param urlInputStream
|
| | | * @param portraitInputStream
|
| | | * @param erCodeInputStream
|
| | | * @param targetPath
|
| | | * @param pX
|
| | | * -二维码的横坐标
|
| | | * @param pY
|
| | | * -二维码的纵坐标
|
| | | * @param size
|
| | | * -二维码的尺寸
|
| | | * @param inviteCode
|
| | | * -邀请码
|
| | | * @throws IOException
|
| | | */
|
| | | // 邀请好友图片
|
| | | public static void inviteFriendImg(InputStream urlInputStream, InputStream portraitInputStream,
|
| | | InputStream erCodeInputStream, String targetPath, int pX, int pY, int size, String inviteCode)
|
| | | throws IOException {
|
| | | BufferedImage bgImage = ImageIO.read(urlInputStream);
|
| | | 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);
|
| | |
|
| | | // 画 二维码
|
| | | BufferedImage qrCodeImage = ImageIO.read(erCodeInputStream);
|
| | | qrCodeImage = ImageUtil.qrCodeImage(g2d, qrCodeImage, pX, pY, size, size); // 二维码长宽
|
| | | // 230*230
|
| | |
|
| | | // 画 头像
|
| | | BufferedImage portraitImg = ImageIO.read(portraitInputStream);
|
| | | int portraitSize = size * 5 / 23;
|
| | |
|
| | | int pPX = pX + size / 2 - portraitSize / 2;
|
| | | int pPY = pY + size / 2 - portraitSize / 2;
|
| | | portraitImg = ImageUtil.portraitImg(g2d, portraitImg, pPX, pPY, portraitSize, portraitSize);// 头像长宽
|
| | |
|
| | | // 画邀请码(邀请码的大小与字体固定)
|
| | | if (!StringUtil.isNullOrEmpty(inviteCode)) {
|
| | | String st = "邀请码:" + inviteCode;
|
| | | int width = bgImage.getWidth();
|
| | | String os = System.getProperty("os.name");
|
| | | String fontBoldPath = "/usr/share/fonts/PingFang_Medium.ttf";
|
| | | if (os.toLowerCase().startsWith("win")) {
|
| | | fontBoldPath = "D:/PingFang_Medium.ttf";
|
| | | }
|
| | |
|
| | | Font boldFont = null;
|
| | | try {
|
| | | boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f);
|
| | | } catch (FontFormatException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | g2d.setFont(boldFont);
|
| | | g2d.setColor(new Color(0, 0, 0));
|
| | |
|
| | | FontMetrics fm = g2d.getFontMetrics(boldFont);
|
| | | int textLength = fm.stringWidth(st);
|
| | |
|
| | | int ax = (width - textLength) / 2;
|
| | | int ay = bgImage.getHeight() - 92 + boldFont.getSize() / 2 - 5;
|
| | |
|
| | | g2d.setColor(new Color(0, 0, 0));
|
| | | g2d.drawString(st, ax, ay);
|
| | |
|
| | | }
|
| | |
|
| | | OutputStream out = new FileOutputStream(new File(targetPath));
|
| | | ImageIO.write(targetImg, "JPEG", out);
|
| | | out.flush();
|
| | | out.close();
|
| | | }
|
| | |
|
| | | // 二维码
|
| | | public static BufferedImage qrCodeImage(Graphics2D g2d, BufferedImage originalImage, int pX, int pY, int width,
|
| | | int height) {
|
| | | g2d.drawImage(originalImage, pX, pY, width, height, null);
|
| | | return originalImage;
|
| | | }
|
| | |
|
| | | // 头像
|
| | | public static BufferedImage portraitImg(Graphics2D g2d, BufferedImage originalImage, int pX, int pY, int width,
|
| | | int height) {
|
| | | g2d.drawImage(originalImage, pX, pY, width, height, null);
|
| | | return originalImage;
|
| | | }
|
| | |
|
| | | public static BufferedImage roundImage(BufferedImage srcImage, int cornerRadius) { // 半径
|
| | | int width = srcImage.getWidth();
|
| | | int height = srcImage.getHeight();
|
| | | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
| | | Graphics2D gs = image.createGraphics();
|
| | | 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);// 文字抗锯齿
|
| | | gs.setRenderingHints(mapH);
|
| | | gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, cornerRadius, cornerRadius));
|
| | | gs.drawImage(srcImage, 0, 0, null);
|
| | | gs.dispose();
|
| | | return image;
|
| | | }
|
| | |
|
| | | public static int saveToImgByInputStream(InputStream inputStream, String imgPath, String imgName) {
|
| | | int stateInt = 1;
|
| | | try {
|
| | | File file = new File(imgPath, imgName);// 可以是任何图片格式.jpg,.png等
|
| | | FileOutputStream fos = new FileOutputStream(file);
|
| | |
|
| | | FileInputStream fis = (FileInputStream) inputStream;
|
| | |
|
| | | byte[] b = new byte[1024];
|
| | | int nRead = 0;
|
| | | while ((nRead = fis.read(b)) != -1) {
|
| | | fos.write(b, 0, nRead);
|
| | | }
|
| | | fos.flush();
|
| | | fos.close();
|
| | | fis.close();
|
| | |
|
| | | } catch (Exception e) {
|
| | | stateInt = 0;
|
| | | e.printStackTrace();
|
| | | } finally {
|
| | | }
|
| | | return stateInt;
|
| | | }
|
| | |
|
| | | static int getTextLengthByWidth(Graphics2D g2d, Font font, String content, int maxWidth, int startPos) {
|
| | | FontMetrics fm = g2d.getFontMetrics(font);
|
| | | for (int i = startPos; i < content.length(); i++) {
|
| | | if (fm.stringWidth(content.substring(0, i)) >= maxWidth)
|
| | | return i + 1;
|
| | | }
|
| | | return content.length();
|
| | | }
|
| | |
|
| | | public static int[] getImgWidthAndHeight(String imgUrl) throws MalformedURLException, IOException {
|
| | | if (StringUtil.isNullOrEmpty(imgUrl))
|
| | | return new int[] { 0, 0 };
|
| | | if (imgUrl.toLowerCase().endsWith("webp"))
|
| | | return getImgWidthAndHeightWithWebp(imgUrl);
|
| | | else
|
| | | return getImgWidthAndHeightWithPngAndJpg(imgUrl);
|
| | | }
|
| | |
|
| | | public static int[] getImgWidthAndHeightWithPngAndJpg(String imgUrl) throws MalformedURLException, IOException {
|
| | | InputStream murl = new URL(imgUrl).openStream();
|
| | | BufferedImage sourceImg = ImageIO.read(murl);
|
| | |
|
| | | int width = sourceImg.getWidth();
|
| | | int height = sourceImg.getHeight();
|
| | | return new int[] { width, height };
|
| | | }
|
| | |
|
| | | public static int[] getImgWidthAndHeightWithWebp(String imgUrl) throws MalformedURLException, IOException {
|
| | | String cacheFile = FileUtil.getCacheDir();
|
| | | String targetPath = cacheFile + "/CACHE_DOWNLOAD_IMG_" + System.currentTimeMillis() + "_"
|
| | | + (long) (Math.random() * 1000000000L);
|
| | | HttpUtil.downloadFile(imgUrl, targetPath);
|
| | | FileInputStream file = new FileInputStream(targetPath);
|
| | | byte[] bytes = new byte[30];
|
| | | file.read(bytes, 0, bytes.length);
|
| | | int width = ((int) bytes[27] & 0xff) << 8 | ((int) bytes[26] & 0xff);
|
| | | int height = ((int) bytes[29] & 0xff) << 8 | ((int) bytes[28] & 0xff);
|
| | | file.close();
|
| | | return new int[] { width, height };
|
| | | }
|
| | |
|
| | | |
| | | |
| | | /**
|
| | | * 头像加白底图 邀请好友图片
|
| | | * @param urlInputStream
|
| | | * @param portraitInputStream
|
| | | * @param erCodeInputStream
|
| | | * @param targetPath
|
| | | * @param inviteCode
|
| | | * @throws IOException
|
| | | */
|
| | | public static void inviteFriendImgWhite(InputStream urlInputStream, InputStream portraitInputStream,
|
| | | InputStream erCodeInputStream, String targetPath,String inviteCode) throws IOException {
|
| | | inviteFriendImgWhite(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, 260, 908, 230, inviteCode);
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 头像加白底图 邀请好友图片 -自定义二维码位置
|
| | | * @param urlInputStream
|
| | | * @param portraitInputStream
|
| | | * @param erCodeInputStream
|
| | | * @param targetPath
|
| | | * @param inviteCode
|
| | | * @throws IOException
|
| | | */
|
| | | public static void inviteFriendImgWhitecustom(InputStream urlInputStream, InputStream portraitInputStream,
|
| | | InputStream erCodeInputStream, String targetPath, Integer pX, Integer pY, Integer size, String inviteCode) throws IOException {
|
| | | inviteFriendImgWhite(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, pX, pY, size, inviteCode);
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 邀请好友图片加上头像加上白底
|
| | | */
|
| | | public static void inviteFriendImgWhite(InputStream urlInputStream, InputStream portraitInputStream,
|
| | | InputStream erCodeInputStream, String targetPath, Integer pX, Integer pY, Integer size, String inviteCode) throws IOException {
|
| | | |
| | | if (pX == null) {
|
| | | pX = 260;
|
| | | }
|
| | | |
| | | if (pY == null) {
|
| | | pY = 908; |
| | | }
|
| | | |
| | | if (size == null) {
|
| | | size = 230;
|
| | | }
|
| | |
|
| | | |
| | | BufferedImage bgImage = ImageIO.read(urlInputStream);
|
| | | 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 qrCodeImage = ImageIO.read(erCodeInputStream);
|
| | | qrCodeImage = ImageUtil.qrCodeImage(g2d, qrCodeImage, pX, pY, size, size);
|
| | | |
| | | int portraitSize = size * 5 / 23;
|
| | | int pPX = pX + size / 2 - portraitSize / 2;
|
| | | int pPY = pY + size / 2 - portraitSize / 2;
|
| | | |
| | | // 头像白色边框
|
| | | g2d.fillRoundRect(pPX-4, pPY-4,portraitSize+8, portraitSize + 8, 5, 5);
|
| | | g2d.setRenderingHints(mapH);
|
| | | // 画 头像
|
| | | BufferedImage portraitImg = ImageIO.read(portraitInputStream);
|
| | | portraitImg = ImageUtil.zoomInImage(portraitImg, portraitSize, portraitSize);
|
| | | portraitImg = ImageUtil.roundImage(portraitImg, 10);
|
| | | g2d.drawImage(portraitImg, pPX, pPY, portraitSize, portraitSize, null);
|
| | |
|
| | | |
| | | // 画邀请码(邀请码的大小与字体固定)
|
| | | if (!StringUtil.isNullOrEmpty(inviteCode)) {
|
| | | String st = "邀请码:" + inviteCode;
|
| | | String os = System.getProperty("os.name");
|
| | | String fontBoldPath = "/usr/share/fonts/PingFang_Medium.ttf";
|
| | | if (os.toLowerCase().startsWith("win")) {
|
| | | fontBoldPath = "C:/PingFang_Medium.ttf";
|
| | | }
|
| | |
|
| | | Font boldFont = null;
|
| | | try {
|
| | | boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f);
|
| | | } catch (FontFormatException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | g2d.setFont(boldFont);
|
| | | g2d.getFontMetrics(boldFont);
|
| | | g2d.setColor(new Color(0, 0, 0));
|
| | | |
| | | int ax = pX - 30;
|
| | | int ay = pY + size + 118;
|
| | | |
| | | g2d.setColor(new Color(0, 0, 0));
|
| | | g2d.drawString(st, ax, ay);
|
| | |
|
| | | }
|
| | |
|
| | | OutputStream out = new FileOutputStream(new File(targetPath));
|
| | | ImageIO.write(targetImg, "JPEG", out);
|
| | | out.flush();
|
| | | out.close();
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 绘制邀请问候图
|
| | | * @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);
|
| | | g2d.setBackground(Color.WHITE);
|
| | | |
| | | // 画 蒙层
|
| | | 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 = 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;
|
| | | |
| | | // 头像白色边框
|
| | | g2d.fillRoundRect(pPX-4, pPY-4,portraitSize+8, portraitSize + 8, 5, 5);
|
| | | g2d.setRenderingHints(mapH);
|
| | | |
| | | BufferedImage portraitImg = ImageIO.read(portraitInStream);
|
| | | portraitImg = ImageUtil.zoomInImage(portraitImg, portraitSize, portraitSize);
|
| | | portraitImg = ImageUtil.roundImage(portraitImg, 10);
|
| | | g2d.drawImage(portraitImg, pPX, pPY, portraitSize, portraitSize, null);
|
| | | |
| | |
|
| | | // 字体白色
|
| | | 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 = "C:/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();
|
| | | }
|
| | | |
| | | }
|
| | | package com.yeshi.fanli.util; |
| | | |
| | | import java.awt.AlphaComposite; |
| | | import java.awt.BasicStroke; |
| | | import java.awt.Color; |
| | | import java.awt.Font; |
| | | import java.awt.FontFormatException; |
| | | import java.awt.FontMetrics; |
| | | import java.awt.Graphics; |
| | | import java.awt.Graphics2D; |
| | | import java.awt.RenderingHints; |
| | | import java.awt.RenderingHints.Key; |
| | | import java.awt.geom.RoundRectangle2D; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | 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.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.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 { |
| | | |
| | | |
| | | /** |
| | | * 分析海报图 |
| | | * @param qrcode |
| | | * @param portraitStream |
| | | * @param goods |
| | | * @return |
| | | */ |
| | | public static InputStream drawGoodsQuickhShare(InputStream qrcode, InputStream portraitStream, String inviteCode, |
| | | String mainPic, GoodsDetailVO goods) { |
| | | try { |
| | | 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"; |
| | | } |
| | | Font font24 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(24.0f); |
| | | Font font26 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(26.0f); |
| | | Font boldFont36 = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); |
| | | |
| | | int px = 640; // 图片宽度 |
| | | // int py = 1154; // 图片高度 显示邀请码 |
| | | int py = 1060; // 图片高度 |
| | | 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.WHITE); |
| | | g2d.fillRect(0, 0, px, py); |
| | | |
| | | int x = 640; |
| | | int y = 640; |
| | | int spacing = 20; // 右边距 |
| | | |
| | | // 1、画商品主图 |
| | | InputStream picStream = HttpUtil.getAsInputStream(mainPic); |
| | | // InputStream picStream = |
| | | // ImageUtil.class.getClassLoader().getResourceAsStream("image/share/test_pic1.jpg"); |
| | | g2d.drawImage(ImageIO.read(picStream), 0, 0, x, y, null); |
| | | |
| | | // 2、画来源图标 |
| | | int length = 10; |
| | | int row1 = 450; |
| | | int iconWidth = 72; |
| | | String source = ""; |
| | | InputStream icon = null; |
| | | int goodsType = goods.getGoodsType(); |
| | | if (goodsType == Constant.SOURCE_TYPE_TAOBAO) { |
| | | source = "淘宝"; |
| | | if (goods.getShopType() == 10) { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tb.png"); |
| | | } else { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tm.png"); |
| | | } |
| | | } else if (goodsType == Constant.SOURCE_TYPE_JD) { |
| | | source = "京东"; |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_jd.png"); |
| | | } else if (goodsType == Constant.SOURCE_TYPE_PDD) { |
| | | source = "拼多多"; |
| | | length = 9; |
| | | row1 = row1 - 26; |
| | | iconWidth = iconWidth + 28; |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_pdd.png"); |
| | | } else { |
| | | return null; |
| | | } |
| | | g2d.drawImage(ImageIO.read(icon), spacing, y + 20, iconWidth, 34, null); |
| | | |
| | | // 3、商品标题内容 |
| | | int row = 0; |
| | | String title = goods.getTitle(); |
| | | length = ImageUtil.getTextLengthByWidth(g2d, font26, title, row1, length); |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | g2d.drawString(title.substring(0, length), spacing + iconWidth + 5, y + 45 + row * 36); |
| | | |
| | | row++; |
| | | title = title.substring(length); |
| | | int length2 = ImageUtil.getTextLengthByWidth(g2d, font26, title, 600, 11); |
| | | if (length2 > 24) { |
| | | g2d.drawString(title.substring(0, 21) + "...", spacing, y + 45 + row * 36); |
| | | } else { |
| | | g2d.drawString(title.substring(0, length2), spacing, y + 45 + row * 36); |
| | | } |
| | | |
| | | int baoYouLength = spacing; |
| | | if (!goods.isHasCoupon()) { |
| | | // 无券 |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("¥ ", spacing, y + 125); |
| | | g2d.setFont(boldFont36); |
| | | g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()).toString(), spacing + 21, y + 125); |
| | | } else { |
| | | // 券后价 |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("券后价 ¥ ", spacing, y + 125); |
| | | |
| | | g2d.setFont(boldFont36); |
| | | String couponPrice = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponPrice()).toString(); |
| | | g2d.drawString(couponPrice, spacing + 116, y + 125); |
| | | |
| | | FontMetrics fm2 = g2d.getFontMetrics(boldFont36); |
| | | int textLength1 = 116 + fm2.stringWidth(couponPrice); |
| | | |
| | | // 渠道原价 |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.setFont(font24); |
| | | g2d.drawString(goods.getPriceName() + " ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()), |
| | | spacing + textLength1 + 10, y + 125); |
| | | |
| | | // 券字 |
| | | InputStream quan = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_quan.png"); |
| | | g2d.drawImage(ImageIO.read(quan), spacing, y + 140, 47, 40, null); |
| | | // 券面额 |
| | | String quanAmount = "¥ " + goods.getCouponInfo().getAmount(); |
| | | FontMetrics fm = g2d.getFontMetrics(font26); |
| | | int textLength = fm.stringWidth(quanAmount); |
| | | |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| | | g2d.drawRoundRect(spacing + 42, y + 140, textLength + 20, 39, 10, 10); |
| | | g2d.drawString(quanAmount, spacing + 55, y + 170); |
| | | |
| | | baoYouLength = spacing + 42 + textLength + 20 + 20; |
| | | } |
| | | |
| | | // 自营 、包邮 |
| | | InputStream ziying = null; |
| | | if (goods.getShopType() == 21) { |
| | | ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_ziying.png"); |
| | | g2d.drawImage(ImageIO.read(ziying), baoYouLength, y + 150, 60, 30, null); |
| | | } else if (goods.isBaoyou()) { |
| | | ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_baoyou.png"); |
| | | g2d.drawImage(ImageIO.read(ziying), baoYouLength, y + 150, 60, 30, null); |
| | | } |
| | | |
| | | // 销量 |
| | | String sale = null; |
| | | int salesType = goods.getSalesType(); |
| | | if (salesType == 1) { |
| | | sale = "月销"; |
| | | } else if (salesType == 2) { |
| | | sale = "2小时销量"; |
| | | } else if (salesType == 3) { |
| | | sale = "今日销量"; |
| | | } else if (salesType == 4) { |
| | | sale = "总销量"; |
| | | } else { |
| | | sale = "月销"; |
| | | } |
| | | sale = sale + " " + goods.getSalesCount(); |
| | | FontMetrics fm = g2d.getFontMetrics(font24); |
| | | int textLength = fm.stringWidth(sale); |
| | | g2d.setFont(font24); |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.drawString(sale, spacing + 605 - textLength, y + 125); |
| | | |
| | | // 下单提示 |
| | | InputStream downTip = null; |
| | | if (goodsType == Constant.SOURCE_TYPE_TAOBAO) { |
| | | downTip = ImageUtil.class.getClassLoader().getResourceAsStream("image/share/down_tip_tb.png"); |
| | | } else { |
| | | downTip = ImageUtil.class.getClassLoader().getResourceAsStream("image/share/down_tip_jd_pdd.png"); |
| | | } |
| | | g2d.drawImage(ImageIO.read(downTip), spacing, y + 200, 382, 203, null); |
| | | |
| | | // 二维码图框 |
| | | InputStream codeFrame = ImageUtil.class.getClassLoader() |
| | | .getResourceAsStream("image/share/qr_code_frame01.png"); |
| | | g2d.drawImage(ImageIO.read(codeFrame), spacing + 405, y + 200, 200, 203, null); |
| | | |
| | | // 画二维码 |
| | | int codeSize = 170; |
| | | int pX = spacing + 422; |
| | | int pY = y + 217; |
| | | g2d.drawImage(ImageIO.read(qrcode), pX, pY, codeSize, codeSize, null); |
| | | |
| | | int portraitSize = 200 * 5 / 23; |
| | | int pPX = pX + codeSize / 2 - portraitSize / 2; |
| | | int pPY = pY + codeSize / 2 - portraitSize / 2; |
| | | // 头像白色边框 |
| | | g2d.setColor(Color.WHITE); |
| | | g2d.fillRoundRect(pPX - 4, pPY - 4, portraitSize + 8, portraitSize + 8, 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.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); |
| | | |
| | | inviteCode = "邀请码:" + inviteCode; |
| | | 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.setColor(new Color(229, 71, 27)); |
| | | 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.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 drawGoodsShareXCX(InputStream qrcode, UserInfo user, GoodsDetailVO goods) { |
| | | try { |
| | | 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"; |
| | | } |
| | | Font font24 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(24.0f); |
| | | Font font26 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(26.0f); |
| | | Font boldFont36 = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); |
| | | |
| | | int px = 640; // 图片宽度 |
| | | int py = 1061; // 图片高度 |
| | | 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.WHITE); |
| | | g2d.fillRect(0, 0, px, py); |
| | | |
| | | int x = 640; |
| | | int y = 640; |
| | | int spacing = 20; // 右边距 |
| | | |
| | | // 1、画商品主图 |
| | | InputStream picStream = HttpUtil.getAsInputStream(goods.getPicUrl().replace("https://", "http://")); |
| | | g2d.drawImage(ImageIO.read(picStream), 0, 0, x, y, null); |
| | | |
| | | // 2、画来源图标 |
| | | int length = 10; |
| | | int row1 = 260; |
| | | int iconWidth = 72; |
| | | String source = ""; |
| | | InputStream icon = null; |
| | | int goodsType = goods.getGoodsType(); |
| | | if (goodsType == Constant.SOURCE_TYPE_TAOBAO) { |
| | | source = "淘宝"; |
| | | if (goods.getShopType() == 10) { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tb.png"); |
| | | } else { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tm.png"); |
| | | } |
| | | } else if (goodsType == Constant.SOURCE_TYPE_JD) { |
| | | source = "京东"; |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_jd.png"); |
| | | } else if (goodsType == Constant.SOURCE_TYPE_PDD) { |
| | | source = "拼多多"; |
| | | length = 9; |
| | | row1 = row1 - 26; |
| | | iconWidth = iconWidth + 28; |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_pdd.png"); |
| | | } else { |
| | | return null; |
| | | } |
| | | g2d.drawImage(ImageIO.read(icon), spacing, y + 20, iconWidth, 34, null); |
| | | |
| | | // 3、商品标题内容 |
| | | int row = 0; |
| | | String title = goods.getTitle(); |
| | | length = ImageUtil.getTextLengthByWidth(g2d, font26, title, row1, length); |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | g2d.drawString(title.substring(0, length), spacing + iconWidth + 5, y + 45 + row * 36); |
| | | |
| | | title = title.substring(length); |
| | | row++; |
| | | // 判断标题内容是否画完,最多可画三行 |
| | | while (title.length() > 0 && row < 3) { |
| | | length = ImageUtil.getTextLengthByWidth(g2d, font26, title, 320, 11); |
| | | g2d.drawString(title.substring(0, length), spacing, y + 45 + row * 36); |
| | | title = title.substring(length); |
| | | row++; |
| | | } |
| | | |
| | | if (!goods.isHasCoupon()) { |
| | | // 无券 |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("¥ ", spacing, y + 175); |
| | | g2d.setFont(boldFont36); |
| | | g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()).toString(), spacing + 21, y + 175); |
| | | } else { |
| | | // 券后价 |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("券后价 ¥ ", spacing, y + 175); |
| | | g2d.setFont(boldFont36); |
| | | g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getCouponPrice()).toString(), spacing + 116, |
| | | y + 175); |
| | | |
| | | // 渠道原价 |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.setFont(font24); |
| | | g2d.drawString(goods.getPriceName() + " ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()), |
| | | spacing, y + 210); |
| | | |
| | | // 券字 |
| | | InputStream quan = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_quan.png"); |
| | | g2d.drawImage(ImageIO.read(quan), spacing, y + 240, 47, 40, null); |
| | | // 券面额 |
| | | String quanAmount = "¥ " + goods.getCouponInfo().getAmount(); |
| | | FontMetrics fm = g2d.getFontMetrics(font26); |
| | | int textLength = fm.stringWidth(quanAmount); |
| | | |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| | | g2d.drawRoundRect(spacing + 42, y + 240, textLength + 20, 39, 10, 10); |
| | | g2d.drawString(quanAmount, spacing + 55, y + 270); |
| | | |
| | | } |
| | | |
| | | // 自营 、包邮 |
| | | if (goods.getShopType() == 21) { |
| | | InputStream ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_ziying.png"); |
| | | g2d.drawImage(ImageIO.read(ziying), spacing + 300, y + 150, 60, 30, null); |
| | | } else if (goods.isBaoyou()) { |
| | | InputStream ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_baoyou.png"); |
| | | g2d.drawImage(ImageIO.read(ziying), spacing + 300, y + 150, 60, 30, null); |
| | | } |
| | | |
| | | // 销量 |
| | | String sale = null; |
| | | int salesType = goods.getSalesType(); |
| | | if (salesType == 1) { |
| | | sale = "月销"; |
| | | } else if (salesType == 2) { |
| | | sale = "2小时销量"; |
| | | } else if (salesType == 3) { |
| | | sale = "今日销量"; |
| | | } else if (salesType == 4) { |
| | | sale = "总销量"; |
| | | } else { |
| | | sale = "月销"; |
| | | } |
| | | sale = sale + " " + goods.getSalesCount(); |
| | | FontMetrics fm = g2d.getFontMetrics(font24); |
| | | int textLength = fm.stringWidth(sale); |
| | | g2d.setFont(font24); |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.drawString(sale, spacing + 360 - textLength, y + 210); |
| | | |
| | | // 二维码图框 |
| | | g2d.setColor(Color.WHITE); |
| | | 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; |
| | | g2d.drawImage(ImageIO.read(qrcode), spacing + 420, y + 38, codeSize, codeSize, null); |
| | | |
| | | // 头像 |
| | | InputStream portraitStream = null; |
| | | try { |
| | | portraitStream = HttpUtil.getAsInputStream(user.getPortrait()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | if (portraitStream == null) { |
| | | portraitStream = ImageUtil.class.getClassLoader() |
| | | .getResourceAsStream("image/official_default_head.jpg"); |
| | | } |
| | | BufferedImage portraitImg = ImageIO.read(portraitStream); |
| | | BufferedImage zoomInImage = ImageUtil.zoomInImage(portraitImg, 84, 84); |
| | | BufferedImage roundImage = ImageUtil.roundImage(zoomInImage, 84); |
| | | g2d.drawImage(roundImage, spacing, y + 310, 84, 84, null); |
| | | |
| | | g2d.setFont(font24); |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | g2d.drawString(user.getNickName(), spacing + 84 + 17, y + 335); |
| | | |
| | | g2d.setFont(font24); |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.drawString(source + "商品99%都有优惠或返利", spacing + 84 + 17, y + 385); |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 分析海报图 |
| | | * @param qrcode |
| | | * @param portraitStream |
| | | * @param goods |
| | | * @return |
| | | */ |
| | | public static InputStream drawGoodsPoster(InputStream qrcode, InputStream portraitStream, String inviteCode, |
| | | String mainPic, GoodsDetailVO goods) { |
| | | try { |
| | | 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"; |
| | | } |
| | | Font font24 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(24.0f); |
| | | Font font26 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(26.0f); |
| | | Font boldFont36 = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); |
| | | |
| | | int px = 640; // 图片宽度 |
| | | // int py = 1154; // 图片高度 显示邀请码 |
| | | int py = 1060; // 图片高度 |
| | | 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.WHITE); |
| | | g2d.fillRect(0, 0, px, py); |
| | | |
| | | int x = 640; |
| | | int y = 640; |
| | | int spacing = 20; // 右边距 |
| | | |
| | | // 1、画商品主图 |
| | | InputStream picStream = HttpUtil.getAsInputStream(mainPic); |
| | | // InputStream picStream = |
| | | // ImageUtil.class.getClassLoader().getResourceAsStream("image/share/test_pic1.jpg"); |
| | | g2d.drawImage(ImageIO.read(picStream), 0, 0, x, y, null); |
| | | |
| | | // 2、画来源图标 |
| | | int length = 10; |
| | | int row1 = 450; |
| | | int iconWidth = 72; |
| | | String source = ""; |
| | | InputStream icon = null; |
| | | int goodsType = goods.getGoodsType(); |
| | | if (goodsType == Constant.SOURCE_TYPE_TAOBAO) { |
| | | source = "淘宝"; |
| | | if (goods.getShopType() == 10) { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tb.png"); |
| | | } else { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tm.png"); |
| | | } |
| | | } else if (goodsType == Constant.SOURCE_TYPE_JD) { |
| | | source = "京东"; |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_jd.png"); |
| | | } else if (goodsType == Constant.SOURCE_TYPE_PDD) { |
| | | source = "拼多多"; |
| | | length = 9; |
| | | row1 = row1 - 26; |
| | | iconWidth = iconWidth + 28; |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_pdd.png"); |
| | | } else { |
| | | return null; |
| | | } |
| | | g2d.drawImage(ImageIO.read(icon), spacing, y + 20, iconWidth, 34, null); |
| | | |
| | | // 3、商品标题内容 |
| | | int row = 0; |
| | | String title = goods.getTitle(); |
| | | length = ImageUtil.getTextLengthByWidth(g2d, font26, title, row1, length); |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | g2d.drawString(title.substring(0, length), spacing + iconWidth + 5, y + 45 + row * 36); |
| | | |
| | | row++; |
| | | title = title.substring(length); |
| | | int length2 = ImageUtil.getTextLengthByWidth(g2d, font26, title, 600, 11); |
| | | if (length2 > 24) { |
| | | g2d.drawString(title.substring(0, 21) + "...", spacing, y + 45 + row * 36); |
| | | } else { |
| | | g2d.drawString(title.substring(0, length2), spacing, y + 45 + row * 36); |
| | | } |
| | | |
| | | int baoYouLength = spacing; |
| | | if (!goods.isHasCoupon()) { |
| | | // 无券 |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("¥ ", spacing, y + 125); |
| | | g2d.setFont(boldFont36); |
| | | g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()).toString(), spacing + 21, y + 125); |
| | | } else { |
| | | // 券后价 |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("券后价 ¥ ", spacing, y + 125); |
| | | |
| | | g2d.setFont(boldFont36); |
| | | String couponPrice = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponPrice()).toString(); |
| | | g2d.drawString(couponPrice, spacing + 116, y + 125); |
| | | |
| | | FontMetrics fm2 = g2d.getFontMetrics(boldFont36); |
| | | int textLength1 = 116 + fm2.stringWidth(couponPrice); |
| | | |
| | | // 渠道原价 |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.setFont(font24); |
| | | g2d.drawString(goods.getPriceName() + " ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()), |
| | | spacing + textLength1 + 10, y + 125); |
| | | |
| | | // 券字 |
| | | InputStream quan = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_quan.png"); |
| | | g2d.drawImage(ImageIO.read(quan), spacing, y + 140, 47, 40, null); |
| | | // 券面额 |
| | | String quanAmount = "¥ " + goods.getCouponInfo().getAmount(); |
| | | FontMetrics fm = g2d.getFontMetrics(font26); |
| | | int textLength = fm.stringWidth(quanAmount); |
| | | |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| | | g2d.drawRoundRect(spacing + 42, y + 140, textLength + 20, 39, 10, 10); |
| | | g2d.drawString(quanAmount, spacing + 55, y + 170); |
| | | |
| | | baoYouLength = spacing + 42 + textLength + 20 + 20; |
| | | } |
| | | |
| | | // 自营 、包邮 |
| | | InputStream ziying = null; |
| | | if (goods.getShopType() == 21) { |
| | | ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_ziying.png"); |
| | | g2d.drawImage(ImageIO.read(ziying), baoYouLength, y + 150, 60, 30, null); |
| | | } else if (goods.isBaoyou()) { |
| | | ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_baoyou.png"); |
| | | g2d.drawImage(ImageIO.read(ziying), baoYouLength, y + 150, 60, 30, null); |
| | | } |
| | | |
| | | // 销量 |
| | | String sale = null; |
| | | int salesType = goods.getSalesType(); |
| | | if (salesType == 1) { |
| | | sale = "月销"; |
| | | } else if (salesType == 2) { |
| | | sale = "2小时销量"; |
| | | } else if (salesType == 3) { |
| | | sale = "今日销量"; |
| | | } else if (salesType == 4) { |
| | | sale = "总销量"; |
| | | } else { |
| | | sale = "月销"; |
| | | } |
| | | sale = sale + " " + goods.getSalesCount(); |
| | | FontMetrics fm = g2d.getFontMetrics(font24); |
| | | int textLength = fm.stringWidth(sale); |
| | | g2d.setFont(font24); |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.drawString(sale, spacing + 605 - textLength, y + 125); |
| | | |
| | | // 下单提示 |
| | | InputStream downTip = null; |
| | | if (goodsType == Constant.SOURCE_TYPE_TAOBAO) { |
| | | downTip = ImageUtil.class.getClassLoader().getResourceAsStream("image/share/down_tip_tb.png"); |
| | | } else { |
| | | downTip = ImageUtil.class.getClassLoader().getResourceAsStream("image/share/down_tip_jd_pdd.png"); |
| | | } |
| | | g2d.drawImage(ImageIO.read(downTip), spacing, y + 200, 382, 203, null); |
| | | |
| | | // 二维码图框 |
| | | InputStream codeFrame = ImageUtil.class.getClassLoader() |
| | | .getResourceAsStream("image/share/qr_code_frame01.png"); |
| | | g2d.drawImage(ImageIO.read(codeFrame), spacing + 405, y + 200, 200, 203, null); |
| | | |
| | | // 画二维码 |
| | | int codeSize = 170; |
| | | int pX = spacing + 422; |
| | | int pY = y + 217; |
| | | g2d.drawImage(ImageIO.read(qrcode), pX, pY, codeSize, codeSize, null); |
| | | |
| | | int portraitSize = 200 * 5 / 23; |
| | | int pPX = pX + codeSize / 2 - portraitSize / 2; |
| | | int pPY = pY + codeSize / 2 - portraitSize / 2; |
| | | // 头像白色边框 |
| | | g2d.setColor(Color.WHITE); |
| | | g2d.fillRoundRect(pPX - 4, pPY - 4, portraitSize + 8, portraitSize + 8, 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.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); |
| | | // |
| | | // inviteCode = "邀请码:" + inviteCode; |
| | | // 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.setColor(new Color(229,71, 27)); |
| | | // 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.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; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 单个商品分享图 |
| | | * |
| | | * @param qrcode 二维码流 |
| | | * @param portrait 头像流 |
| | | * @param goods 商品信息 |
| | | * @return |
| | | */ |
| | | public static InputStream drawGoodsShareSingle(InputStream qrcode, InputStream portrait, GoodsDetailVO goods) { |
| | | try { |
| | | 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"; |
| | | } |
| | | Font font24 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(24.0f); |
| | | Font font26 = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(26.0f); |
| | | Font boldFont36 = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); |
| | | |
| | | int px = 640; // 图片宽度 |
| | | int py = 1061; // 图片高度 |
| | | if (!goods.isHasCoupon()) { |
| | | py = py - 131; |
| | | } |
| | | |
| | | 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.WHITE); |
| | | g2d.fillRect(0, 0, px, py); |
| | | |
| | | int x = 640; |
| | | int y = 640; |
| | | int spacing = 20; // 右边距 |
| | | |
| | | // 1、画商品主图 |
| | | InputStream picStream = HttpUtil.getAsInputStream(goods.getPicUrl().replace("https://", "http://")); |
| | | g2d.drawImage(ImageIO.read(picStream), 0, 0, x, y, null); |
| | | |
| | | // 2、画来源图标 |
| | | int length = 10; |
| | | int row1 = 260; |
| | | int iconWidth = 72; |
| | | InputStream icon = null; |
| | | int goodsType = goods.getGoodsType(); |
| | | if (goodsType == Constant.SOURCE_TYPE_TAOBAO) { |
| | | if (goods.getShopType() == 10) { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tb.png"); |
| | | } else { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tm.png"); |
| | | } |
| | | } else if (goodsType == Constant.SOURCE_TYPE_JD) { |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_jd.png"); |
| | | } else if (goodsType == Constant.SOURCE_TYPE_PDD) { |
| | | length = 9; |
| | | row1 = row1 - 26; |
| | | iconWidth = iconWidth + 28; |
| | | icon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_pdd.png"); |
| | | } else { |
| | | return null; |
| | | } |
| | | g2d.drawImage(ImageIO.read(icon), spacing, y + 20, iconWidth, 34, null); |
| | | |
| | | // 3、商品标题内容 |
| | | int row = 0; |
| | | String title = goods.getTitle(); |
| | | length = ImageUtil.getTextLengthByWidth(g2d, font26, title, row1, length); |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | g2d.drawString(title.substring(0, length), spacing + iconWidth + 5, y + 45 + row * 36); |
| | | |
| | | title = title.substring(length); |
| | | row++; |
| | | // 判断标题内容是否画完,最多可画三行 |
| | | while (title.length() > 0 && row < 3) { |
| | | length = ImageUtil.getTextLengthByWidth(g2d, font26, title, 320, 11); |
| | | g2d.drawString(title.substring(0, length), spacing, y + 45 + row * 36); |
| | | title = title.substring(length); |
| | | row++; |
| | | } |
| | | |
| | | String code_frame = "image/share/qr_code_frame.png"; |
| | | if (!goods.isHasCoupon()) { |
| | | // 无券 |
| | | code_frame = "image/share/qr_code_frame_no_coupon.png"; |
| | | |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("¥ ", spacing, y + 175); |
| | | g2d.setFont(boldFont36); |
| | | g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()).toString(), spacing + 21, y + 175); |
| | | } else { |
| | | // 券后价 |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("券后价 ¥ ", spacing, y + 175); |
| | | g2d.setFont(boldFont36); |
| | | g2d.drawString(MoneyBigDecimalUtil.getWithNoZera(goods.getCouponPrice()).toString(), spacing + 116, |
| | | y + 175); |
| | | |
| | | // 渠道原价(在售价) |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.setFont(font24); |
| | | g2d.drawString(goods.getPriceName() + " ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()), |
| | | spacing, y + 210); |
| | | |
| | | // 券字 |
| | | InputStream quan = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_quan.png"); |
| | | g2d.drawImage(ImageIO.read(quan), spacing, y + 240, 47, 40, null); |
| | | // 券面额 |
| | | String quanAmount = "¥ " + goods.getCouponInfo().getAmount(); |
| | | FontMetrics fm = g2d.getFontMetrics(font26); |
| | | int textLength = fm.stringWidth(quanAmount); |
| | | g2d.setFont(font26); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| | | g2d.drawRoundRect(spacing + 42, y + 240, textLength + 20, 39, 10, 10); |
| | | g2d.drawString(quanAmount, spacing + 55, y + 270); |
| | | |
| | | // 提示语 |
| | | InputStream tipStream = ImageUtil.class.getClassLoader() |
| | | .getResourceAsStream("image/share/tip_angle.png"); |
| | | g2d.drawImage(ImageIO.read(tipStream), spacing, y + 300, 601, 101, null); |
| | | } |
| | | |
| | | // 自营 、包邮 |
| | | if (goods.getShopType() == 21) { |
| | | InputStream ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_ziying.png"); |
| | | g2d.drawImage(ImageIO.read(ziying), spacing + 300, y + 150, 60, 30, null); |
| | | } else if (goods.isBaoyou()) { |
| | | InputStream ziying = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_baoyou.png"); |
| | | g2d.drawImage(ImageIO.read(ziying), spacing + 300, y + 150, 60, 30, null); |
| | | } |
| | | |
| | | // 销量 |
| | | String sale = null; |
| | | int salesType = goods.getSalesType(); |
| | | if (salesType == 1) { |
| | | sale = "月销"; |
| | | } else if (salesType == 2) { |
| | | sale = "2小时销量"; |
| | | } else if (salesType == 3) { |
| | | sale = "今日销量"; |
| | | } else if (salesType == 4) { |
| | | sale = "总销量"; |
| | | } else { |
| | | sale = "月销"; |
| | | } |
| | | sale = sale + " " + goods.getSalesCount(); |
| | | FontMetrics fm = g2d.getFontMetrics(font24); |
| | | int textLength = fm.stringWidth(sale); |
| | | g2d.setFont(font24); |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | g2d.drawString(sale, spacing + 360 - textLength, y + 210); |
| | | |
| | | // 二维码图框 |
| | | g2d.setColor(Color.WHITE); |
| | | InputStream codeFrame = ImageUtil.class.getClassLoader().getResourceAsStream(code_frame); |
| | | g2d.drawImage(ImageIO.read(codeFrame), spacing + 405, y + 20, 200, 261, null); |
| | | |
| | | // 画 二维码 |
| | | int codeSize = 170; |
| | | g2d.drawImage(ImageIO.read(qrcode), spacing + 420, y + 38, codeSize, codeSize, null); |
| | | |
| | | // 画二维码中 头像 |
| | | int portraitSize = 40; |
| | | int pPX = spacing + 425 + codeSize / 2 + -portraitSize / 2; |
| | | int pPY = y + 35 + codeSize / 2 - portraitSize / 2; |
| | | BufferedImage portraitImg = ImageIO.read(portrait); |
| | | portraitImg = ImageUtil.roundImage(portraitImg, 10); |
| | | // 头像白色边框 |
| | | g2d.fillRoundRect(pPX - 4, pPY - 4, portraitSize + 8, portraitSize + 8, 10, 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) { |
| | | e.printStackTrace(); |
| | | try { |
| | | LogHelper.errorDetailInfo(e); |
| | | } catch (Exception e1) { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | // 画商品分享图 |
| | | public static InputStream drawGoodsShareImg(InputStream qrcodeStream, InputStream portrait, |
| | | TaoBaoGoodsBrief goods) { |
| | | |
| | | 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(720, 1280, 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, 720, 1280); |
| | | 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(); |
| | | } |
| | | |
| | | g2d.setFont(font); |
| | | try { |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | // 画来源图标 |
| | | InputStream userTypeIcon = null; |
| | | if (goods.getUserType() == 0)// 淘宝 |
| | | { |
| | | userTypeIcon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tb.png"); |
| | | } else { |
| | | userTypeIcon = ImageUtil.class.getClassLoader().getResourceAsStream("image/icon_tm.png"); |
| | | } |
| | | |
| | | g2d.drawImage(ImageIO.read(userTypeIcon), 50, 88, null); |
| | | |
| | | // 商品标题 |
| | | String title = goods.getTitle(); |
| | | int row = 0; |
| | | int length = 0; |
| | | // 画第一排 |
| | | length = getTextLengthByWidth(g2d, font, title, 500, 10); |
| | | g2d.drawString(title.substring(0, length), 50 + 65, 112 + row * 40); |
| | | title = title.substring(length); |
| | | row++; |
| | | |
| | | // 判断是否画完,最多画2排 |
| | | while (title.length() > 0 && row < 2) { |
| | | length = getTextLengthByWidth(g2d, font, title, 335, 10); |
| | | g2d.drawString(title.substring(0, length), 50, 112 + row * 40); |
| | | title = title.substring(length); |
| | | row++; |
| | | } |
| | | |
| | | // 画商品主图 |
| | | InputStream goodsPicture = TaoBaoHttpUtil.getAsInputStream(goods.getPictUrl()); |
| | | BufferedImage picImage = ImageIO.read(goodsPicture); |
| | | picImage = zoomInImage(picImage, 620, 620); |
| | | g2d.drawImage(picImage, 50, 190, null); |
| | | Font boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(50.0f); |
| | | // 画价格 |
| | | // 有券 |
| | | if (!StringUtil.isNullOrEmpty(goods.getCouponInfo())) { |
| | | BigDecimal finalPrice = goods.getZkPrice(); |
| | | if (goods.getCouponStartFee().compareTo(goods.getZkPrice()) <= 0 |
| | | && goods.getZkPrice().compareTo(goods.getCouponAmount()) >= 0) { |
| | | finalPrice = goods.getZkPrice().subtract(goods.getCouponAmount()); |
| | | } |
| | | |
| | | g2d.setColor(new Color(229, 0, 93)); |
| | | g2d.drawString("券后价 ¥", 57, 875); |
| | | g2d.setColor(new Color(229, 0, 93)); |
| | | g2d.setFont(boldFont); |
| | | g2d.drawString(finalPrice.toString(), 85 + 110, 875); |
| | | |
| | | // 画券右侧 |
| | | BufferedImage quanRight = ImageIO |
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/fanli_quan_right.png")); |
| | | g2d.setColor(new Color(241, 66, 66)); |
| | | g2d.drawImage(quanRight, 670 - quanRight.getWidth(), 852, null); |
| | | |
| | | // 画券的内容 |
| | | g2d.setColor(new Color(229, 0, 93)); |
| | | |
| | | String quanString = " " + MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()) + "元券 "; |
| | | |
| | | boldFont = boldFont.deriveFont(44.0f); |
| | | g2d.setFont(boldFont); |
| | | FontMetrics fm = g2d.getFontMetrics(boldFont); |
| | | int textLength = fm.stringWidth(quanString); |
| | | |
| | | g2d.fillRect(670 - quanRight.getWidth() - textLength, 852, textLength, 80); |
| | | |
| | | g2d.setColor(Color.WHITE); |
| | | |
| | | g2d.setFont(boldFont); |
| | | g2d.drawString(quanString, 670 - quanRight.getWidth() - textLength, 927 - 19); |
| | | |
| | | // 画券左侧 |
| | | BufferedImage quanLeft = ImageIO |
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/fanli_quan_left.png")); |
| | | g2d.drawImage(quanLeft, 670 - quanRight.getWidth() - textLength - quanLeft.getWidth(), 852, null); |
| | | |
| | | String zkPriceName = ""; |
| | | if (goods.getUserType() == 0) |
| | | zkPriceName = "淘宝价 ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()); |
| | | else |
| | | zkPriceName = "天猫价 ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()); |
| | | |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | font = font.deriveFont(30.0f); |
| | | g2d.setFont(font); |
| | | g2d.drawString(zkPriceName, 56, 930); |
| | | |
| | | } else// 无券 |
| | | { |
| | | g2d.setColor(new Color(229, 0, 93)); |
| | | g2d.drawString("¥", 60, 870); |
| | | g2d.setColor(new Color(229, 0, 93)); |
| | | BigDecimal finalPrice = goods.getZkPrice(); |
| | | g2d.setFont(boldFont); |
| | | g2d.drawString(finalPrice.toString(), 85, 870); |
| | | } |
| | | |
| | | g2d.setColor(new Color(247, 247, 247)); |
| | | g2d.fillRect(56, 1015, 608, 204); |
| | | |
| | | // 画二维码 |
| | | |
| | | BufferedImage qrcodeImage = ImageIO.read(qrcodeStream); |
| | | qrcodeImage = zoomInImage(qrcodeImage, 170, 170); |
| | | |
| | | g2d.drawImage(qrcodeImage, 56 + 17, 1015 + 17, null); |
| | | |
| | | // 画头像 |
| | | if (portrait != null) { |
| | | BufferedImage portraitImg = ImageIO.read(portrait); |
| | | portraitImg = zoomInImage(portraitImg, 40, 40); |
| | | g2d.drawImage(portraitImg, 56 + 17 + (qrcodeImage.getWidth() - portraitImg.getWidth()) / 2, |
| | | 1015 + 17 + (qrcodeImage.getHeight() - portraitImg.getHeight()) / 2, null); |
| | | } |
| | | |
| | | font = font.deriveFont(30.0f); |
| | | g2d.setFont(font); |
| | | g2d.setColor(new Color(229, 0, 93)); |
| | | |
| | | g2d.setColor(new Color(102, 102, 102)); |
| | | g2d.drawString("长按识别二维码领取优惠券", 250 + 17, 1130 + 17); |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 绘制大的商品动态分享图 |
| | | * |
| | | * @param qrcodeStream |
| | | * @param portrait |
| | | * @param goods |
| | | * @return |
| | | */ |
| | | public static InputStream drawActivityGoodsShareBigImg(InputStream qrcodeStream, InputStream portrait, |
| | | List<RecommendActivityTaoBaoGoods> 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++) { |
| | | RecommendActivityTaoBaoGoods goods = goodsList.get(i); |
| | | |
| | | // 第一张图需要有价格信息 |
| | | 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(goods.getQuanPrice(), 90, 650); |
| | | |
| | | // 画原价 |
| | | g2d.setColor(new Color(102, 102, 102)); |
| | | font = font.deriveFont(24.0f); |
| | | g2d.setFont(font); |
| | | |
| | | String zkPrice = new BigDecimal(goods.getQuanPrice().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.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; |
| | | } |
| | | |
| | | /** |
| | | * 绘制大的商品动态分享图 |
| | | * |
| | | * @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.getCouponPrice(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); |
| | | |
| | | String quanString = ""; |
| | | BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()); |
| | | |
| | | if (!withNoZera.toString().endsWith("0")) { |
| | | |
| | | // 画券右侧 |
| | | 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)); |
| | | |
| | | quanString = " " + withNoZera + "元券 "; |
| | | |
| | | 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); |
| | | |
| | | // 画券 |
| | | |
| | | String quanString = ""; |
| | | BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()); |
| | | |
| | | if (!withNoZera.toString().endsWith("0")) { |
| | | |
| | | // 画券右侧 |
| | | 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)); |
| | | |
| | | quanString = " " + withNoZera + "元券 "; |
| | | |
| | | 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); |
| | | // 圆角 |
| | | portraitImg = roundImage(portraitImg, 10); |
| | | |
| | | 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.setColor(new Color(102, 102, 102)); |
| | | g2d.drawString("长按识别二维码免费领券", 312, 1150); |
| | | |
| | | g2d.setColor(new Color(102, 102, 102)); |
| | | g2d.drawString("共", 1000, 1150); |
| | | |
| | | g2d.setColor(new Color(229, 0, 93)); |
| | | g2d.drawString(goodsList.size() + "", 1055, 1150); |
| | | |
| | | g2d.setColor(new Color(102, 102, 102)); |
| | | g2d.drawString("个商品", 1090, 1150); |
| | | |
| | | // 提示语 |
| | | BufferedImage tips = ImageIO |
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/share/tips1.png")); |
| | | tips = zoomInImage(tips, 850, 65); |
| | | g2d.drawImage(tips, 312, 1190, null); |
| | | |
| | | g2d.dispose(); |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 绘制大的商品动态分享图 |
| | | * |
| | | * @param qrcodeStream |
| | | * @param portrait |
| | | * @param goods |
| | | * @return |
| | | */ |
| | | public static InputStream drawGoodsSharePicture(InputStream qrcodeStream, InputStream portrait, |
| | | List<CommonGoods> 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 { |
| | | boolean index = true; |
| | | // 画第一张 |
| | | for (int i = 0; i < goodsList.size(); i++) { |
| | | CommonGoods goods = goodsList.get(i); |
| | | BigDecimal couplePrice = CommonGoodsUtil.getAfterUseCouplePrice(goods); |
| | | // 第一张图需要有价格信息 |
| | | if (i == 0 || index) { |
| | | int topX = 50; |
| | | int topY = 50; |
| | | |
| | | // 画大图 起始点坐标为(50,50) |
| | | int widht = 650; |
| | | int height = 650; |
| | | InputStream goodsPicture = TaoBaoHttpUtil |
| | | .getAsInputStream(goods.getPicture().replace("_.webp", "").replace("_220x220", "")); |
| | | if (goodsPicture == null) |
| | | continue; |
| | | |
| | | BufferedImage picImage = ImageIO.read(goodsPicture); |
| | | picImage = ImageUtil.zoomInImage(picImage, widht, height); |
| | | 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, widht + 1, widht + 1); |
| | | |
| | | // 画透明背景 |
| | | g2d.setColor(new Color(255, 255, 255, 210)); |
| | | g2d.fillRect(10, height - 50, widht + 40, 100); |
| | | |
| | | // 画券后价金额 |
| | | boldFont = boldFont.deriveFont(42.0f); |
| | | g2d.setFont(boldFont); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | |
| | | g2d.drawString("¥", 70 + 10, height); |
| | | g2d.drawString(couplePrice + "", 70 + 45, height); |
| | | |
| | | // 画原价 |
| | | 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 + 15, height + 30); |
| | | |
| | | // 画删除线 |
| | | FontMetrics fm = g2d.getFontMetrics(font); |
| | | int textLength = fm.stringWidth(zkPrice); |
| | | g2d.setStroke(new BasicStroke(2.0f)); |
| | | g2d.drawLine(70 + 20, height + 21, 90 + 10 + textLength, height + 21); |
| | | |
| | | BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()); |
| | | if (withNoZera != null && withNoZera.compareTo(new BigDecimal(0)) > 0) { |
| | | g2d.setRenderingHints(mapH); |
| | | |
| | | // 券值得长度 |
| | | int size = withNoZera.toString().length() * 22; |
| | | int place = withNoZera.toString().length() * 22; |
| | | |
| | | // 券红色底部 |
| | | int quanBGWidht = widht - 100 - place; |
| | | int quanBGHeight = height - 33; |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.fillRoundRect(quanBGWidht, quanBGHeight, 130 + size, 70, 10, 10); |
| | | |
| | | // 画券字 |
| | | boldFont = boldFont.deriveFont(40.0f); |
| | | g2d.setFont(boldFont); |
| | | g2d.setColor(new Color(255, 255, 255)); |
| | | g2d.drawString("券", quanBGWidht + 8, quanBGHeight + 48); |
| | | |
| | | // 券白色底部 |
| | | g2d.fillRoundRect(quanBGWidht + 55, quanBGHeight + 4, 70 + size, 62, 10, 10); |
| | | |
| | | // 画券面值 |
| | | g2d.setFont(boldFont); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("¥ " + withNoZera + "", quanBGWidht + 70, quanBGHeight + 48); |
| | | } |
| | | |
| | | index = false; |
| | | |
| | | } 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.getPicture().replace("_.webp", "")); |
| | | if (goodsPicture == null) |
| | | continue; |
| | | |
| | | BufferedImage picImage = ImageIO.read(goodsPicture); |
| | | picImage = ImageUtil.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); |
| | | |
| | | // 画券 |
| | | BigDecimal withNoZera = MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()); |
| | | if (withNoZera != null && withNoZera.compareTo(new BigDecimal(0)) > 0) { |
| | | // // 画透明背景 |
| | | // g2d.setColor(new Color(255, 255, 255, 210)); |
| | | // g2d.fillRect(topX + 8, topY + 240, 300, 65); |
| | | |
| | | g2d.setRenderingHints(mapH); |
| | | |
| | | // 券值得长度 |
| | | int size = withNoZera.toString().length() * 22; |
| | | int place = withNoZera.toString().length() * 22; |
| | | |
| | | // // 券红色底部 |
| | | int quanBGWidht = topX + 170 - place; |
| | | int quanBGHeight = topY + 220; |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.fillRoundRect(quanBGWidht, quanBGHeight, 130 + size, 70, 10, 10); |
| | | |
| | | // 画券字 |
| | | boldFont = boldFont.deriveFont(40.0f); |
| | | g2d.setFont(boldFont); |
| | | g2d.setColor(new Color(255, 255, 255)); |
| | | g2d.drawString("券", quanBGWidht + 8, quanBGHeight + 48); |
| | | |
| | | // 券白色底部 |
| | | g2d.fillRoundRect(quanBGWidht + 55, quanBGHeight + 4, 70 + size, 62, 10, 10); |
| | | |
| | | // 画券面值 |
| | | g2d.setFont(boldFont); |
| | | g2d.setColor(new Color(229, 0, 92)); |
| | | g2d.drawString("¥ " + withNoZera + "", quanBGWidht + 70, quanBGHeight + 48); |
| | | } |
| | | } |
| | | } |
| | | |
| | | g2d.setColor(new Color(247, 247, 247)); |
| | | g2d.fillRect(50, 1070, 1320, 214); |
| | | |
| | | // 画二维码 |
| | | |
| | | BufferedImage qrcodeImage = ImageIO.read(qrcodeStream); |
| | | qrcodeImage = ImageUtil.zoomInImage(qrcodeImage, 170, 170); |
| | | |
| | | g2d.drawImage(qrcodeImage, 70, 1092, null); |
| | | |
| | | // 画头像 |
| | | if (portrait != null) { |
| | | BufferedImage portraitImg = ImageIO.read(portrait); |
| | | // 放缩大小 |
| | | portraitImg = ImageUtil.zoomInImage(portraitImg, 40, 40); |
| | | // 圆角 |
| | | portraitImg = ImageUtil.roundImage(portraitImg, 10); |
| | | |
| | | 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.setColor(new Color(102, 102, 102)); |
| | | g2d.drawString("长按识别二维码免费领券", 312, 1150); |
| | | |
| | | g2d.setColor(new Color(102, 102, 102)); |
| | | g2d.drawString("共", 1000, 1150); |
| | | |
| | | g2d.setColor(new Color(229, 0, 93)); |
| | | g2d.drawString(goodsList.size() + "", 1055, 1150); |
| | | |
| | | g2d.setColor(new Color(102, 102, 102)); |
| | | g2d.drawString("个商品", 1090, 1150); |
| | | |
| | | // 提示语 |
| | | BufferedImage tips = ImageIO |
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/share/tips1.png")); |
| | | tips = ImageUtil.zoomInImage(tips, 850, 65); |
| | | g2d.drawImage(tips, 312, 1190, null); |
| | | g2d.dispose(); |
| | | |
| | | 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, |
| | | TaoBaoGoodsBrief goods) throws Exception { |
| | | |
| | | 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"; |
| | | } |
| | | |
| | | Font font = null; |
| | | Font boldFont = null; |
| | | |
| | | font = Font.createFont(Font.PLAIN, new File(fontPath)).deriveFont(28.0f); |
| | | |
| | | boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(28.0f); |
| | | |
| | | final BufferedImage targetImg = new BufferedImage(720, 1280, 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, 720, 1280); |
| | | |
| | | g2d.setFont(font.deriveFont(28.0f)); |
| | | |
| | | // 画广告语 |
| | | InputStream adTitle = ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_share_title_icon.png"); |
| | | if (adTitle != null) { |
| | | BufferedImage adTitleImage = ImageIO.read(adTitle); |
| | | g2d.drawImage(adTitleImage, 192, 101, null); |
| | | } |
| | | |
| | | // 画商品主图 |
| | | InputStream goodsPicture = TaoBaoHttpUtil.getAsInputStream(goods.getPictUrl().replace("https://", "http://")); |
| | | BufferedImage picImage = ImageIO.read(goodsPicture); |
| | | picImage = zoomInImage(picImage, 620, 620); |
| | | g2d.drawImage(picImage, 50, 207, null); |
| | | |
| | | // 画价格 |
| | | BigDecimal money = TaoBaoUtil.getCouponPrice(goods); |
| | | money = MoneyBigDecimalUtil.getWithNoZera(money); |
| | | g2d.setColor(new Color(240, 66, 66)); |
| | | |
| | | g2d.setFont(boldFont.deriveFont(30.0f)); |
| | | g2d.drawString("¥", 62, 900); |
| | | |
| | | g2d.setFont(boldFont.deriveFont(56.0f)); |
| | | g2d.drawString(money.toString(), 90, 900); |
| | | |
| | | g2d.setFont(font.deriveFont(56.0f)); |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | |
| | | if (!StringUtil.isNullOrEmpty(goods.getCouponInfo())) { |
| | | // 画天猫价或者淘宝价 |
| | | String zkPriceName = ""; |
| | | if (goods.getUserType() == 0) |
| | | zkPriceName = "淘宝价 ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()); |
| | | else |
| | | zkPriceName = "天猫价 ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getZkPrice()); |
| | | |
| | | font = font.deriveFont(30.0f); |
| | | g2d.setFont(font); |
| | | g2d.drawString(zkPriceName, 56, 955); |
| | | |
| | | // 画券右侧 |
| | | BufferedImage quanRight = ImageIO |
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_quan_right.png")); |
| | | g2d.setColor(new Color(241, 66, 66)); |
| | | g2d.drawImage(quanRight, 670 - quanRight.getWidth(), 867, null); |
| | | |
| | | // 画券的内容 |
| | | g2d.setColor(new Color(241, 66, 66)); |
| | | |
| | | String quanString = " ¥ " + MoneyBigDecimalUtil.getWithNoZera(goods.getCouponAmount()) + " 券 "; |
| | | |
| | | font = font.deriveFont(44.0f); |
| | | g2d.setFont(font); |
| | | FontMetrics fm = g2d.getFontMetrics(font); |
| | | int textLength = fm.stringWidth(quanString); |
| | | |
| | | g2d.fillRect(670 - quanRight.getWidth() - textLength, 872, textLength, 80); |
| | | |
| | | g2d.setColor(Color.WHITE); |
| | | |
| | | g2d.setFont(font); |
| | | g2d.drawString(quanString, 670 - quanRight.getWidth() - textLength, 927); |
| | | |
| | | // 画券左侧 |
| | | BufferedImage quanLeft = ImageIO |
| | | .read(ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_quan_left.png")); |
| | | g2d.drawImage(quanLeft, 670 - quanRight.getWidth() - textLength - quanLeft.getWidth(), 867, null); |
| | | } |
| | | InputStream userTypeIcon; |
| | | if (goods.getUserType() == 0)// 淘宝 |
| | | { |
| | | userTypeIcon = ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_icon_tb.png"); |
| | | } else { |
| | | userTypeIcon = ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_icon_tm.png"); |
| | | } |
| | | |
| | | g2d.drawImage(ImageIO.read(userTypeIcon), 63, 1022, null); |
| | | |
| | | String title = goods.getTitle(); |
| | | // 商品标题 |
| | | font = font.deriveFont(30.0f); |
| | | g2d.setFont(font); |
| | | |
| | | int length = getTextLengthByWidth(g2d, font, title, 290, 8); |
| | | g2d.setColor(new Color(153, 153, 153)); |
| | | |
| | | int row = 0; |
| | | // 画第一排 |
| | | g2d.drawString(title.substring(0, length), 20 + 50 + 46, 1044 + row * 40); |
| | | |
| | | if (length < title.length()) |
| | | title = title.substring(length); |
| | | else |
| | | title = ""; |
| | | // 判断是否画完 |
| | | row++; |
| | | while (title.length() > 0) { |
| | | length = getTextLengthByWidth(g2d, font, title, 330, 10); |
| | | g2d.drawString(title.substring(0, length), 63, 1044 + row * 40); |
| | | title = title.substring(length); |
| | | row++; |
| | | } |
| | | |
| | | // 画边框 |
| | | InputStream erCodeSide = ImageUtil.class.getClassLoader().getResourceAsStream("image/hcj_ercode_side.png"); |
| | | BufferedImage erCodeSideImage = ImageIO.read(erCodeSide); |
| | | erCodeSideImage = zoomInImage(erCodeSideImage, 200, 200); |
| | | g2d.drawImage(erCodeSideImage, 466, 986, null); |
| | | |
| | | // 画二维码 |
| | | |
| | | BufferedImage qrcodeImage = ImageIO.read(qrcodeStream); |
| | | qrcodeImage = zoomInImage(qrcodeImage, 170, 170); |
| | | |
| | | g2d.drawImage(qrcodeImage, 481, 1000, null); |
| | | |
| | | // 画头像 |
| | | if (portrait != null) { |
| | | BufferedImage portraitImg = ImageIO.read(portrait); |
| | | portraitImg = zoomInImage(portraitImg, 30, 30); |
| | | g2d.drawImage(portraitImg, 481 + (qrcodeImage.getWidth() - portraitImg.getWidth()) / 2, |
| | | 1000 + (qrcodeImage.getHeight() - portraitImg.getHeight()) / 2, null); |
| | | } |
| | | |
| | | font = font.deriveFont(22.0f); |
| | | g2d.setFont(font); |
| | | g2d.setColor(new Color(241, 66, 66)); |
| | | |
| | | FontMetrics fm = g2d.getFontMetrics(font); |
| | | int textLength = fm.stringWidth("海草街提醒您"); |
| | | g2d.drawString("海草街提醒您", 481 + (qrcodeImage.getWidth() - textLength) / 2, 1220); |
| | | |
| | | g2d.setColor(new Color(102, 102, 102)); |
| | | |
| | | fm = g2d.getFontMetrics(font); |
| | | textLength = fm.stringWidth("长按识别二维码"); |
| | | g2d.drawString("长按识别二维码", 481 + (qrcodeImage.getWidth() - textLength) / 2, 1255); |
| | | |
| | | g2d.dispose(); |
| | | |
| | | ByteArrayOutputStream aos = new ByteArrayOutputStream(); |
| | | ImageIO.write(targetImg, "JPEG", aos); |
| | | InputStream is = new ByteArrayInputStream(aos.toByteArray()); |
| | | return is; |
| | | } |
| | | |
| | | private static int[] computeCropPosition(BufferedImage source, int width, int height) { |
| | | int[] cropParams = new int[4]; |
| | | int owidth = source.getWidth(); |
| | | int oheight = source.getHeight(); |
| | | if (oheight * width - owidth * height > 0) { |
| | | cropParams[0] = 0; |
| | | cropParams[1] = (oheight - height) / 2; |
| | | cropParams[2] = owidth; |
| | | cropParams[3] = cropParams[2] * height / width; |
| | | } else { |
| | | cropParams[0] = (owidth - width) / 2; |
| | | cropParams[1] = 0; |
| | | cropParams[3] = oheight; |
| | | cropParams[2] = cropParams[3] * width / height; |
| | | } |
| | | |
| | | return cropParams; |
| | | } |
| | | |
| | | public static BufferedImage crop(BufferedImage source, int startX, int startY, int w, int h) { |
| | | int width = source.getWidth(); |
| | | int height = source.getHeight(); |
| | | |
| | | if (startX <= -1) { |
| | | startX = 0; |
| | | } |
| | | if (startY <= -1) { |
| | | startY = 0; |
| | | } |
| | | if (w <= -1) { |
| | | w = width - 1; |
| | | } |
| | | if (h <= -1) { |
| | | h = height - 1; |
| | | } |
| | | BufferedImage result = new BufferedImage(w, h, source.getType()); |
| | | for (int y = startY; y < h + startY; y++) { |
| | | for (int x = startX; x < w + startX; x++) { |
| | | int rgb = source.getRGB(x, y); |
| | | result.setRGB(x - startX, y - startY, rgb); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static BufferedImage zoomInImage(BufferedImage originalImage, int width, int height) { |
| | | int type = originalImage.getType(); |
| | | if (type == 0) |
| | | type = 5; |
| | | BufferedImage newImage = new BufferedImage(width, height, type); |
| | | Graphics g = newImage.getGraphics(); |
| | | g.drawImage(originalImage, 0, 0, width, height, null); |
| | | g.dispose(); |
| | | return newImage; |
| | | } |
| | | |
| | | // 邀请好友图片 |
| | | public static void inviteFriendImg(InputStream urlInputStream, InputStream portraitInputStream, |
| | | InputStream erCodeInputStream, String targetPath, String inviteCode) throws IOException { |
| | | inviteFriendImg(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, 260, 908, 230, inviteCode); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param urlInputStream |
| | | * @param portraitInputStream |
| | | * @param erCodeInputStream |
| | | * @param targetPath |
| | | * @param pX -二维码的横坐标 |
| | | * @param pY -二维码的纵坐标 |
| | | * @param size -二维码的尺寸 |
| | | * @param inviteCode -邀请码 |
| | | * @throws IOException |
| | | */ |
| | | // 邀请好友图片 |
| | | public static void inviteFriendImg(InputStream urlInputStream, InputStream portraitInputStream, |
| | | InputStream erCodeInputStream, String targetPath, int pX, int pY, int size, String inviteCode) |
| | | throws IOException { |
| | | BufferedImage bgImage = ImageIO.read(urlInputStream); |
| | | 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); |
| | | |
| | | // 画 二维码 |
| | | BufferedImage qrCodeImage = ImageIO.read(erCodeInputStream); |
| | | qrCodeImage = ImageUtil.qrCodeImage(g2d, qrCodeImage, pX, pY, size, size); // 二维码长宽 |
| | | // 230*230 |
| | | |
| | | // 画 头像 |
| | | BufferedImage portraitImg = ImageIO.read(portraitInputStream); |
| | | int portraitSize = size * 5 / 23; |
| | | |
| | | int pPX = pX + size / 2 - portraitSize / 2; |
| | | int pPY = pY + size / 2 - portraitSize / 2; |
| | | portraitImg = ImageUtil.portraitImg(g2d, portraitImg, pPX, pPY, portraitSize, portraitSize);// 头像长宽 |
| | | |
| | | // 画邀请码(邀请码的大小与字体固定) |
| | | if (!StringUtil.isNullOrEmpty(inviteCode)) { |
| | | String st = "邀请码:" + inviteCode; |
| | | int width = bgImage.getWidth(); |
| | | String os = System.getProperty("os.name"); |
| | | String fontBoldPath = "/usr/share/fonts/PingFang_Medium.ttf"; |
| | | if (os.toLowerCase().startsWith("win")) { |
| | | fontBoldPath = "D:/PingFang_Medium.ttf"; |
| | | } |
| | | |
| | | Font boldFont = null; |
| | | try { |
| | | boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); |
| | | } catch (FontFormatException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | g2d.setFont(boldFont); |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | |
| | | FontMetrics fm = g2d.getFontMetrics(boldFont); |
| | | int textLength = fm.stringWidth(st); |
| | | |
| | | int ax = (width - textLength) / 2; |
| | | int ay = bgImage.getHeight() - 92 + boldFont.getSize() / 2 - 5; |
| | | |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | g2d.drawString(st, ax, ay); |
| | | |
| | | } |
| | | |
| | | OutputStream out = new FileOutputStream(new File(targetPath)); |
| | | ImageIO.write(targetImg, "JPEG", out); |
| | | out.flush(); |
| | | out.close(); |
| | | } |
| | | |
| | | // 二维码 |
| | | public static BufferedImage qrCodeImage(Graphics2D g2d, BufferedImage originalImage, int pX, int pY, int width, |
| | | int height) { |
| | | g2d.drawImage(originalImage, pX, pY, width, height, null); |
| | | return originalImage; |
| | | } |
| | | |
| | | // 头像 |
| | | public static BufferedImage portraitImg(Graphics2D g2d, BufferedImage originalImage, int pX, int pY, int width, |
| | | int height) { |
| | | g2d.drawImage(originalImage, pX, pY, width, height, null); |
| | | return originalImage; |
| | | } |
| | | |
| | | public static BufferedImage roundImage(BufferedImage srcImage, int cornerRadius) { // 半径 |
| | | int width = srcImage.getWidth(); |
| | | int height = srcImage.getHeight(); |
| | | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| | | Graphics2D gs = image.createGraphics(); |
| | | 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);// 文字抗锯齿 |
| | | gs.setRenderingHints(mapH); |
| | | gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, cornerRadius, cornerRadius)); |
| | | gs.drawImage(srcImage, 0, 0, null); |
| | | gs.dispose(); |
| | | return image; |
| | | } |
| | | |
| | | public static int saveToImgByInputStream(InputStream inputStream, String imgPath, String imgName) { |
| | | int stateInt = 1; |
| | | try { |
| | | File file = new File(imgPath, imgName);// 可以是任何图片格式.jpg,.png等 |
| | | FileOutputStream fos = new FileOutputStream(file); |
| | | |
| | | FileInputStream fis = (FileInputStream) inputStream; |
| | | |
| | | byte[] b = new byte[1024]; |
| | | int nRead = 0; |
| | | while ((nRead = fis.read(b)) != -1) { |
| | | fos.write(b, 0, nRead); |
| | | } |
| | | fos.flush(); |
| | | fos.close(); |
| | | fis.close(); |
| | | |
| | | } catch (Exception e) { |
| | | stateInt = 0; |
| | | e.printStackTrace(); |
| | | } finally { |
| | | } |
| | | return stateInt; |
| | | } |
| | | |
| | | public static int getTextLengthByWidth(Graphics2D g2d, Font font, String content, int maxWidth, int startPos) { |
| | | FontMetrics fm = g2d.getFontMetrics(font); |
| | | for (int i = startPos; i < content.length(); i++) { |
| | | if (fm.stringWidth(content.substring(0, i)) >= maxWidth) |
| | | return i + 1; |
| | | } |
| | | return content.length(); |
| | | } |
| | | |
| | | public static int[] getImgWidthAndHeight(String imgUrl) throws MalformedURLException, IOException { |
| | | if (StringUtil.isNullOrEmpty(imgUrl)) |
| | | return new int[] { 0, 0 }; |
| | | if (imgUrl.toLowerCase().endsWith("webp")) |
| | | return getImgWidthAndHeightWithWebp(imgUrl); |
| | | else |
| | | return getImgWidthAndHeightWithPngAndJpg(imgUrl); |
| | | } |
| | | |
| | | public static int[] getImgWidthAndHeightWithPngAndJpg(String imgUrl) throws MalformedURLException, IOException { |
| | | InputStream murl = new URL(imgUrl).openStream(); |
| | | BufferedImage sourceImg = ImageIO.read(murl); |
| | | |
| | | int width = sourceImg.getWidth(); |
| | | int height = sourceImg.getHeight(); |
| | | return new int[] { width, height }; |
| | | } |
| | | |
| | | public static int[] getImgWidthAndHeightWithWebp(String imgUrl) throws MalformedURLException, IOException { |
| | | String cacheFile = FileUtil.getCacheDir(); |
| | | String targetPath = cacheFile + "/CACHE_DOWNLOAD_IMG_" + System.currentTimeMillis() + "_" |
| | | + (long) (Math.random() * 1000000000L); |
| | | HttpUtil.downloadFile(imgUrl, targetPath); |
| | | FileInputStream file = new FileInputStream(targetPath); |
| | | byte[] bytes = new byte[30]; |
| | | file.read(bytes, 0, bytes.length); |
| | | int width = ((int) bytes[27] & 0xff) << 8 | ((int) bytes[26] & 0xff); |
| | | int height = ((int) bytes[29] & 0xff) << 8 | ((int) bytes[28] & 0xff); |
| | | file.close(); |
| | | return new int[] { width, height }; |
| | | } |
| | | |
| | | /** |
| | | * 头像加白底图 邀请好友图片 |
| | | * |
| | | * @param urlInputStream |
| | | * @param portraitInputStream |
| | | * @param erCodeInputStream |
| | | * @param targetPath |
| | | * @param inviteCode |
| | | * @throws IOException |
| | | */ |
| | | public static void inviteFriendImgWhite(InputStream urlInputStream, InputStream portraitInputStream, |
| | | InputStream erCodeInputStream, String targetPath, String inviteCode) throws IOException { |
| | | inviteFriendImgWhite(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, 260, 908, 230, |
| | | inviteCode); |
| | | } |
| | | |
| | | /** |
| | | * 头像加白底图 邀请好友图片 -自定义二维码位置 |
| | | * |
| | | * @param urlInputStream |
| | | * @param portraitInputStream |
| | | * @param erCodeInputStream |
| | | * @param targetPath |
| | | * @param inviteCode |
| | | * @throws IOException |
| | | */ |
| | | public static void inviteFriendImgWhitecustom(InputStream urlInputStream, InputStream portraitInputStream, |
| | | InputStream erCodeInputStream, String targetPath, Integer pX, Integer pY, Integer size, String inviteCode) |
| | | throws IOException { |
| | | inviteFriendImgWhite(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, pX, pY, size, |
| | | inviteCode); |
| | | } |
| | | |
| | | /** |
| | | * 邀请好友图片加上头像加上白底 |
| | | */ |
| | | public static void inviteFriendImgWhite(InputStream urlInputStream, InputStream portraitInputStream, |
| | | InputStream erCodeInputStream, String targetPath, Integer pX, Integer pY, Integer size, String inviteCode) |
| | | throws IOException { |
| | | |
| | | if (pX == null) { |
| | | pX = 260; |
| | | } |
| | | |
| | | if (pY == null) { |
| | | pY = 908; |
| | | } |
| | | |
| | | if (size == null) { |
| | | size = 230; |
| | | } |
| | | |
| | | BufferedImage bgImage = ImageIO.read(urlInputStream); |
| | | 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 qrCodeImage = ImageIO.read(erCodeInputStream); |
| | | qrCodeImage = ImageUtil.qrCodeImage(g2d, qrCodeImage, pX, pY, size, size); |
| | | |
| | | int portraitSize = size * 5 / 23; |
| | | int pPX = pX + size / 2 - portraitSize / 2; |
| | | int pPY = pY + size / 2 - portraitSize / 2; |
| | | |
| | | // 头像白色边框 |
| | | g2d.fillRoundRect(pPX - 4, pPY - 4, portraitSize + 8, portraitSize + 8, 5, 5); |
| | | g2d.setRenderingHints(mapH); |
| | | // 画 头像 |
| | | BufferedImage portraitImg = ImageIO.read(portraitInputStream); |
| | | portraitImg = ImageUtil.zoomInImage(portraitImg, portraitSize, portraitSize); |
| | | portraitImg = ImageUtil.roundImage(portraitImg, 10); |
| | | g2d.drawImage(portraitImg, pPX, pPY, portraitSize, portraitSize, null); |
| | | |
| | | // 画邀请码(邀请码的大小与字体固定) |
| | | if (!StringUtil.isNullOrEmpty(inviteCode)) { |
| | | |
| | | int ax = pX - (inviteCode.length() - 4) * 13; |
| | | int ay = pY + size + 118; |
| | | |
| | | int width_diap = 235 + (int) (24.6 * (inviteCode.length() - 4)); |
| | | g2d.setColor(Color.white); |
| | | // 画出一个矩形 |
| | | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.65f)); // 透明度 |
| | | g2d.fillRect(ax, ay - 38, width_diap, 50); |
| | | // 前半圆 |
| | | g2d.fillArc(ax - 25, ay - 38, 50, 50, 90, 180); |
| | | // 后半圆 |
| | | g2d.fillArc(ax + width_diap - 25, ay - 38, 50, 50, 270, 180); |
| | | |
| | | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 1f)); |
| | | String st = "邀请码:" + inviteCode; |
| | | String os = System.getProperty("os.name"); |
| | | String fontBoldPath = "/usr/share/fonts/PingFang_Medium.ttf"; |
| | | if (os.toLowerCase().startsWith("win")) { |
| | | fontBoldPath = "C:/PingFang_Medium.ttf"; |
| | | } |
| | | |
| | | Font boldFont = null; |
| | | try { |
| | | boldFont = Font.createFont(Font.PLAIN, new File(fontBoldPath)).deriveFont(36.0f); |
| | | } catch (FontFormatException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | g2d.setFont(boldFont); |
| | | g2d.getFontMetrics(boldFont); |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | |
| | | g2d.setColor(new Color(0, 0, 0)); |
| | | g2d.drawString(st, ax, ay); |
| | | } |
| | | |
| | | OutputStream out = new FileOutputStream(new File(targetPath)); |
| | | ImageIO.write(targetImg, "JPEG", out); |
| | | out.flush(); |
| | | out.close(); |
| | | } |
| | | |
| | | /** |
| | | * 绘制邀请问候图 |
| | | * |
| | | * @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); |
| | | g2d.setBackground(Color.WHITE); |
| | | |
| | | // 画 蒙层 |
| | | 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 = 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; |
| | | |
| | | // 头像白色边框 |
| | | g2d.fillRoundRect(pPX - 4, pPY - 4, portraitSize + 8, portraitSize + 8, 5, 5); |
| | | g2d.setRenderingHints(mapH); |
| | | |
| | | BufferedImage portraitImg = ImageIO.read(portraitInStream); |
| | | portraitImg = ImageUtil.zoomInImage(portraitImg, portraitSize, portraitSize); |
| | | portraitImg = ImageUtil.roundImage(portraitImg, 10); |
| | | g2d.drawImage(portraitImg, pPX, pPY, portraitSize, portraitSize, null); |
| | | |
| | | // 字体白色 |
| | | 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 = "C:/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(); |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 安卓-支付宝画图 |
| | | * @param money |
| | | * @param orderNo |
| | | * @param time |
| | | * @param targetPath |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static InputStream drawAlipayAndroid(BigDecimal money, String orderNo, String time) throws IOException { |
| | | String bgpic = "image/alipay/android.png"; |
| | | BufferedImage bgImage = ImageIO.read(ImageUtil.class.getClassLoader().getResourceAsStream(bgpic)); |
| | | |
| | | 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.setRenderingHints(mapH); |
| | | g2d.drawImage(bgImage, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null); |
| | | g2d.setBackground(Color.BLACK); |
| | | |
| | | // 字体 |
| | | String os = System.getProperty("os.name"); |
| | | String fontRob0to = "/usr/share/fonts/ROBOTO-REGULAR.TTF"; |
| | | String fontBahnschrift = "/usr/share/fonts/BAHNSCHRIFT.TTF"; |
| | | if (os.toLowerCase().startsWith("win")) { |
| | | fontRob0to = "D:/ROBOTO-REGULAR.TTF"; |
| | | fontBahnschrift = "D:/BAHNSCHRIFT.TTF"; |
| | | } |
| | | |
| | | // 字体大小 |
| | | Font font40 = null; |
| | | Font font42 = null; |
| | | Font font86 = null; |
| | | try { |
| | | font40 = Font.createFont(Font.PLAIN, new File(fontRob0to)).deriveFont(40.00f); |
| | | font42 = Font.createFont(Font.PLAIN, new File(fontBahnschrift)).deriveFont(42.00f); |
| | | font86 = Font.createFont(Font.PLAIN, new File(fontRob0to)).deriveFont(86.68f); |
| | | } catch (FontFormatException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | |
| | | /* 时间信息 */ |
| | | g2d.setFont(font42); |
| | | g2d.setColor(new Color(247, 247, 247)); |
| | | g2d.drawString(TimeUtil.formatHHMM_12H(new Date()), 134, 67); |
| | | |
| | | |
| | | /* 资金信息 */ |
| | | String moneyInfo = "+" + money.setScale(2); |
| | | // 计算长度 |
| | | FontMetrics fm = g2d.getFontMetrics(font86); |
| | | int textLength = fm.stringWidth(moneyInfo); |
| | | // 居中位置 |
| | | int x_money = (bgImage.getWidth() - textLength) / 2 ; |
| | | // 画金额 |
| | | g2d.setFont(font86); |
| | | g2d.setColor(new Color(51, 51, 51)); |
| | | g2d.drawString(moneyInfo, x_money, 420); |
| | | |
| | | |
| | | /* 创建时间信息 */ |
| | | FontMetrics fm2 = g2d.getFontMetrics(font40); |
| | | int l_time = fm2.stringWidth(time); |
| | | int x_time = bgImage.getWidth() - l_time -41 ; |
| | | g2d.setFont(font40); |
| | | g2d.setColor(new Color(51, 51, 51)); |
| | | g2d.drawString(time, x_time, 972); |
| | | |
| | | /* 创建时间信息 */ |
| | | int l_order = fm2.stringWidth(orderNo); |
| | | int x_order = bgImage.getWidth() - l_order -41 ; |
| | | g2d.setFont(font40); |
| | | g2d.setColor(new Color(51, 51, 51)); |
| | | g2d.drawString(orderNo, x_order, 1072); |
| | | g2d.dispose(); |
| | | |
| | | ByteArrayOutputStream aos = new ByteArrayOutputStream(); |
| | | ImageIO.write(targetImg, "png", aos); |
| | | |
| | | return new ByteArrayInputStream(aos.toByteArray()); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * IOS 支付宝画图 |
| | | * @param money |
| | | * @param orderNo |
| | | * @param time |
| | | * @param targetPath |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static InputStream drawAlipayIOS(BigDecimal money, String orderNo, String time) |
| | | throws IOException { |
| | | String bgpic = "image/alipay/ios.png"; |
| | | BufferedImage bgImage = ImageIO.read(ImageUtil.class.getClassLoader().getResourceAsStream(bgpic)); |
| | | |
| | | 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.setRenderingHints(mapH); |
| | | g2d.drawImage(bgImage, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null); |
| | | g2d.setBackground(Color.BLACK); |
| | | |
| | | // 字体 |
| | | String os = System.getProperty("os.name"); |
| | | String fontHeiTi = "/usr/share/fonts/AdobeHeiTiStd.otf"; |
| | | String fontSquare = "/usr/share/fonts/SquareHeitiStd.TTF"; |
| | | if (os.toLowerCase().startsWith("win")) { |
| | | fontHeiTi = "D:/AdobeHeiTiStd.otf"; |
| | | fontSquare = "D:/SquareHeitiStd.TTF"; |
| | | } |
| | | |
| | | // 字体大小 |
| | | Font font40 = null; |
| | | Font font44 = null; |
| | | Font font92 = null; |
| | | try { |
| | | font44 = Font.createFont(Font.PLAIN, new File(fontHeiTi)).deriveFont(44.18f); |
| | | font40 = Font.createFont(Font.PLAIN, new File(fontSquare)).deriveFont(40.00f); |
| | | font92 = Font.createFont(Font.PLAIN, new File(fontSquare)).deriveFont(92.03f); |
| | | } catch (FontFormatException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | /* 时间信息 */ |
| | | g2d.setFont(font44); |
| | | g2d.setColor(new Color(16, 16, 16)); |
| | | MyDrawString(TimeUtil.formatHHMM_12H(new Date()), 82, 82, font44, g2d); |
| | | |
| | | |
| | | /* 资金信息 */ |
| | | String moneyInfo = "+" + money.setScale(2); |
| | | // 计算长度 |
| | | FontMetrics fm = g2d.getFontMetrics(font92); |
| | | int textLength = fm.stringWidth(moneyInfo); |
| | | // 居中位置 |
| | | int x_money = (bgImage.getWidth() - textLength) / 2; |
| | | // 画金额 |
| | | g2d.setFont(font92); |
| | | g2d.setColor(new Color(51, 51, 51)); |
| | | g2d.drawString(moneyInfo, x_money, 480); |
| | | |
| | | /* 创建时间信息 */ |
| | | FontMetrics fm2 = g2d.getFontMetrics(font40); |
| | | int l_time = fm2.stringWidth(time); |
| | | int x_time = bgImage.getWidth() - l_time - 41; |
| | | g2d.setFont(font40); |
| | | g2d.setColor(new Color(51, 51, 51)); |
| | | g2d.drawString(time, x_time, 1052); |
| | | |
| | | /* 创建时间信息 */ |
| | | int l_order = fm2.stringWidth(orderNo); |
| | | int x_order = bgImage.getWidth() - l_order - 41; |
| | | g2d.setFont(font40); |
| | | g2d.setColor(new Color(51, 51, 51)); |
| | | g2d.drawString(orderNo, x_order, 1160); |
| | | |
| | | g2d.dispose(); |
| | | |
| | | ByteArrayOutputStream aos = new ByteArrayOutputStream(); |
| | | ImageIO.write(targetImg, "png", aos); |
| | | return new ByteArrayInputStream(aos.toByteArray()); |
| | | } |
| | | |
| | | public static void MyDrawString(String str,int x,int y, Font font40, Graphics2D g2d){ |
| | | int i = 0; |
| | | int x_next = x; |
| | | String tempStr= null; |
| | | while(str.length()>0) |
| | | { |
| | | tempStr=str.substring(0, 1); |
| | | str=str.substring(1, str.length()); |
| | | g2d.drawString(tempStr, x_next, y); |
| | | |
| | | if (i == 1) { |
| | | x_next += 3 + 22; |
| | | } else if (i == 2) { |
| | | x_next += 10; |
| | | } else { |
| | | x_next += 0 + 22; |
| | | } |
| | | i++; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 拉新海报 |
| | | * @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; |
| | | } |
| | | |
| | | } |