绘制群聊头像,从一个到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);
}
}