package com.test;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
/**
* 图片工具类
* @author paul
*
*/
public class ImageUtils {
public static void main(String[] args) {
ImageUtils iu = new ImageUtils();
iu.waterMark("E:\\i.png", "E:\\i_1.png", "T");
List<String> list = new ArrayList<String>();
list.add("E:\\i.png");
list.add("E:\\i_1.png");
iu.append(list, "E:\\i_2.png", true);
}
/**
* 图片添加文字水印(保持背景透明)
* @param srcImgPath
* @param outImgPath
* @param text
*/
public void waterMark(String srcImgPath, String outImgPath, String text) {
try {
BufferedImage image = ImageIO.read(new File(srcImgPath));
int width = image.getWidth();
int height = image.getHeight();
Graphics2D g2d = image.createGraphics();
Font font = new Font("宋体", Font.PLAIN, 50);
//设置字体居中
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(text, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2d.setFont(font);
g2d.drawString(text, (int) x, (int) baseY);
g2d.dispose();
File file = new File(outImgPath);
ImageIO.write(image, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 图片拼接(保持背景透明)
* @param inputFileNameList
* @param outputFileName
* @param isX 是否横向拼接
*/
public void append(List<String> inputFileNameList, String outputFileName, boolean isX) {
try {
boolean isFirstPng = true;
BufferedImage outputImg = null;
int outputImgW = 0;
int outputImgH = 0;
for (String pngFileName : inputFileNameList) {
if (isFirstPng) {
isFirstPng = false;
outputImg = ImageIO.read(new File(pngFileName));
outputImgW = outputImg.getWidth();
outputImgH = outputImg.getHeight();
} else {
BufferedImage appendImg = ImageIO.read(new File(pngFileName));
int appendImgW = appendImg.getWidth();
int appendImgH = appendImg.getHeight();
if (isX) {
outputImgW = outputImgW + appendImgW;
outputImgH = outputImgH > appendImgH ? outputImgH : appendImgH;
} else {
outputImgW = outputImgW > appendImgW ? outputImgW : appendImgW;
outputImgH = outputImgH + appendImgH;
}
// create basic image
Graphics2D g2d = outputImg.createGraphics();
BufferedImage imageNew = g2d.getDeviceConfiguration().createCompatibleImage(outputImgW, outputImgH, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = imageNew.createGraphics();
int oldImgW = outputImg.getWidth();
int oldImgH = outputImg.getHeight();
g2d.drawImage(outputImg, 0, 0, oldImgW, oldImgH, null);
if (isX) {
g2d.drawImage(appendImg, oldImgW, 0, appendImgW, appendImgH, null);
} else {
g2d.drawImage(appendImg, 0, oldImgH, appendImgW, appendImgH, null);
}
g2d.dispose();
outputImg = imageNew;
}
}
File file = new File(outputFileName);
ImageIO.write(outputImg, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
图片工具类(不断更新中)
最新推荐文章于 2024-05-13 22:01:39 发布