JAVA实现图片水印
一、JAVA图片水印实现原理
1.1、JAVA图片水印实现思路
1、创建缓存图片对象。
2、创建Java绘图工具对象。
3、使用绘图工具对象将原图绘制到缓存图片对象。
4、使用绘图工具将水印(文字/图片)绘制到缓存图片对象。
5、创建图像编码工具类。
6、使用图像编码工具类,输出缓存图像到目标图片文件。
1.2、JAVA图片水印使用工具类
1、BufferedImage 把对象储存到缓存中,提高运行效率。
2、Graohics2D 对对象进行操作。
3、JPEGImageEncode 对对象进行编码,把内存中的对象刻录到我们的磁盘上。
二、实现图片添加单个文字水印
/*
* 给图片添加单个文字水印类
* */
publicclassTextWatermarking {
//定义图片水印字体类型
publicstaticfinalString FONT_NAME = "微软雅黑";
//定义图片水印字体加粗、变细、倾斜等样式
publicstaticfinalintFONT_STYLE= Font.BOLD;
//设置字体大小
publicstaticfinalintFONT_SIZE= 120;
//设置文字透明程度
publicstaticfloatALPHA= 0.3F;
publicstaticfinalintX=10;
publicstaticfinalintY=10;
/**
* 给图片添加单个文字水印、可设置水印文字旋转角度
* source 需要添加水印的图片路径(如:F:/images/6.jpg)
* outPut 添加水印后图片输出路径(如:F:/images/)
* imageName 图片名称
* imageType 图片类型
* color 水印文字的颜色
* word 水印文字
* degree 水印文字旋转角度,为null表示不旋转
*/
publicBoolean markImageBySingleText(String sourcePath, String outputPath, String imageName, String imageType, Color color, String word, Integer degree) {
try{
//读取原图片信息
File file = new File(sourcePath);
if(!file.isFile()) {
returnfalse;
}
//获取源图像的宽度、高度
Image image = ImageIO.read(file);
intwidth = image.getWidth(null);
intheight = image.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//创建绘图工具对象
Graphics2D graphics2D = bufferedImage.createGraphics();
//其中的0代表和原图位置一样
graphics2D.drawImage(image, 0, 0, width, height, null);
//设置水印文字(设置水印字体样式、粗细、大小)
graphics2D.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
//设置水印颜色
graphics2D.setColor(color);
//设置水印透明度
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPHA));
//设置水印旋转
if(null != degree) {
graphics2D.rotate(Math.toRadians(degree),(double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
}
intwidth1=FONT_SIZE*getTextLength(word);
intheight1=FONT_SIZE;
intwidthDiff=width-width1;
intheightDiff=height-height1;
intx=X;
inty=Y;
if(x>widthDiff)
{
x=widthDiff;
}
if(y>heightDiff)
{
y=heightDiff;
}
//进行绘制
graphics2D.drawString(word, x, y+120);
graphics2D.dispose();
//输出图片
File sf = new File(outputPath, imageName+"."+imageType);
// 保存图片
ImageIO.write(bufferedImage, imageType, sf);
} catch (Exception e) {
e.printStackTrace();
}
returntrue;
}
/**
* 如果水印內容超出判断
*/
publicintgetTextLength(String text)
{
intlength=text.length();
for(int i = 0; i < text.length(); i++) {
String s=String.valueOf(text.charAt(i));
if(s.getBytes().length>1)
{
length++;
}
}
length=length%2==0?length/2:length/2+1;
returnlength;
}
}
三、实现图片添加单个图片水印
/*
* 给图片添加单个图片水印、可设置水印图片旋转角度
* */
publicclassImageWatermark {
publicstaticfinalintX=10;
publicstaticfinalintY=10;
/**
*icon 水印图片路径(如:F:/images/icon.png)
*source 没有加水印的图片路径(如:F:/images/6.jpg)
*output 加水印后的图片路径(如:F:/images/)
*imageName 图片名称(如:11111)
*imageType 图片类型(如:jpg)
*degree 水印图片旋转角度,为null表示不旋转
*/
publicBoolean markImageBySingleIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {
try{
File file = new File(source);
File ficon = new File(icon);
if(!file.isFile()) {
returnfalse;
}
//将icon加载到内存中
Image ic = ImageIO.read(ficon);
//icon高度
inticheight= ic.getHeight(null);
//将源图片读到内存中
Image img = ImageIO.read(file);
//图片宽
intwidth = img.getWidth(null);
//图片高
intheight = img.getHeight(null);
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//创建一个指定 BufferedImage 的 Graphics2D 对象
Graphics2D g = bi.createGraphics();
//设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//呈现一个图像,在绘制前进行从图像空间到用户空间的转换
g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
if(null != degree) {
//设置水印旋转
g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
}
//水印图象的路径 水印一般为gif或者png的,这样可设置透明度
ImageIcon imgIcon = new ImageIcon(icon);
//得到Image对象。
Image con = imgIcon.getImage();
intwidth1=con.getWidth(null);
intheight1=con.getHeight(null);
intwidthDiff=width-width1;
intheightDiff=height-height1;
intx=X;
inty=Y;
if(x>widthDiff)
{
x=widthDiff;
}
if(y>heightDiff)
{
y=heightDiff;
}
//透明度,最小值为0,最大值为1
floatclarity = 0.6f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));
//表示水印图片的坐标位置(x,y)
g.drawImage(con, x, y, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
File sf = new File(output, imageName+"."+imageType);
ImageIO.write(bi, imageType, sf); // 保存图片
} catch (Exception e) {
e.printStackTrace();
}
returntrue;
}
四、实现图片添加多个文字水印
/*
* 给图片添加多个文字水印类
* */
publicclassTextWatermarking {
// 定义图片水印字体类型
publicstaticfinalString FONT_NAME = "微软雅黑";
// 定义图片水印字体加粗、变细、倾斜等样式
publicstaticfinalintFONT_STYLE= Font.BOLD;
// 设置字体大小
publicstaticfinalintFONT_SIZE= 120;
// 设置文字透明程度
publicstaticfloatALPHA= 0.3F;
/**
* 给图片添加多个文字水印、可设置水印文字旋转角度 source 需要添加水印的图片路径 outPut 添加水印后图片输出路径 imageName 图片名称
* imageType 图片类型 color 水印文字的颜色 word 水印文字 degree 水印文字旋转角度,为null表示不旋转
*/
publicBoolean markImageByMoreText(String sourcePath, String outputPath, String imageName, String imageType,
Color color, String word, Integer degree) {
try{
// 读取原图片信息
File file = newFile(sourcePath);
if(!file.isFile()) {
returnfalse;
}
// 获取源图像的宽度、高度
Image image = ImageIO.read(file);
intwidth = image.getWidth(null);
intheight = image.getHeight(null);
BufferedImage bufferedImage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 创建绘图工具对象
Graphics2D graphics2D = bufferedImage.createGraphics();
// 其中的0代表和原图位置一样
graphics2D.drawImage(image, 0, 0, width, height, null);
// 设置水印文字(设置水印字体样式、粗细、大小)
graphics2D.setFont(newFont(FONT_NAME, FONT_STYLE, FONT_SIZE));
// 设置水印颜色
graphics2D.setColor(color);
// 设置水印透明度
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
// 设置水印旋转
if(null != degree) {
graphics2D.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2,
(double) bufferedImage.getHeight() / 2);
}
intwidth1 = FONT_SIZE * getTextLength(word);
intheight1 = FONT_SIZE;
intx = -width / 2;
inty = -height / 2;
while(x < width * 1.5) {
y = -height / 2;
while(y < height * 1.5) {
graphics2D.drawString(word, x, y);
y += height1 + 300;
}
x += width1 + 300;
}
graphics2D.dispose();
// 输出图片
File sf = newFile(outputPath, imageName + "." + imageType);
// 保存图片
ImageIO.write(bufferedImage, imageType, sf);
} catch(Exception e) {
e.printStackTrace();
}
returntrue;
}
/**
* 如果水印內容超出判断
*/
publicintgetTextLength(String text) {
intlength = text.length();
for(int i = 0; i < text.length(); i++) {
String s = String.valueOf(text.charAt(i));
if(s.getBytes().length > 1) {
length++;
}
}
length = length % 2 == 0 ? length / 2 : length / 2 + 1;
returnlength;
}
}
五、JAVA实现添加多个图片水印
publicclassMoreImageWatermark {
// 设置文字透明程度
publicstaticfloatALPHA= 0.3F;
/**
* 给图片添加多个图片水印、可设置水印图片旋转角度 logoPath 水印图片的路径 source 需要添加水印的图片路径 outPut
* 添加水印后图片输出路径 imageName 图片名称 imageType 图片类型 degree 水印文字旋转角度,为null表示不旋转
*/
publicBoolean markImageByMoreImage(String logoPath, String sourcePath, String outputPath, String imageName,
String imageType, Integer degree) {
try{
// 读取原图片信息
File file = newFile(sourcePath);
if(!file.isFile()) {
returnfalse;
}
// 获取源图像的宽度、高度
Image image = ImageIO.read(file);
intwidth = image.getWidth(null);
intheight = image.getHeight(null);
BufferedImage bufferedImage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 创建绘图工具对象
Graphics2D graphics2D = bufferedImage.createGraphics();
// 其中的0代表和原图位置一样
graphics2D.drawImage(image, 0, 0, width, height, null);
// 设置水印透明度
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
// 设置水印旋转
if(null != degree) {
graphics2D.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2,
(double) bufferedImage.getHeight() / 2);
}
File logo = newFile(logoPath);
Image logoimage = ImageIO.read(logo);
intwidth1 = logoimage.getWidth(null);
intheight1 = logoimage.getHeight(null);
intx = -width / 2;
inty = -height / 2;
while(x < width * 1.5) {
y = -height / 2;
while(y < height * 1.5) {
graphics2D.drawImage(logoimage, x, y, null);
y += height1 + 300;
}
x += width1 + 300;
}
graphics2D.dispose();
// 输出图片
File sf = newFile(outputPath, imageName + "." + imageType);
// 保存图片
ImageIO.write(bufferedImage, imageType, sf);
} catch(Exception e) {
e.printStackTrace();
}
returntrue;
}
}