今天小编接到一个小小的任务:将xml中base64编码格式的图片拼接成一个图片,再以base64编码格式放回到xml中

直接贴上代码:(只附上图片拼接的代码)

【用于将多个图片拼接成一张图片】

1 /**
 2  * 合并任数量的图片成一张图片
 3  * 
 4  * @param isHorizontal
 5  *            true代表水平合并,fasle代表垂直合并
 6  * @param imgs
 7  *            待合并的图片数组
 8  * @return
 9  * @throws IOException
10  */
11 private BufferedImage mergeImage(boolean isHorizontal, BufferedImage... imgs) throws IOException {
12     // 生成新图片
13     BufferedImage destImage = null;
14     // 计算新图片的长和高
15     int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
16     // 获取总长、总宽、最长、最宽
17     for (int i = 0; i < imgs.length; i++) {
18         BufferedImage img = imgs[i];
19         allw += img.getWidth();
20         allh += img.getHeight();
21         if (img.getWidth() > allwMax) {
22             allwMax = img.getWidth();
23         }
24         if (img.getHeight() > allhMax) {
25             allhMax = img.getHeight();
26         }
27     }
28     // 创建新图片
29     if (isHorizontal) {
30         destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
31     } else {
32         destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
33     }
34     // 合并所有子图片到新图片
35     int wx = 0, wy = 0;
36     for (int i = 0; i < imgs.length; i++) {
37         BufferedImage img = imgs[i];
38         int w1 = img.getWidth();
39         int h1 = img.getHeight();
40         // 从图片中读取RGB
41         int[] ImageArrayOne = new int[w1 * h1];
42         ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
43         if (isHorizontal) { // 水平方向合并
44             destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
45         } else { // 垂直方向合并
46             destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
47         }
48         wx += w1;
49         wy += h1;
50     }
51     return destImage;
52 }

以上代码仅供参考,如有问题,还请大神们多多指教