很多时候会用到给图片增加水印,这个时候我推荐利用“光影魔术手”,它完成水印的功能非常强大,而且也可以批处理,不过Java也可以非常轻松的完成这些事情,但效果也打点折扣,坦诚点说吧,java在图像处理方面确实不如c++,不过这本来就不是java的强项,都可以理解。
利用Java给图片增加水印道理很简单:利用Java在内存中加载一副图片,然后在此基础上加载第二幅图片,而这叠加就成了水印,同时第二幅图片您可以控制图片的透明度。水印文字也一个道理。利用这个道理,你甚至可以做到组合图片等等操作,只有想不到,没有做不到。
代码正在整理最近随后放上去。这里就把算法说一下。
水印图片的方法是:Graphics2D的drawImage(waterImage, x, y, width, height, null);
水印文字的方法是:Graphics2D的drawString(pressText, x, y);
/**
* 添加图片水印
*
* @param targetImg
* 目标图片路径,如:C:\\kutuku.jpg
* @param waterImg
* 水印图片路径,如:C:\\kutuku.png
* @param x
* 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间
* @param y
* 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间
* @param alpha
* 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
*/
public final static void pressImage(String targetImg, String waterImg,
int x, int y, float alpha) {
try {
// 加载目标图片
File file = new File(targetImg);
Image image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
// 将目标图片加载到内存。
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
// 加载水印图片。
Image waterImage = ImageIO.read(new File(waterImg));
int width_1 = waterImage.getWidth(null);
int height_1 = waterImage.getHeight(null);
// 设置水印图片的透明度。
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 设置水印图片的位置。
int widthDiff = width - width_1;
int heightDiff = height - height_1;
if (x < 0) {
x = widthDiff / 2;
} else if (x > widthDiff) {
x = widthDiff;
}
if (y < 0) {
y = heightDiff / 2;
} else if (y > heightDiff) {
y = heightDiff;
}
// 将水印图片“画”在原有的图片的制定位置。
g.drawImage(waterImage, x, y, width_1, height_1, null);
// 关闭画笔。
g.dispose();
// 保存目标图片。
ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 添加文字水印
*
* @param targetImg
* 目标图片路径,如:C:\\kutoku.jpg
* @param pressText
* 水印文字, 如:kutuku.com
* @param fontName
* 字体名称, 如:宋体
* @param fontStyle
* 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC)
* @param fontSize
* 字体大小,单位为像素
* @param color
* 字体颜色
* @param x
* 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间
* @param y
* 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间
* @param alpha
* 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
*/
public static void pressText(String targetImg, String pressText,
String fontName, int fontStyle, int fontSize, Color color, int x,
int y, float alpha) {
try {
// 加载目标图片
File file = new File(targetImg);
Image image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
// 将目标图片加载到内存。
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setColor(color);
// 设置水印图片的透明度。
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 设置水印图片的位置。
int width_1 = fontSize * getLength(pressText);
int height_1 = fontSize;
int widthDiff = width - width_1;
int heightDiff = height - height_1;
if (x < 0) {
x = widthDiff / 2;
} else if (x > widthDiff) {
x = widthDiff;
}
if (y < 0) {
y = heightDiff / 2;
} else if (y > heightDiff) {
y = heightDiff;
}
// 将水印文字“写”在原有的图片的制定位置。
g.drawString(pressText, x, y + height_1);
// 关闭画笔。
g.dispose();
// 保存目标图片。
ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);
} catch (Exception e) {
e.printStackTrace();
}
}