使用java合并图片

编写Bimg

省略getset方法

1
2
3
4
5
6
7
8
import java.awt.image.BufferedImage;

public class Bimg {
private BufferedImage image;
private int w;
private int y;
}

编写测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class Test {

private static List<String> getImgs() {
List<String> array = new ArrayList<>();
array.add("D:\\temp\\signimgs\\img\\1.png");
array.add("D:\\temp\\signimgs\\img\\2.png");
array.add("D:\\temp\\signimgs\\img\\3.png");
array.add("D:\\temp\\signimgs\\img\\4.png");
return array;
}

public static void main(String[] args) throws Exception {
// 横向图片数量
int wsize = 3;
List<String> imgs = getImgs();
int allWidth = 0;
int allHeight = 0;
List<Bimg> bimgs = new ArrayList<>();
// 目前横坐标
int w = 0;
// 目前竖坐标
int y = 0;
// 当前行宽度
int currentRowWidth = 0;
// 当前行高度
int currentRowHeight = 0;
for (int i = 1; i <= imgs.size(); i++) {
String imgPath = imgs.get(i - 1);
File imgFile = new File(imgPath);
InputStream in = new FileInputStream(imgFile);
BufferedImage imageBuffer = ImageIO.read(in);
Bimg bimg = new Bimg();
bimgs.add(bimg);
bimg.setImage(imageBuffer);
// 判断是否需要换行
if (i % (wsize + 1) == 0) {
// 需要换行
w = 0;
y = currentRowHeight;
allWidth = Math.max(allWidth, currentRowWidth);
allHeight = allHeight + currentRowHeight;
currentRowWidth = 0;
currentRowHeight = 0;
}
bimg.setW(w);
bimg.setY(y);
w = w + imageBuffer.getWidth();
currentRowHeight = Math.max(currentRowHeight, imageBuffer.getHeight());
// 维护当前行宽度
currentRowWidth = currentRowWidth + imageBuffer.getWidth();
}
// 如果最终的高宽都是0,就进行一次设置,主要为仅一行的情况做处理
if (currentRowWidth != 0 || currentRowHeight != 0) {
allWidth = Math.max(allWidth, currentRowWidth);
allHeight = allHeight + currentRowHeight;
}

// 解决透明区域变黑的问题
BufferedImage combined = new BufferedImage(allWidth, allHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = combined.createGraphics();
combined = g.getDeviceConfiguration().createCompatibleImage(combined.getWidth(), combined.getHeight(),
Transparency.TRANSLUCENT);
// 解决透明区域变黑的问题
Graphics graphics = combined.getGraphics();
for (Bimg bimg : bimgs) {
graphics.drawImage(bimg.getImage(), bimg.getW(), bimg.getY(), null);
}
ImageIO.write(combined, "png", new File("D:\\temp\\signimgs\\img\\res.png"));
}

}

其中需要注意的是,如果源图片存在透明区域,直接合并的话,透明区域会变为黑色,该情况使用下面的代码就能解决:

1
2
3
4
BufferedImage combined = new BufferedImage(allWidth, allHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = combined.createGraphics();
combined = g.getDeviceConfiguration().createCompatibleImage(combined.getWidth(), combined.getHeight(),
Transparency.TRANSLUCENT);