MCPcopy Create free account
hub / github.com/Jarrettluo/all-docs / MergeImage

Class MergeImage

src/main/java/com/jiaruiblog/util/MergeImage.java:13–86  ·  view source on GitHub ↗

@program: transformation @description: 多张图片合成 @author: cuixy @create: 2019-07-26 17:10

Source from the content-addressed store, hash-verified

11 * @create: 2019-07-26 17:10
12 **/
13public class MergeImage {
14
15 /**
16 * 合并任数量的图片成一张图片
17 *
18 * @param isHorizontal
19 * true代表水平合并,fasle代表垂直合并
20 * @param imgs
21 * 待合并的图片数组
22 * @return
23 * @throws IOException
24 */
25 public static BufferedImage mergeImage(boolean isHorizontal, List<BufferedImage> imgs) throws IOException {
26 // 生成新图片
27 BufferedImage destImage = null;
28 // 计算新图片的长和高
29 int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
30 // 获取总长、总宽、最长、最宽
31 for (int i = 0; i < imgs.size(); i++) {
32 BufferedImage img = imgs.get(i);
33 allw += img.getWidth();
34
35 if (imgs.size() != i + 1) {
36 allh += img.getHeight() + 2;
37 } else {
38 allh += img.getHeight();
39 }
40
41 if (img.getWidth() > allwMax) {
42 allwMax = img.getWidth();
43 }
44
45 if (img.getHeight() > allhMax) {
46 allhMax = img.getHeight();
47 }
48 }
49
50 // 创建新图片
51 if (isHorizontal) {
52 destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
53 } else {
54 destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
55 }
56
57 // 注释,分隔线从灰色变成纯黑
58 // Graphics2D g2 = (Graphics2D) destImage.getGraphics();
59 // g2.setBackground(Color.LIGHT_GRAY);
60 // g2.clearRect(0, 0, allw, allh);
61 // g2.setPaint(Color.RED);
62
63 // 合并所有子图片到新图片
64 int wx = 0, wy = 0;
65 for (int i = 0; i < imgs.size(); i++) {
66 BufferedImage img = imgs.get(i);
67 int w1 = img.getWidth();
68 int h1 = img.getHeight();
69 // 从图片中读取RGB
70 int[] ImageArrayOne = new int[w1 * h1];

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected