java 拼接头像9宫格
程序员文章站
2022-06-13 21:03:40
...
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<String>
* @param message String
* @param outPutPath String
* @throws IOException
* return String
*/
public static String overlapImage(List<String> 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<BufferedImage> 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<bufferedImageList.size();i++){
if(i<3){//012
graphics.drawImage(bufferedImageList.get(i), sonImageWithAndHeight*i+dividerWithAndHeight*i+dividerWithAndHeight, dividerWithAndHeight, sonImageWithAndHeight, sonImageWithAndHeight, null);
}else if(i<6){//345
graphics.drawImage(bufferedImageList.get(i), sonImageWithAndHeight*(i%3)+dividerWithAndHeight*(i%3)+dividerWithAndHeight, sonImageWithAndHeight+dividerWithAndHeight*2, sonImageWithAndHeight, sonImageWithAndHeight, null);
}else if(i<9){//678
graphics.drawImage(bufferedImageList.get(i), sonImageWithAndHeight*(i%6)+dividerWithAndHeight*(i%6)+dividerWithAndHeight, sonImageWithAndHeight*2+dividerWithAndHeight*3, sonImageWithAndHeight, sonImageWithAndHeight, null);
}
}
}
}
graphics.dispose();
// 输出新的图片
ImageIO.write(backgroundImage, "png", new File(outPutPath));
return outPutPath;
}
/**
* 重新设置图片大小
*
* @param width
* @param height
* @param bufferedImage
* @return
*/
private static BufferedImage resizeImage(int width, int height, BufferedImage bufferedImage) {
BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
newBufferedImage.getGraphics().drawImage(bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
return newBufferedImage;
}
/**
* 图片设置圆角 (画白边)
* @param srcImage BufferedImage
* @param radius Int
* @param border Int
* @param padding Int
* @return BufferedImage
* @throws IOException
*/
private static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding) throws IOException{
int width = srcImage.getWidth();
int height = srcImage.getHeight();
int canvasWidth = width + padding * 2;
int canvasHeight = height + padding * 2;
BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gs = image.createGraphics();
gs.setComposite(AlphaComposite.Src);
gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gs.setColor(Color.WHITE);
gs.fill(new RoundRectangle2D.Float(0, 0, canvasWidth, canvasHeight, radius, radius));
gs.setComposite(AlphaComposite.SrcAtop);
gs.drawImage(setClip(srcImage, radius), padding, padding, null);
if(border !=0){
gs.setColor(Color.GRAY);
gs.setStroke(new BasicStroke(border));
gs.drawRoundRect(padding, padding, canvasWidth - 2 * padding, canvasHeight - 2 * padding, radius, radius);
}
gs.dispose();
return image;
}
/**
* 图片设置圆角
* @param srcImage BufferedImage
* @return BufferedImage
* @throws IOException
*/
private static BufferedImage setRadius(BufferedImage srcImage) throws IOException{
int radius = (srcImage.getWidth() + srcImage.getHeight()) / 6;
return setRadius(srcImage, radius, 2, 5);
}
/**
* 图片切圆角
* 圆角矩形:
* RoundRectangle2D rectRound = new RoundRectangle2D.Double(20,30,130,100,18,15);//左上角是(20,30),宽是130,高是100,圆角的长轴是18,短轴是15。
*
* @param srcImage BufferedImage
* @param radius radius
* @return BufferedImage
*/
private static BufferedImage setClip(BufferedImage srcImage, int radius){
int width = srcImage.getWidth();
int height = srcImage.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D gs = image.createGraphics();
gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
gs.drawImage(srcImage, 0, 0, null);
gs.dispose();
return image;
}
}
2、调用:
List<String> 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