一、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;

}

}

java 图片水印_JAVA实现图片水印相关推荐

  1. jsp text 水印_java实现图片上加文字水印(SpringMVC + Jsp)

    看之前要先对SpringMVC进行了解打好基础,下面直接先看效果图 代码编写 1.导入相关架包 2.配置文件 web.xml watermarkspringmvc dispatcherServlet ...

  2. java thumbnails 中心点_java Thumbnails 图片处理的使用

    在后端开发的过程中,都逃不开与文件传输特别是图片的传输打交道,但是因为现在各种拍照设备发展越来越快,拍出的照片更是越来越清晰,但是照片文件的大小也是越来越大了,手机拍照小则2M大则30M这在网络传输过 ...

  3. java验证图片大小_java 校验图片的大小、尺寸、比例

    import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.i ...

  4. java 修改图片分辨率_java 修改图片的像素大小,清晰度

    修改图片的像素,清晰度 代码 /** * 改变图片 像素 * * @param file * @param qality 参数qality是取值0~1范围内 清晰程度 数值越小分辨率越低 * @par ...

  5. background图片叠加_java实现图片的叠加效果

    实现效果图 这个效果的实现思路是: 1.先准备一张背景图,像素自己去定.像素越高最后生成的图片会更清晰,当然图片也会越大,注意这个背景图一定要是png格式的,要放用户头像的位置要镂空,透明的. 至于为 ...

  6. java图片加文字水印_JAVA实现图片的修改,添加文字水印效果

    JAVA实现图片的修改,添加文字水印效果,根据文字内容生成图片,下面是具体的代码,以及测试方法: 实现类: package JavaMoImage; import javax.p_w_picpathi ...

  7. java给图片加水印_Java 为图片添加水印(图片水印,文字水印)

    Java 为图片添加水印(图片水印,文字水印) 2014-07-28·WeaponX 5267 次浏览 ```java import java.awt.Color; import java.awt.F ...

  8. java实现清除图片水印_Java实现图片水印

    publicfinalclassImageUtils {publicImageUtils() { }publicfinalstaticString getPressImgPath() {returnA ...

  9. java如何给pdf加水印_java pdf加水印的方法

    本文实例为大家分享了java pdf加水印的具体代码,供大家参考,具体内容如下 引入依赖 com.itextpdf.tool xmlworker 5.5.10 com.itextpdf itextpd ...

最新文章

  1. Struts Form中Date类型之“argument type mismatch”错误解决方法
  2. VSS2005下载地址
  3. 解决无法连接到远程SqlServer 2005 Express服务器的问题
  4. clientdataset 过滤 in_天心大风量亚高效过滤器
  5. gzip+chunked页面分段输出,resin gzip trunked无效,页面数据写入自定义buffer
  6. hive插入数据:FAILED: ParseException line 1:12 missing TABLE at 'student' near 'EOF'
  7. mysql怎么创建自动作业_mysql 让一个存储过程定时作业的代码
  8. grunt入门 出处:http://artwl.cnblogs.com
  9. linux 关闭ext3日志,ssh – 可以在Linux(ext3)上减慢日志写入速度吗?
  10. c语言 poll,c语言 linux 中 poll 的参数
  11. java多线程02-----------------synchronized底层实现及JVM对synchronized的优化
  12. MySql【超简单】清空部分表的数据
  13. Google Chrome 独立安装下载
  14. 【PLSQL】PLSQL安装、破解以及汉化教程
  15. ee er_61对词根相同后缀分别是er和ee的单词要这样区别记忆它们
  16. python wav转pcm
  17. 算法--二分查找(python实现)
  18. 跳出横向的、孤立的牵绊,真正站在新的角度来看待Web3.0
  19. 如何将带网口打印机进行局域网共享打印
  20. Linux常见权限处理、文件搜索、帮助、压缩命令

热门文章

  1. 浙江大学 工程伦理 第十一章单元测试答案
  2. Halcon实例分析——check_blister.hdev
  3. 音视频技术开发周刊 | 279
  4. 【标准】Oracle Optimal Flexible Architecture(OFA)简介
  5. 职高计算机教学案例 反思,教学案例分析反思
  6. 趣玩Python——如何帮女朋友快速抢票
  7. ECM是什么-企业内容管理
  8. nao机器人国际比赛程序 python编写
  9. Mybatis中,SQLSessionFactoryBuilder使用build方法时做了哪些事?
  10. 巧用宏录制,轻松制作Excel简易查询小系统