java后台拼接9宫格头像和添加水印文字,效果:
参考:https://blog.csdn.net/shichen88/article/details/101293541
1、拼接工具类:
package com.bai.blog.utils.files;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 拼接图片 参考:https://blog.csdn.net/shichen88/article/details/101293541
* FileName: FileSplitUtil
* Create by: 漠天
* Date: 2019/12/23
*/
public class FileSplitUtil {
/**
* 合成图片
*
* @param imagePaths List
* @param message String
* @param outPutPath String
* @throws IOException
* return String
*/
public static String overlapImage(List imagePaths, String message, String outPutPath) throws IOException {
int withAndHeight = 530;//背景图片的宽高
int dividerWithAndHeight = 20;//divider分割空白宽高
int sonImageWithAndHeight = 150;//画的子头像图片的宽高
int sonImageRadius= 30;//画的子头像的圆角
// 设置背景图片大小,自己添加替换图片逻辑
// BufferedImage backgroundImage = resizeImage(566, 230, ImageIO.read(new File(backgroundPath)));
//new一个白色背景板
BufferedImage backgroundImage = new BufferedImage(withAndHeight,withAndHeight,BufferedImage.TYPE_INT_ARGB);
backgroundImage.getGraphics().setColor(Color.white);
backgroundImage.getGraphics().fillRect(0,0,withAndHeight,withAndHeight);
backgroundImage.getGraphics().dispose();
backgroundImage = setClip(backgroundImage,30);
// 初始化有效的图片BufferedImage列表
List bufferedImageList = new ArrayList<>();
for(String imagePath:imagePaths){
bufferedImageList.add(setClip(resizeImage(sonImageWithAndHeight, sonImageWithAndHeight, ImageIO.read(new File(imagePath))),sonImageRadius));
}
// 获取背景画布
Graphics2D graphics = backgroundImage.createGraphics();
// 在背景图片上添加文字
graphics.setColor(Color.blue);
graphics.setFont(new Font("宋体", Font.PLAIN, 20));
graphics.drawString(message, sonImageWithAndHeight/3, sonImageWithAndHeight/3);
// 画出旋转的文字,在指定位置
Font font = new Font("宋体", Font.PLAIN, 80);
AffineTransform affineTransform = new AffineTransform();
affineTransform.rotate(-Math.toRadians(45), 50, 20);
Font rotatedFont = font.deriveFont(affineTransform);
graphics.setFont(rotatedFont);
graphics.drawString(message, sonImageWithAndHeight, withAndHeight-sonImageWithAndHeight);
// 在背景图片上添加子图片
if(bufferedImageList.size()>0){
if(bufferedImageList.size()<2){
graphics.drawImage(bufferedImageList.get(0), dividerWithAndHeight, dividerWithAndHeight, withAndHeight-dividerWithAndHeight*2, withAndHeight-dividerWithAndHeight*2, null);
}else if(bufferedImageList.size()<3){
int newSonWidth = withAndHeight/2-dividerWithAndHeight*2;
int centerPlex =(withAndHeight-newSonWidth)/2;
graphics.drawImage(bufferedImageList.get(0), dividerWithAndHeight+dividerWithAndHeight/2, centerPlex, newSonWidth, newSonWidth, null);
graphics.drawImage(bufferedImageList.get(1), newSonWidth+dividerWithAndHeight*2+dividerWithAndHeight/2, centerPlex, newSonWidth, newSonWidth, null);
}else if(bufferedImageList.size()<10){
for(int i=0;i
2、调用:
List formatFilePath = new ArrayList<>();
formatFilePath.add("D:/JavaLibFiles/Files/1.png");
formatFilePath.add("D:/JavaLibFiles/Files/2.png");
formatFilePath.add("D:/JavaLibFiles/Files/3.png");
formatFilePath.add("D:/JavaLibFiles/Files/4.png");
formatFilePath.add("D:/JavaLibFiles/Files/5.png");
formatFilePath.add("D:/JavaLibFiles/Files/6.png");
formatFilePath.add("D:/JavaLibFiles/Files/7.png");
String outPutPath = "D:/JavaLibFiles/Files/splitImage.png";
String message = "测试拼接信息";
String splitResultImagePath = FileSplitUtil.overlapImage(formatFilePath,message,outPutPath);
// 结果地址:splitResultImagePath