package com.yeshi.makemoney.app.utils;
|
|
import com.google.zxing.WriterException;
|
import jdk.internal.util.xml.impl.Input;
|
import org.yeshi.utils.FileUtil;
|
import org.yeshi.utils.QRCodeUtil;
|
|
import javax.imageio.ImageIO;
|
import java.awt.*;
|
import java.awt.image.BufferedImage;
|
import java.io.*;
|
import java.util.HashMap;
|
|
/**
|
* @author hxh
|
* @title: ImageUtil
|
* @description: 绘图工具类
|
* @date 2022/4/9 12:43
|
*/
|
public class DrawImageUtil {
|
|
public static InputStream drawInvite(InputStream bg, String inviteCode, String link) throws Exception {
|
BufferedImage bgImage = ImageIO.read(bg);
|
final BufferedImage targetImg = new BufferedImage(bgImage.getWidth(), bgImage.getHeight(),
|
BufferedImage.TYPE_INT_RGB);
|
|
HashMap<RenderingHints.Key, Object> mapH = new HashMap<RenderingHints.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);
|
|
Font font = null;
|
//画邀请码
|
String st = "我的邀请码:" + inviteCode;
|
try {
|
font = Font.createFont(Font.PLAIN, DrawImageUtil.class.getClassLoader().getResourceAsStream("fonts/yahei.ttf")).deriveFont(36.0f);
|
} catch (FontFormatException e) {
|
e.printStackTrace();
|
}
|
g2d.setFont(font);
|
g2d.setColor(new Color(255, 255, 255));
|
|
|
FontMetrics fm = g2d.getFontMetrics(font);
|
int textLength = fm.stringWidth(st);
|
|
|
int ax = (bgImage.getWidth() - textLength) / 2;
|
int ay = bgImage.getHeight() - bgImage.getWidth() * 8 / 15 + font.getSize();
|
|
g2d.fillRoundRect(ax - 40, ay - 20 - font.getSize(), textLength + 40 * 2, font.getSize() + 25 * 2, font.getSize() + 25 * 2, font.getSize() + 25 * 2);
|
|
g2d.setColor(new Color(254, 0, 62));
|
g2d.drawString(st, ax, ay);
|
|
|
//画二维码背景
|
g2d.setColor(new Color(255, 255, 255));
|
|
ay = ay - 15 - font.getSize() + (font.getSize() + 17 * 2) * 7 / 4;
|
|
g2d.fillRoundRect(ax - 40, ay, textLength + 40 * 2, textLength + 30 * 2, font.getSize() + 17 * 2, font.getSize() + 17 * 2);
|
|
|
BufferedImage code = QRCodeUtil.getInstance(500, 500).createImageDeleteWhite(link, null, 0xFFFE003E, 0xFFFFFFFF, true);
|
|
|
g2d.drawImage(zoomInImage(code, textLength, textLength), null, ax, ay + 30);
|
|
|
ByteArrayOutputStream aos = new ByteArrayOutputStream();
|
ImageIO.write(targetImg, "JPEG", aos);
|
return new ByteArrayInputStream(aos.toByteArray());
|
}
|
|
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;
|
}
|
|
}
|