spring boot 生成群聊头像

作者: 就怕是个demo | 来源:发表于2017-05-18 17:57 被阅读551次

    代码来自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

    相关文章

      网友评论

      本文标题:spring boot 生成群聊头像

      本文链接:https://www.haomeiwen.com/subject/wkasxxtx.html