html 九宫格头像,JAVA-仿微信九宫格头像

bufferedImages = new ArrayList();

int imageSize = 0;

if (paths.size() <= 1) {

//若为一张图片

imageSize = ONE_IMAGE_SIZE;

} else if (paths.size() > 1 && paths.size() < 5) {

//若为2-4张图片

imageSize = TWO_IMAGE_SIZE;

} else {

//若>=5张图片

imageSize = FIVE_IMAGE_SIZE;

}

for (int i = 0; i < paths.size(); i++) {

BufferedImage resize2 = ImageUtil.resize2(paths.get(i), imageSize, imageSize, true);

bufferedImages.add(resize2);

}

BufferedImage outImage = new BufferedImage(CANVANS_W, CANVANS_H, BufferedImage.TYPE_INT_RGB);

// 生成画布

Graphics g = outImage.getGraphics();

Graphics2D g2d = (Graphics2D) g;

// 设置背景色

g2d.setBackground(new Color(231, 231, 231));

// 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。

g2d.clearRect(0, 0, CANVANS_W, CANVANS_H);

// 开始拼凑 根据图片的数量判断该生成那种样式的组合头像目前为九种

for (int i = 1; i <= bufferedImages.size(); i++) {

Integer size = bufferedImages.size();

switch (size) {

case 1:

g2d.drawImage(bufferedImages.get(i - 1), SIDE, SIDE, null);

break;

case 2:

if (i == 1) {

g2d.drawImage(bufferedImages.get(i - 1), SIDE, (CANVANS_W - imageSize) / 2, null);

} else {

g2d.drawImage(bufferedImages.get(i - 1), 2 * SIDE + imageSize, (CANVANS_W - imageSize) / 2, null);

}

break;

case 3:

if (i == 1) {

g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - imageSize) / 2, SIDE, null);

} else {

g2d.drawImage(bufferedImages.get(i - 1), (i - 1) * SIDE + (i - 2) * imageSize, imageSize + (2 * SIDE), null);

}

break;

case 4:

if (i <= 2) {

g2d.drawImage(bufferedImages.get(i - 1), i * SIDE + (i - 1) * imageSize, SIDE, null);

} else {

g2d.drawImage(bufferedImages.get(i - 1), (i - 2) * SIDE + (i - 3) * imageSize, imageSize + 2 * SIDE, null);

}

break;

case 5:

if (i <= 2) {

g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2 + (i - 1) * imageSize + (i - 1) * SIDE, (CANVANS_W - 2 * imageSize - SIDE) / 2, null);

} else {

g2d.drawImage(bufferedImages.get(i - 1), (i - 2) * SIDE + (i - 3) * imageSize, ((CANVANS_W - 2 * imageSize - SIDE) / 2) + imageSize + SIDE, null);

}

//

break;

case 6:

if (i <= 3) {

g2d.drawImage(bufferedImages.get(i - 1), SIDE * i + imageSize * (i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2, null);

} else {

g2d.drawImage(bufferedImages.get(i - 1), ((i - 3) * SIDE) + ((i - 4) * imageSize), ((CANVANS_W - 2 * imageSize - SIDE) / 2) + imageSize + SIDE, null);

}

break;

case 7:

if (i <= 1) {

g2d.drawImage(bufferedImages.get(i - 1), 2 * SIDE + imageSize, SIDE, null);

}

if (i <= 4 && i > 1) {

g2d.drawImage(bufferedImages.get(i - 1), ((i - 1) * SIDE) + ((i - 2) * imageSize), 2 * SIDE + imageSize, null);

}

if (i <= 7 && i > 4) {

g2d.drawImage(bufferedImages.get(i - 1), ((i - 4) * SIDE) + ((i - 5) * imageSize), 3 * SIDE + 2 * imageSize, null);

}

break;

case 8:

if (i <= 2) {

g2d.drawImage(bufferedImages.get(i - 1), (CANVANS_W - 2 * imageSize - SIDE) / 2 + (i - 1) * imageSize + (i - 1) * SIDE, SIDE, null);

}

if (i <= 5 && i > 2) {

g2d.drawImage(bufferedImages.get(i - 1), ((i - 2) * SIDE) + ((i - 3) * imageSize), 2 * SIDE + imageSize, null);

}

if (i <= 8 && i > 5) {

g2d.drawImage(bufferedImages.get(i - 1), ((i - 5) * SIDE) + ((i - 6) * imageSize), 3 * SIDE + 2 * imageSize, null);

}

break;

case 9:

if (i <= 3) {

g2d.drawImage(bufferedImages.get(i - 1), (i * SIDE) + ((i - 1) * imageSize), SIDE, null);

}

if (i <= 6 && i > 3) {

g2d.drawImage(bufferedImages.get(i - 1), ((i - 3) * SIDE) + ((i - 4) * imageSize), 2 * SIDE + imageSize, null);

}

if (i <= 9 && i > 6) {

g2d.drawImage(bufferedImages.get(i - 1), ((i - 6) * SIDE) + ((i - 7) * imageSize), 3 * SIDE + 2 * imageSize, null);

}

break;

default:

break;

}

}

StringBuffer outPath = new StringBuffer().append(dir)

.append(File.separatorChar)

.append("GP-" + groupId).append(".jpg");

String format = "JPG";

File file = new File(outPath.toString());

if (!file.exists()) {

file.mkdirs();

}

return ImageIO.write(outImage, format, file);

}

/**

* 图片缩放

*

* @param filePath 图片路径

* @param height 高度

* @param width 宽度

* @param bb 比例不对时是否需要补白

*/

public static BufferedImage resize2(String filePath, int height, int width,

boolean bb) {

try {

double ratio = 0; // 缩放比例

// System.out.println("图片缩放"+filePath);

BufferedImage bi = null;

if (filePath.indexOf("http://") == 0) {

bi = ImageIO.read(new URL(filePath));

} else {

bi = ImageIO.read(new File(filePath));

}

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;

}

/**

* 删除文件

* @param path

* @param filename

*/

public void delFile(String path, String filename) {

File file = new File(path + "/" + filename);

if (file.exists() && file.isFile()) {

file.delete();

}

}

/**

* 可依次生成九种情况的群头像

*

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

Integer groupId = 0;

for (int i = 1; i < 10; i++) {

Listlist = new ArrayList<>();

for (int j = 0; j < i; j++) {

//本地图片路径或者网络图片路径

list.add("/Users/Basil/Desktop/1.jpg");

}

File file = new File("");

//生成目录

String path = file.getCanonicalPath() + "/src/main/webapp/temp/";

groupId++;

ImageUtil.getCombinationOfhead(list, path, groupId.toString());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值