仿微信九宫格(从1-9个头像)
取前五个用户头像生成一个新的头像(从1-5个头像)
可自定义算法做头像拼接
效果图如下
package com.example.demo.util;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @className UpdateGroupLogoUtil
* @description TODO
*/
public class UpdateGroupLogoUtil {
/**
* 图片格式:JPG
*/
private static final String PICTRUE_FORMATE = "jpg";
/**
* 生成组合头像
* 画布宽度和高度为166,图片间距为2
*
* @param paths 用户头像路径列表
* @param outPath 生成后的头像保存路径
* @throws IOException
*/
public static void generate(List<String> paths, String outPath) throws IOException {
generate(paths, 166, 4, outPath);
}
/**
* 生成组合头像
*
* @param paths 用户头像路径列表
* @param length 画板的宽高和高度
* @param interval 画板中的图片间距
* @param outPath 生成后的头像保存路径
* @throws IOException
*/
public static void generate(List<String> paths, int length, int interval, String outPath) throws IOException {
//圆形五图
int wh = (length - interval * 4) / 2; // 每个图片的宽高和高度:图片数>4
//微信9宫格
//int wh = (length - interval * 4) / 3; // 每个图片的宽高和高度:图片数>4
if (paths.size() == 1) {
wh = length - interval * 2; // 每个图片的宽高和高度:图片数=1
}
if (paths.size() > 1 && paths.size() < 5) {
wh = (length - interval * 3) / 2; // 每个图片的宽高和高度:图片数>0并且<5
}
List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
for (int i = 0; i < paths.size(); i++) {
bufferedImages.add(UpdateGroupLogoUtil.resize(paths.get(i), wh, wh, false));
}
// BufferedImage.TYPE_INT_RGB可以自己定义可查看API
BufferedImage outImage = new BufferedImage(length, length, BufferedImage.TYPE_INT_RGB);
// 生成画布
Graphics g = outImage.getGraphics();
Graphics2D g2d = (Graphics2D) g;
// 设置背景色
g2d.setBackground(new Color(255, 255, 255));
// 通过使用当前绘图表面的背景