package com.legend.core.util;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.legend.core.config.WordPath;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* 生成分享好友
*
* @author 生成分享好友
*/
@Service
@Slf4j
public class ImageUtil {
//我把生成海报地址的路径配置到了springboot的yml配置文件中了
@Resource
private WordPath wordPath;
/**
* 生成海报
*
* @param content 二维码内容
* @param written 文字内容
* @param filePath 保存文件 例:1.png (d:/1.png)
* @param hbPath 海报图片地址 例:1.png (d:/1.png)
* @param logoPath 二维码logo
* @return
* @author Uncle
* @Description 在一张背景图上添加二维码
* @Date 2020-09-28 23:59
*/
public String drawString(String content, String written, String filePath, File hbPath, File logoPath) {
try {
BufferedImage image = addWater(content, hbPath, logoPath);
Graphics2D gd = image.createGraphics();
// 3、设置对线段的锯齿状边缘处理
gd.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// 5、设置水印文字颜色
gd.setColor(Color.darkGray);
// 6、设置水印文字Font
gd.setFont(new Font("苹方", Font.PLAIN, 32));
// 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
gd.drawString(written, 440, 1122);
gd.dispose();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(image, "png", imOut);
InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
// 获取yml海报的配置
String file = wordPath.getPosterPath() + filePath + ".png";
if (!new File(wordPath.getPosterPath()).exists()) {
new File(wordPath.getPosterPath()).mkdirs();
}
OutputStream outStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outStream);
inputStream.close();
outStream.close();
// 返回文件地址
return file;
} catch (Exception e) {
log.error("海报生成失败:", e);
}
return null;
}
/***
* 在一张背景图上添加二维码
*/
public BufferedImage addWater(String content, File hbPath, File logoPath) throws Exception {
// 读取原图片信息
//得到文件
//File file = new File(hbPath);
//文件转化为图片
Image srcImg = ImageIO.read(hbPath);
//获取图片的宽
int srcImgWidth = srcImg.getWidth(null);
//获取图片的高
int srcImgHeight = srcImg.getHeight(null);
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
//使用工具类生成二维码
Image image = createQrCode(content, 230, 230, logoPath);
//将小图片绘到大图片上,500,300 .表示你的小图片在大图片上的位置。
g.drawImage(image, 25, 1070, null);
//设置颜色。
g.setColor(Color.WHITE);
g.dispose();
return bufImg;
}
private BufferedImage createQrCode(String content, int width, int height, File logoPath) throws IOException {
QrConfig config = new QrConfig(width, height);
if (logoPath != null) {
Image image = ImageIO.read(new FileInputStream(logoPath));
config.setImg(image);
}
config.setErrorCorrection(ErrorCorrectionLevel.H);
return QrCodeUtil.generate(
content,
config);
}
public InputStream resourceLoader(String fileFullPath) throws IOException {
ResourceLoader resourceLoader = new DefaultResourceLoader();
return resourceLoader.getResource(fileFullPath).getInputStream();
}
}
|