Java根据Hash生成用户头像


#1

Java根据Hash生成用户头像

Github 等很多应用。在用户注册后,都会为你随机生成一个头像。非常的简约。实用。

AvatarHelper

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Base64;
import java.util.Random;

import javax.imageio.ImageIO;

public class AvatarHelper {
	
	// 可以直接在<img/>标签或者浏览器地址栏预览的base64编码头
	public static final String BASE64_PREFIX = "data:image/png;base64,";

	/**
	 * 生成头像的base64编码
	 * @param id
	 * @return
	 * @throws IOException
	 */
	public static String createBase64Avatar(int id) throws IOException {
		return new String(Base64.getEncoder().encode(create(id)));
	}

	/**
	 * 根据id生成一个头像,颜色随机。如果是使用hashCode()值的话,值可能为负数。需要要注意。
	 * @param id
	 * @return
	 * @throws IOException
	 */
	public static byte[] create(int id) throws IOException {
		int width = 20;
		int grid = 5;
		int padding = width / 2;
		int size = width * grid + width;
		BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
		Graphics2D _2d = img.createGraphics();
		_2d.setColor(new Color(240, 240, 240));
		_2d.fillRect(0, 0, size, size);
		_2d.setColor(randomColor(80, 200));
		char[] idchars = createIdent(id);
		int i = idchars.length;
		for (int x = 0; x < Math.ceil(grid / 2.0); x++) {
			for (int y = 0; y < grid; y++) {
				if (idchars[--i] < 53) {
					_2d.fillRect((padding + x * width), (padding + y * width), width, width);
					if (x < Math.floor(grid / 2)) {
						_2d.fillRect((padding + ((grid - 1) - x) * width), (padding + y * width), width, width);
					}
				}
			}
		}
		_2d.dispose();
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ImageIO.write(img, "png", byteArrayOutputStream);
		return byteArrayOutputStream.toByteArray();
	}

	private static Color randomColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255) {
			fc = 255;
		}
		if (bc > 255) {
			bc = 255;
		}
		int r = fc + random.nextInt(Math.abs(bc - fc));
		int g = fc + random.nextInt(Math.abs(bc - fc));
		int b = fc + random.nextInt(Math.abs(bc - fc));
		return new Color(r, g, b);
	}

	private static char[] createIdent(int id) {
		BigInteger bi_content = new BigInteger((id + "").getBytes());
		BigInteger bi = new BigInteger(id + "identicon" + id, 36);
		bi = bi.xor(bi_content);
		return bi.toString(10).toCharArray();
	}
}

生成一个试试看

public class Main {
	public static void main(String[] args) throws Exception {
		String avatar = AvatarHelper.createBase64Avatar(Math.abs("springboot.io".hashCode()));
		System.out.println(AvatarHelper.BASE64_PREFIX + avatar);
	}
}

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAIAAAC2BqGFAAAA7ElEQVR42u3ZUQ0AIAhAQTtbwzzmIoIddNOB98a/cL+20JUaAtCgBRo0aIEGLdBJoUef23OyVr13QYMGDRo0aNCgQYMGDRo0aNCgQYMGDRo0aNCgQYMGnSHQoEGDBg0aNGjQoEGDBg0aNGjQoEGDBg0aNGjQoEFXP/hVoEGDBg0aNGjQoEGDBg0aNGjQoEGDBg0aNGjQoEGDBg0aNGjQoEGDBg0aNGjQoEGDBg0aNGjQoEGDBg0adD7ok4N/G9CgQYMGDRo0aNCgQYMGDRo0aNCgQYMGDRo0aNCgK0Kr+C84aIEGLdCgQQs0aEUsQEqByPsi9i0AAAAASUVORK5CYII=

复制到浏览器地址栏可以直接预览

代码摘抄自某个博客,具体忘记了。如果你知道该代码出处,请告之。我会在文章中注明出处。:grin: