Java 生成微信群头像 九宫格头像

  • Post author:
  • Post category:java


通过获取用户网络路径地址的 头像.生成一张网络路径的九宫格头像. dir是网络服务器路径
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;

/**
 * 该类是图片处理类
 * 
 * @author 
 * 
 */
public final class ImageUtil {
    /** 图片格式:JPG */
    private static final String PICTRUE_FORMATE_JPG = "jpg";

    private ImageUtil() {
    }
/**
     * 生成组合头像
     * 
     * @param paths
     *            用户图像
 * @param dir 
 * @param groupId 
 * @return 
     * @throws IOException
     */
    public static boolean getCombinationOfhead(List<String> paths, String dir, String groupId)
            throws IOException {
        List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
        // 压缩图片所有的图片生成尺寸同意的 为 50x50
        for (int i = 0; i < paths.size(); i++) {
        	BufferedImage resize2 = ImageUtil.resize2(paths.get(i), 40, 40, true);
            bufferedImages.add(resize2);
        }

        int width = 112; // 这是画板的宽高

        int height = 112; // 这是画板的高度

        // 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,



版权声明:本文为lliuhongyang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。