使用一个模板图片,在图片上添加图片或者文字都可以。
依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<optional>true</optional>
</dependency>
封装数据类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.awt.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PositionPO {
private Object data;
private float x;
private float y;
private float w;
private float h;
private Font font;
public PositionPO(Object data, float x, float y, float w, float h) {
this.data = data;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public PositionPO(Object data, float x, float y) {
this.data = data;
this.x = x;
this.y = y;
}
public PositionPO(Object data, float x, float y, Font font) {
this.data = data;
this.x = x;
this.y = y;
this.font = font;
}
public PositionPO(float x, float y, float w, float h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
}
import com.yt.distributor.po.pdf.PositionPO;
import lombok.Data;
import java.util.List;
@Data
public class ImageHandlePO {
private List<PositionPO> textList;
private List<PositionPO> imageList;
public ImageHandlePO(List<PositionPO> textList, List<PositionPO> imageList) {
this.textList = textList;
this.imageList = imageList;
}
}
常量类
package com.yt.distributor.constant;
import org.springframework.core.io.ClassPathResource;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class ImageConstant {
public static final float PELLUCIDITY = 1.0F;
public static final Font FONT = new Font("微软雅黑", Font.BOLD, 18);
public static File POSTER_SOURCE_FILE;
public static final String FORMAT = "png";
static{
try {
ClassPathResource resource = new ClassPathResource("conf/poster.jpg");
POSTER_SOURCE_FILE = resource.getFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
图像处理类
import com.yt.distributor.constant.ImageConstant;
import com.yt.distributor.po.img.ImageHandlePO;
import com.yt.distributor.po.pdf.PositionPO;
import lombok.extern.log4j.Log4j2;
import net.dreamlu.mica.core.utils.$;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@Log4j2
public class PictureSynthesis {
public static final Object FLAG = true;
public static File sourceFile;
public static void main(String[] args) throws IOException {
BufferedImage image = QrCodeGenerator.generateQrCode("http://www.baiud.com/index.html?id=13", 192, 192);
List<PositionPO> imageList = new ArrayList<>();
imageList.add(new PositionPO(ImageIO.read(new URL("https://thirdwx.qlogo.cn/mmopen/vi_32/AtTHbmrMict69vB7ocDMbstibgvwxpK51bOoNkQiaemrImnicUK2L9OoF1JibHiceLwY53ibiaicJQibuEwLNFicJiaYcQHRiaw/132")), 120F, 1688F, 192F, 192F));
imageList.add(new PositionPO(image, 785F, 1632F, 192F , 192F));
Font font = new Font("微软雅黑", Font.PLAIN, 30);
List<PositionPO> textList = new ArrayList<>();
textList.add(new PositionPO("颜魔子辰", 120F, 1660F, font));
textList.add(new PositionPO("颜魔子辰邀请您", 336F, 1758F, font));
textList.add(new PositionPO("加入某某小店。", 336F, 1796F, font));
textList.add(new PositionPO("长按可识别二维码", 760F, 1880F, font));
String sourcePath = "C:\\Users\\Administrator\\Desktop\\poster.jpg";
String savePath = "C:\\Users\\Administrator\\Desktop\\poster-handle.jpg";
handleImage(new ImageHandlePO(textList, imageList), new File(sourcePath), savePath);
}
public static InputStream handleImage(ImageHandlePO po, File sourceFile) throws IOException {
synchronized (FLAG) {
PictureSynthesis.sourceFile = sourceFile;
BufferedImage image = watermark(po);
return getInputStream(image);
}
}
public static void handleImage(ImageHandlePO po, File sourceFile, String saveFilePath) throws IOException {
synchronized (FLAG) {
PictureSynthesis.sourceFile = sourceFile;
BufferedImage buffImg = watermark(po);
generateWaterFile(buffImg, saveFilePath);
}
}
private static BufferedImage watermark(ImageHandlePO po) throws IOException {
BufferedImage buffImg = ImageIO.read(sourceFile);
Graphics2D g2d = buffImg.createGraphics();
if ($.isNotEmpty(po.getTextList())){
for (PositionPO pp : po.getTextList()){
g2d.setColor(Color.black);
g2d.setFont( pp.getFont() == null ? ImageConstant.FONT : pp.getFont());
g2d.drawString(pp.getData().toString(), pp.getX(), pp.getY());
}
}
if ($.isNotEmpty(po.getImageList())){
for (PositionPO pp : po.getImageList()){
BufferedImage image = (BufferedImage) pp.getData();
int width = pp.getW() <= 0 ? image.getWidth() : (int) pp.getW();
int height = pp.getH() <= 0 ? image.getHeight() : (int) pp.getH();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ImageConstant.PELLUCIDITY));
g2d.drawImage(image, (int)pp.getX(), (int)pp.getY(), width, height, null);
}
}
g2d.dispose();
return buffImg;
}
private static void generateWaterFile(BufferedImage buffImg, String savePath) {
int temp = savePath.lastIndexOf(".") + 1;
try {
ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
} catch (IOException e1) {
e1.printStackTrace();
}
}
private static void getFonts(){
String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
for(String fontName:fontNames){
System.out.println(fontName);
}
}
public static InputStream getInputStream(BufferedImage image){
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(image, ImageConstant.FORMAT, os);
return new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
log.error("提示:",e);
}
return null;
}
}
效果图,以上的数据都是按图片的1080*1920像素来设定的,下面红框部分是动态生成的。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ae837257c585d97630deec2a7dd5eacc.png)