Java 给图片和动图添加水印的方法
这两天根据需求在做图片上传添加水印,实话说重来不知道java还可以这样操作,既然有个这要求我就去找资料研究了一番,现在把它分享一下,希望能帮到有需要的兄弟。
给普通图片添加水印和给动图添加水印是不一样的,给普通图片添加水印用的是java自带的方法写的,给动图使用了gif4j框架,这个框架在CSDN里面很多可以下载,建议下载破解版的,因为原来的jar包会有自带的一个水印是去不了的。
importjava.awt.*;
importjava.awt.image.BufferedImage;
importjava.io.*;
importjavax.imageio.ImageIO;
importjavax.swing.ImageIcon;
//这下面是gif4j框架的类
importcom.gif4j.GifDecoder;
importcom.gif4j.GifEncoder;
importcom.gif4j.GifImage;
importcom.gif4j.GifTransformer;
importcom.gif4j.TextPainter;
importcom.gif4j.Watermark;
/**
*CreatedbyZXDon2018/1/18.
*/
publicclassImageRemarkUtil{
//水印透明度
privatefloatalpha=0.5f;
//水印横向位置
privateintpositionWidth=150;
//水印纵向位置
privateintpositionHeight=300;
//水印宽
privateintwidth=80;
//水印高
privateintheight=80;
//水印文字字体
privateFontfont=newFont("宋体",Font.BOLD,72);
//水印文字颜色
privateColorcolor=Color.red;
/***********普通图片加水印***********/
/**
*
*@paramalpha
*水印透明度
*@parampositionWidth
*水印横向位置
*@parampositionHeight
*水印纵向位置
*@paramfont
*水印文字字体
*@paramcolor
*水印文字颜色
*/
publicvoidsetImageMarkOptions(floatalpha,intpositionWidth,
intpositionHeight,intwidth,intheight,Fontfont,Colorcolor){
if(alpha!=0.0f)
this.alpha=alpha;
if(positionWidth!=0)
this.positionWidth=positionWidth;
if(positionHeight!=0)
this.positionHeight=positionHeight;
if(height!=0)
this.height=height;
if(width!=0)
this.width=width;
if(font!=null)
this.font=font;
if(color!=null)
this.color=color;
}
/**
*给图片添加水印图片
*
*@paramiconPath
*水印图片路径
*@paramsrcImgPath
*源图片路径
*@paramtargerPath
*目标图片路径
*/
publicvoidmarkImageByIcon(StringiconPath,StringsrcImgPath,
StringtargerPath){
markImageByIcon(iconPath,srcImgPath,targerPath,null);
}
/**
*给图片添加水印图片、可设置水印图片旋转角度
*
*@paramiconPath
*水印图片路径
*@paramsrcImgPath
*源图片路径
*@paramtargerPath
*目标图片路径
*@paramdegree
*水印图片旋转角度
*/
publicvoidmarkImageByIcon(StringiconPath,StringsrcImgPath,
StringtargerPath,Integerdegree){
OutputStreamos=null;
try{
ImagesrcImg=ImageIO.read(newFile(srcImgPath));
BufferedImagebuffImg=newBufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null),BufferedImage.TYPE_INT_RGB);
//1、得到画笔对象
Graphics2Dg=buffImg.createGraphics();
//2、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(
srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null),Image.SCALE_SMOOTH),0,0,
null);
//3、设置水印旋转
if(null!=degree){
g.rotate(Math.toRadians(degree),
(double)buffImg.getWidth()/2,
(double)buffImg.getHeight()/2);
}
//4、水印图片的路径水印图片一般为gif或者png的,这样可设置透明度
ImageIconimgIcon=newImageIcon(iconPath);
//5、得到Image对象。
Imageimg=imgIcon.getImage();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
IntegerX=srcImg.getWidth(null);
IntegerY=srcImg.getHeight(null);
//6、水印图片的位置
g.drawImage(img,X-(positionWidth+width),Y-(positionHeight+height),width,height,null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
//7、释放资源
g.dispose();
//8、生成图片
os=newFileOutputStream(targerPath);
ImageIO.write(buffImg,"JPG",os);
System.out.println("图片完成添加水印图片");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(null!=os)
os.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
/**
*给图片添加水印文字
*
*@paramlogoText
*水印文字
*@paramsrcImgPath
*源图片路径
*@paramtargerPath
*目标图片路径
*/
publicvoidmarkImageByText(StringlogoText,StringsrcImgPath,
StringtargerPath){
markImageByText(logoText,srcImgPath,targerPath,null);
}
/**
*给图片添加水印文字、可设置水印文字的旋转角度
*
*@paramlogoText
*@paramsrcImgPath
*@paramtargerPath
*@paramdegree
*/
publicvoidmarkImageByText(StringlogoText,StringsrcImgPath,
StringtargerPath,Integerdegree){
InputStreamis=null;
OutputStreamos=null;
try{
//1、源图片
ImagesrcImg=ImageIO.read(newFile(srcImgPath));
BufferedImagebuffImg=newBufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null),BufferedImage.TYPE_INT_RGB);
//2、得到画笔对象
Graphics2Dg=buffImg.createGraphics();
//3、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(
srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null),Image.SCALE_SMOOTH),0,0,
null);
//4、设置水印旋转
if(null!=degree){
g.rotate(Math.toRadians(degree),
(double)buffImg.getWidth()/2,
(double)buffImg.getHeight()/2);
}
//5、设置水印文字颜色
g.setColor(color);
//6、设置水印文字Font
g.setFont(font);
//7、设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
//8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
g.drawString(logoText,positionWidth,positionHeight);
//9、释放资源
g.dispose();
//10、生成图片
os=newFileOutputStream(targerPath);
ImageIO.write(buffImg,"JPG",os);
System.out.println("图片完成添加水印文字");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(null!=is)
is.close();
}catch(Exceptione){
e.printStackTrace();
}
try{
if(null!=os)
os.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
/***********动图加水印************/
/**
*缩放gif图片,直接传的File文件,可设置宽和高
*/
publicvoidmakeGif(Filesrc,Filedest,intwidth,intheight)throwsIOException{
GifImagegifImage=GifDecoder.decode(src);//创建一个GifImage对象.
GifImageresizeIMG=GifTransformer.resize(gifImage,width,height,true);
GifEncoder.encode(resizeIMG,dest);
}
//缩放gif图片,直接传文件路径,可设置宽和高
publicvoidmakeGif(Stringsrc,Stringdest,intwidth,intheight)throwsIOException{
GifImagegifImage=GifDecoder.decode(newFile(src));//创建一个GifImage对象.
makeGif(newFile(src),newFile(dest),gifImage.getScreenWidth()/2,
gifImage.getScreenHeight()/2);
}
//缩放gif图片,传文件File文件,不可设置宽和高
publicvoidmakeGif(Filesrc,Filedest)throwsIOException{
GifImagegifImage=GifDecoder.decode(src);//创建一个GifImage对象.
makeGif(src,dest,gifImage.getScreenWidth()/2,gifImage.getScreenHeight()/2);
}
//缩放gif图片,传文件路径,不可设置宽和高
publicvoidmakeGif(Stringsrc,Stringdest)throwsIOException{
makeGif(newFile(src),newFile(dest));
}
/**
*动图中加文字水印
*/
publicvoidaddTextWatermarkToGif(Filesrc,StringwatermarkText,Filedest)throwsIOException{
//水印初始化、设置(字体、样式、大小、颜色)
TextPaintertextPainter=newTextPainter(newFont("黑体",Font.ITALIC,12));
textPainter.setOutlinePaint(Color.WHITE);
BufferedImagerenderedWatermarkText=textPainter.renderString(watermarkText,true);
//图片对象
GifImagegf=GifDecoder.decode(src);
//获取图片大小
intiw=gf.getScreenWidth();
intih=gf.getScreenHeight();
//获取水印大小
inttw=renderedWatermarkText.getWidth();
intth=renderedWatermarkText.getHeight();
//水印位置
Pointp=newPoint();
p.x=iw-tw-5;
p.y=ih-th-4;
//加水印
Watermarkwatermark=newWatermark(renderedWatermarkText,p);
gf=watermark.apply(GifDecoder.decode(src),true);
//输出
GifEncoder.encode(gf,dest);
}
/**
*动图中加图片水印
*/
publicvoidaddImageWatermarkToGif(Filesrc,StringwatermarkPath,Filedest){
try{
BufferedImagerenderedWatermarkText=ImageIO.read(newFile(watermarkPath));
//图片对象
GifImagegf=GifDecoder.decode(src);
//获取图片大小
intiw=gf.getScreenWidth();
intih=gf.getScreenHeight();
//获取水印大小
inttw=renderedWatermarkText.getWidth();
intth=renderedWatermarkText.getHeight();
//水印位置
Pointp=newPoint();
p.x=iw-tw-20;
p.y=ih-th-20;
//加水印
Watermarkwatermark=newWatermark(renderedWatermarkText,p);
//水印透明度
watermark.setTransparency(1);
gf=watermark.apply(GifDecoder.decode(src),false);
//输出
GifEncoder.encode(gf,dest);
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
//需要加水印图片的路径
StringsrcImgPath="d:/1.jpg";
StringlogoText="复印无效";
//图片水印的路径
StringiconPath="d:/2.jpg";
//添加完水印文件的输出路径
StringtargerTextPath="d:/qie_text.jpg";
StringtargerTextPath2="d:/qie_text_rotate.jpg";
StringtargerIconPath="d:/qie_icon.jpg";
StringtargerIconPath2="d:/qie_icon_rotate.jpg";
System.out.println("给图片添加水印文字开始...");
//给图片添加水印文字
markImageByText(logoText,srcImgPath,targerTextPath);
//给图片添加水印文字,水印文字旋转-45
markImageByText(logoText,srcImgPath,targerTextPath2,-45);
System.out.println("给图片添加水印文字结束...");
System.out.println("给图片添加水印图片开始...");
setImageMarkOptions(0.3f,1,1,null,null);
//给图片添加水印图片
markImageByIcon(iconPath,srcImgPath,targerIconPath);
//给图片添加水印图片,水印图片旋转-45
markImageByIcon(iconPath,srcImgPath,targerIconPath2,-45);
System.out.println("给图片添加水印图片结束...");
//动图添加水印(添加水印动图文件,添加的水印,添加完输出文件)
addTextWatermarkToGif(newFile("d:\\10.gif"),"复印无效",newFile("d:\\11.gif"));
addImageWatermarkToGif(newFile("d:\\gif\\10.gif"),"d:\\gif\\3.png",newFile("d:\\gif\\4.gif"));
}
}
这里面有普通图片添加水印和动图添加水印,普通图片添加水印方法如果传的是动图能添加成功,但是动图就成静态的了,动图添加水印方法如果传的是普通图片,会直接报错了。
这些我在做的时候都有试过,现在就当记笔记记录在此,也希望能帮助到有需要的兄弟。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
