绘制群聊头像

绘制群聊头像,从一个到9个,比较简单

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Temp1 {
    public static void main(String[] args) throws IOException {
        File file = new File("mint.jpg");
        BufferedImage bufferedImage = ImageIO.read(file);
        int unit = Math.max(bufferedImage.getHeight(), bufferedImage.getWidth());
        int outSize = 521, border = 10, gap = 3;
        BufferedImage outImage = new BufferedImage(outSize, outSize, BufferedImage.TYPE_INT_RGB);

        Graphics graphics = outImage.getGraphics();

        Graphics2D graphics2D = (Graphics2D) graphics;

        graphics2D.setBackground(Color.CYAN);

        draw9(outSize, unit, gap, border, bufferedImage, graphics2D, outImage);
        draw8(outSize, unit, gap, border, bufferedImage, graphics2D, outImage);
        draw7(outSize, unit, gap, border, bufferedImage, graphics2D, outImage);
        draw5(outSize, unit, gap, border, bufferedImage, graphics2D, outImage);
        draw6(outSize, unit, gap, border, bufferedImage, graphics2D, outImage);

        file = new File("qiaoba.jpg");
        ImageInputStream imageInputStream = ImageIO.createImageInputStream(file);
        int unit2 = (outSize - gap) / 2 - border;
        Rectangle rectangle = new Rectangle(unit2, unit2);
        ImageReader imageReader = ImageIO.getImageReadersByFormatName("jpg").next();
        imageReader.setInput(imageInputStream, true);
        ImageReadParam imageReadParam = imageReader.getDefaultReadParam();
        imageReadParam.setSourceRegion(rectangle);
        bufferedImage = imageReader.read(0, imageReadParam);
        draw4(outSize, unit2, gap, border, bufferedImage, graphics2D, outImage);
        draw3(outSize, unit2, gap, border, bufferedImage, graphics2D, outImage);
        draw2(outSize, unit2, gap, border, bufferedImage, graphics2D, outImage);

        int unit3 = outSize * 2 / 3;
        rectangle = new Rectangle(unit3, unit3);
        imageReadParam.setSourceRegion(rectangle);
        bufferedImage = imageReader.read(0, imageReadParam);
        draw1(outSize, unit3, gap, border, bufferedImage, graphics2D, outImage);
    }

    private static void draw1(int outSize, int unit3, int gap, int border, BufferedImage bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);
        graphics2D.drawImage(bufferedImage, (outSize - unit3) / 2, (outSize - unit3) / 2, null);
        writeFile(1, outImage);
    }

    private static void draw2(int outSize, int unit2, int gap, int border, BufferedImage bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);
        for (int i = 0; i < 2; i++) {
            graphics2D.drawImage(bufferedImage, border + (unit2 + gap) * i, (outSize - unit2) / 2, null);
        }
        writeFile(2, outImage);
    }

    private static void draw3(int outSize, int unit2, int gap, int border, BufferedImage bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);
        graphics2D.drawImage(bufferedImage, (outSize - unit2) / 2, border, null);
        for (int j = 0; j < 2; j++) {
            graphics2D.drawImage(bufferedImage, border + (unit2 + gap) * j, border + unit2 + gap, null);
        }
        writeFile(3, outImage);
    }

    private static void draw4(int outSize, int unit, int gap, int border, BufferedImage bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                graphics2D.drawImage(bufferedImage, border + (unit + gap) * j, border + (unit + gap) * i, null);
            }
        }
        writeFile(4, outImage);
    }

    private static void draw5(int outSize, int unit, int gap, int border, BufferedImage bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);

        for (int i = 0; i < 2; i++) {
            graphics2D.drawImage(bufferedImage, (outSize - gap) / 2 - unit + (unit + gap) * i, (outSize - gap) / 2 -
                    unit, null);
        }
        for (int i = 0; i < 3; i++) {
            graphics2D.drawImage(bufferedImage, border + (unit + gap) * i, (outSize - gap) / 2 + gap, null);
        }


        writeFile(5, outImage);
    }

    private static void draw6(final int outSize, final int unit, final int gap, int border, BufferedImage
            bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);
        int yborder = (outSize - gap) / 2 - unit;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                graphics2D.drawImage(bufferedImage, border + (unit + gap) * j, yborder + (unit + gap) * i, null);
            }
        }
        writeFile(6, outImage);
    }

    private static void draw7(int outSize, int unit, int gap, int border, BufferedImage bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);
        int startX = (outSize - unit) / 2;
        graphics2D.drawImage(bufferedImage, startX, border, null);
        for (int i = 1; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                graphics2D.drawImage(bufferedImage, border + (unit + gap) * j, border + (unit + gap) * i, null);
            }
        }
        writeFile(7, outImage);
    }

    private static void draw8(int outSize, int unit, int gap, int border, BufferedImage bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);
        int startX = (outSize - gap) / 2 - unit;
        for (int j = 0; j < 2; j++) {
            graphics2D.drawImage(bufferedImage, startX + (unit + gap) * j, border, null);
        }
        for (int i = 1; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                graphics2D.drawImage(bufferedImage, border + (unit + gap) * j, border + (unit + gap) * i, null);
            }
        }
        writeFile(8, outImage);
    }

    private static void draw9(int outSize, int unit, int gap, int border, BufferedImage bufferedImage, Graphics2D
            graphics2D, BufferedImage outImage) throws IOException {
        graphics2D.clearRect(0, 0, outSize, outSize);

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                graphics2D.drawImage(bufferedImage, border + (unit + gap) * j, border + (unit + gap) * i, null);
            }
        }
        writeFile(9, outImage);
    }

    private static void writeFile(int i, BufferedImage outImage) throws IOException {
        File file = new File("result" + i + ".jpg");
        ImageIO.write(outImage, "jpg", file);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值