概述
Java图片添加水印工具
废话少说,直接上代码:
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImgUtil {
public static void main(String[] args) {
String srcImgPath = "D:/test/123.jpg";
String iconPath = "d:test/printtext.png";
pressImage(srcImgPath, iconPath);
pressText(srcImgPath, "水印内容说明。如(仅限xxx使用,他用无效)", 0, Color.red);
}
public static boolean pressImage(String targetImg, String waterImg) {
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,
0.5f));
int widthDiff = width - width_1;
int heightDiff = height - height_1;
int x = widthDiff / 2;// 水印位置
int y = heightDiff / 2;// 水印位置
g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件结束
g.dispose();
ImageIO.write(bufferedImage, "png", file);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 照片添加水印
* @param targetImg 原始照片路径
* @param pressText 水印文字
* @param fontSize 文字大小 <=0时字体大小自适应
* @param color 文字颜色
* @return
*/
public static Boolean pressText(String targetImg, String pressText, int fontSize, Color color) {
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);
if (fontSize<=0) {
fontSize = 4*width/(5*pressText.length());
}
g.setFont(new Font("宋体", Font.PLAIN, fontSize));
g.setColor(color);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
0.5f));
int width_1 = fontSize * getLength(pressText);
int height_1 = fontSize;
int widthDiff = width - width_1;
int heightDiff = height - height_1;
int x = widthDiff / 2;//水印位置
int y = heightDiff / 2;
g.drawString(pressText, x, y + height_1);
g.dispose();
ImageIO.write(bufferedImage, "jpg", file);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static int getLength(String text) {
int textLength = text.length();
int length = textLength;
for (int i = 0; i < textLength; i++) {
if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
length++;
}
}
return (length % 2 == 0) ? length / 2 : length / 2 + 1;
}
}
最后
以上就是忧郁荔枝为你收集整理的Java图片添加水印工具的全部内容,希望文章能够帮你解决Java图片添加水印工具所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复