完整代码:
package com.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import javax.imageio.ImageIO;
public class WaterMarkUtils2 {
// 默认格式
private static final String FORMAT_NAME = "JPG";
// 默认 宽度
private static final int WIDTH = 100;
// 默认 高度
private static final int HEIGHT = 100;
public static void main(String[] args) {
// 输入图片地址
String[] imgs = { "C:\\Users\\edz\\Desktop\\b1.jpg","C:\\Users\\edz\\Desktop\\a1.jpg" };
// 调用方法生成图片
merge(imgs, "jpg", "C:\\Users\\edz\\Desktop\\b2.jpg");
}
/**
* Java拼接多张图片
*
* @param imgs
* 图片地址集合
* @param type
* 图片类型
* @param dst_pic
* //输出的文件:F:/test2.jpg
* @return
*/
public static boolean merge(String[] imgs, String type, String dst_pic) {
// 获取需要拼接的图片长度
int len = imgs.length;
// 判断长度是否大于0
if (len < 1) {
return false;
}
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
try {
src[i] = new File(imgs[i]);
images[i] = ImageIO.read(src[i]);
} catch (Exception e) {
e.printStackTrace();
return false;
}
int width = images[i].getWidth();
int height = images[i].getHeight();
// 从图片中读取RGB 像素
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height,
ImageArrays[i], 0, width);
}
int dst_height = 0;
int dst_width = images[0].getWidth();
// 合成图片像素
for (int i = 0; i < images.length; i++) {
dst_width = dst_width > images[i].getWidth() ? dst_width
: images[i].getWidth();
dst_height += images[i].getHeight();
}
// 合成后的图片
System.out.println("宽度:" + dst_width);
System.out.println("高度:" + dst_height);
if (dst_height < 1) {
System.out.println("dst_height < 1");
return false;
}
// 生成新图片
try {
dst_width = images[0].getWidth();
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height,
BufferedImage.TYPE_INT_RGB);
int height_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(),
ImageArrays[i], 0, dst_width);
height_i += images[i].getHeight();
}
File outFile = new File(dst_pic);
ImageIO.write(ImageNew, type, outFile);// 写图片 ,输出到硬盘
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 获取图片
*
* @param content
* 内容
* @param font
* 字体
* @param width
* 宽
* @param height
* 高
* @param destPath
* 输出路径
* @throws IOException
*/
public static void getImage(String content, Font font, Integer width,
Integer height, String destPath) throws IOException {
mkdirs(destPath);
String file = UUID.randomUUID().toString() + ".jpg";
ImageIO.write(getImage(content, font, width, height), FORMAT_NAME,
new File(destPath + "/" + file));
}
/**
* 创建 目录
*
* @param destPath
*/
public static void mkdirs(String destPath) {
File file = new File(destPath);
// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
/**
* 创建图片
*
* @param content
* 内容
* @param font
* 字体
* @param width
* 宽
* @param height
* 高
* @return
*/
private static BufferedImage createImage(String content, Font font,
Integer width, Integer height) {
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.BLACK);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(content, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(content, (int) x, (int) baseY);
return bi;
}
/**
* 获取 图片
*
* @param content
* 内容
* @param font
* 字体
* @param width
* 宽
* @param height
* 高
* @return
*/
public static BufferedImage getImage(String content, Font font,
Integer width, Integer height) {
width = width == null ? WIDTH : width;
height = height == null ? HEIGHT : height;
if (null == font)
font = new Font("Serif", Font.BOLD, 11);
return createImage(content, font, width, height);
}
}