spring boot 生成群聊头像

代码来自http://www.cnblogs.com/zovon/p/4345501.html
按着项目需求改动一下,做个笔记

  • ImageUtil.java
package com.md.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by CrazyMouse on 2017/5/18.
 */
public final class ImageUtil {
    /**图片格式: JPG*/
    private static final String PICTRUE_FORMATE_JPG = "jpg";

    public ImageUtil() {
    }

    /**
     * 生成组合头像
     * @param paths 用户头像地址
     * @throws IOException
     */
    public static void getCombinationOfHead(List<String> paths, String outputImage) throws IOException {
        List<BufferedImage> bufferedImages = new ArrayList<>();
        // 压缩图片所有的图片生成尺寸
        int imgWidth = 0;
        int imgHeight = 0;
        if (paths.size() == 1) {
            imgWidth = 180;
            imgHeight = 180;
        } else if (paths.size() >= 2 && paths.size() <= 4) {
            imgWidth = 90;
            imgHeight = 90;
        } else if (paths.size() > 4) {
            imgWidth = 60;
            imgHeight = 60;
        }
        for (int i = 0; i < paths.size(); i++) {
            bufferedImages.add(ImageUtil.resize(paths.get(i), imgHeight, imgWidth, true));
        }

        int width = 180; // 画板的宽高
        int height = 180; // 画板的高度
        // BufferedImage.TYPE_INT_RGB可以自己定义可查看API
        BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 生成画布
        Graphics g = outImage.getGraphics();
        Graphics2D g2d = (Graphics2D) g;
        // 设置背景色
        g2d.setBackground(new Color(231,231,231));
        // 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
        g2d.clearRect(0, 0, width, height);
        // 开始拼凑 根据图片的数量判断该生成那种样式的组合头像目前为4中
        for (int i = 1; i <= bufferedImages.size(); i++) {
            if (bufferedImages.size() == 9) {
                if (i <= 3) {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-1), 0, null);
                } else if(i > 3 && i <= 6) {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-4), 60, null);
                } else {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-7), 120, null);
                }
            } else if (bufferedImages.size() == 8) {
                if (i <= 3) {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-1), 0, null);
                } else if(i > 3 && i <= 6) {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-4), 60, null);
                } else {
                    g2d.drawImage(bufferedImages.get(i - 1), 30 + 60 * (i-7), 120, null);
                }
            } else if (bufferedImages.size() == 7) {
                if (i <= 3) {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-1), 0, null);
                } else if(i > 3 && i <= 6) {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-4), 60, null);
                } else {
                    g2d.drawImage(bufferedImages.get(i - 1), 60, 120, null);
                }
            } else if (bufferedImages.size() == 6) {
                if (i <= 3) {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-1), 30, null);
                } else {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-4), 90, null);
                }
            } else if (bufferedImages.size() == 5) {
                if (i <= 3) {
                    g2d.drawImage(bufferedImages.get(i - 1), 60 * (i-1), 30, null);
                } else {
                    g2d.drawImage(bufferedImages.get(i - 1), 30 + 60 * (i-4), 90, null);
                }
            } else if (bufferedImages.size() == 4) {
                if (i <= 2) {
                    g2d.drawImage(bufferedImages.get(i - 1), 90 * (i-1), 0, null);
                } else {
                    g2d.drawImage(bufferedImages.get(i - 1), 90 * (i-3), 90, null);
                }
            } else if (bufferedImages.size() == 3) {
                if (i == 3) {
                    g2d.drawImage(bufferedImages.get(i - 1), 45, 90, null);
                } else {
                    g2d.drawImage(bufferedImages.get(i - 1), 90 * (i-1), 0, null);
                }
            } else if (bufferedImages.size() == 2) {
                g2d.drawImage(bufferedImages.get(i - 1), 90 * (i-1), 45, null);
            } else if (bufferedImages.size() == 1) {
                g2d.drawImage(bufferedImages.get(i - 1), 0, 0, null);
            }
            // 需要改变颜色的话在这里绘上颜色。可能会用到AlphaComposite类
        }
        String outPath = "/Users/CrazyMouse/Desktop/temp/"+outputImage+".jpg";
        String format = "JPG";
        ImageIO.write(outImage, format, new File(outPath));
    }

    /**
     * 图片缩放
     * @param filePath
     * @param height
     * @param width
     * @param bb 比例不对时是否需要补白
     * @return
     */
    public static BufferedImage resize(String filePath, int height, int width, boolean bb) {
        try {
            double ratio = 0; // 缩放比例
            File f = new File(filePath);
            BufferedImage bi = ImageIO.read(f);
            Image itemp = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            // 计算比例
            if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
                if (bi.getHeight() > bi.getWidth()) {
                    ratio = (new Integer(height)).doubleValue() / bi.getHeight();
                } else {
                    ratio = (new Integer(width)).doubleValue() / bi.getWidth();
                }
                AffineTransformOp op = new AffineTransformOp(
                    AffineTransform.getScaleInstance(ratio, ratio), null);
                itemp = op.filter(bi, null);
            }
            if (bb) {
                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics2D g = image.createGraphics();
                g.setColor(Color.white);
                g.fillRect(0, 0, width, height);
                if (width == itemp.getWidth(null)) {
                    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
                            itemp.getWidth(null), itemp.getHeight(null),
                            Color.white, null);
                } else {
                    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
                            itemp.getWidth(null), itemp.getHeight(null),
                            Color.white, null);
                }
                g.dispose();
                itemp = image;
            }
            return (BufferedImage) itemp;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
  • test
package com.md;

import com.md.utils.ImageUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CrazyMouse on 2017/5/18.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ImageUtilTest {

    @Test
    public void testOneImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 1; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image1");
    }

    @Test
    public void testTwoImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 2; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image2");
    }

    @Test
    public void testThreeImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 3; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image3");
    }

    @Test
    public void testFourImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image4");
    }

    @Test
    public void testFiveImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image5");
    }

    @Test
    public void testSixImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 6; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image6");
    }

    @Test
    public void testSevenImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image7");
    }

    @Test
    public void testEightImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image8");
    }

    @Test
    public void testNineImage() throws Exception {
        List<String> paths = new ArrayList<>();
        for (int i = 1; i <= 9; i++) {
            paths.add("/Users/CrazyMouse/Desktop/temp/img"+i+".jpg");
        }
        ImageUtil.getCombinationOfHead(paths, "image9");
    }
}
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • 前端知识体系http://www.cnblogs.com/sb19871023/p/3894452.html 前端...
    秋风喵阅读 12,301评论 7 163
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,056评论 25 707
  • 后来在25岁那年我终于成长起来了,果然使人成熟的不是岁月而是经历。这两个月的大起大落比我过去20年都更要波...
    梦梦梦梦happy阅读 448评论 0 0
  • 校园“朵朵白” 文/田晨语(四年级) 我们的学校里有许多美景,有郁郁葱葱的花草树木,有平坦宽阔的大操场,有古色古香...
    doudouli阅读 562评论 0 0
  • 今天在朋友家,又一次目睹了她与她的妈妈的对话如下: 朋友:你什么时候回来,都一点多了,吃不吃饭了?(有点烦躁,不耐...
    甲乙丙JYB阅读 296评论 0 1