本文实例讲述了Java实现给图片添加图片水印,文字水印及马赛克的方法。分享给大家供大家参考,具体如下:

可以在eclipse中新建个Utils类,把以下代码复制进去直接使用,以下方法实现单个或多个水印的添加

package com.rzxt.fyx.common.util;

import java.awt.AlphaComposite;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.RenderingHints;

import java.awt.image.BufferedImage;

import java.io.File;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

/**

* 给图片添加水印

* @author tgy

*

*/

public class MarkImageUtils {

/**

* @param args

*/

public static void main(String[] args) {

String output = "F:/images/";

String source = "F:/images/6.jpg"; //源图片路径

String icon = "F:/images/icon2.png"; //覆盖图片路径

String imageName = "mark_image"; //图片名称

String imageType = "jpg"; //图片类型jpg,jpeg,png,gif

String text = "加水印了";

int size = 4; //马赛克大小

Integer degree = null; //水印旋转角度-45,null表示不旋转

String result = null;

//给图片添加图片水印

result = MarkImageUtils.markImageByMoreIcon(icon,source,output,imageName,imageType,degree);

// result = MarkImageUtils.markImageBySingleIcon(icon, source, output, imageName, imageType, degree);

// //给图片添加文字水印

// result = MarkImageUtils.markImageByMoreText(source,output,imageName,imageType,Color.red,text,degree);

// result = MarkImageUtils.markImageBySingleText(source,output,imageName,imageType,Color.red,text,degree);

// //给图片打马赛克

// result = MarkImageUtils.markImageByMosaic(source,output,imageName,imageType,size);

System.out.println(result);

}

/**

* 给图片不同位置添加多个图片水印、可设置水印图片旋转角度

* @param icon 水印图片路径(如:F:/images/icon.png)

* @param source 没有加水印的图片路径(如:F:/images/6.jpg)

* @param output 加水印后的图片路径(如:F:/images/)

* @param imageName 图片名称(如:11111)

* @param imageType 图片类型(如:jpg)

* @param degree 水印图片旋转角度,为null表示不旋转

*/

public static String markImageByMoreIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {

String result = "添加图片水印出错";

try {

File file = new File(source);

File ficon = new File(icon);

if (!file.isFile()) {

return source + " 不是一个图片文件!";

}

//将icon加载到内存中

Image ic = ImageIO.read(ficon);

//icon高度

int icheight = ic.getHeight(null);

//将源图片读到内存中

Image img = ImageIO.read(file);

//图片宽

int width = img.getWidth(null);

//图片高

int height = img.getHeight(null);

BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//创建一个指定 BufferedImage 的 Graphics2D 对象

Graphics2D g = bi.createGraphics();

//x,y轴默认是从0坐标开始

int x = 0;

int y = 0;

//默认两张水印图片的间隔高度是水印图片的1/3

int temp = icheight/3;

int space = 1;

if(height>=icheight){

space = height/icheight;

if(space>=2){

temp = y = icheight/2;

if(space==1||space==0){

x = 0;

y = 0;

}

}

}else{

x = 0;

y = 0;

}

//设置对线段的锯齿状边缘处理

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);

//呈现一个图像,在绘制前进行从图像空间到用户空间的转换

g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);

for(int i=0;i

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();

//透明度,最小值为0,最大值为1

float clarity = 0.6f;

g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));

//表示水印图片的坐标位置(x,y)

//g.drawImage(con, 300, 220, null);

g.drawImage(con, x, y, null);

g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

y+=(icheight+temp);

}

g.dispose();

File sf = new File(output, imageName+"."+imageType);

ImageIO.write(bi, imageType, sf); // 保存图片

result = "图片完成添加Icon水印";

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 给图片添加单个图片水印、可设置水印图片旋转角度

* @param icon 水印图片路径(如:F:/images/icon.png)

* @param source 没有加水印的图片路径(如:F:/images/6.jpg)

* @param output 加水印后的图片路径(如:F:/images/)

* @param imageName 图片名称(如:11111)

* @param imageType 图片类型(如:jpg)

* @param degree 水印图片旋转角度,为null表示不旋转

*/

public static String markImageBySingleIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {

String result = "添加图片水印出错";

try {

File file = new File(source);

File ficon = new File(icon);

if (!file.isFile()) {

return source + " 不是一个图片文件!";

}

//将icon加载到内存中

Image ic = ImageIO.read(ficon);

//icon高度

int icheight = ic.getHeight(null);

//将源图片读到内存中

Image img = ImageIO.read(file);

//图片宽

int width = img.getWidth(null);

//图片高

int height = img.getHeight(null);

BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//创建一个指定 BufferedImage 的 Graphics2D 对象

Graphics2D g = bi.createGraphics();

//x,y轴默认是从0坐标开始

int x = 0;

int y = (height/2)-(icheight/2);

//设置对线段的锯齿状边缘处理

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();

//透明度,最小值为0,最大值为1

float clarity = 0.6f;

g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));

//表示水印图片的坐标位置(x,y)

//g.drawImage(con, 300, 220, null);

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); // 保存图片

result = "图片完成添加Icon水印";

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 给图片添加多个文字水印、可设置水印文字旋转角度

* @param source 需要添加水印的图片路径(如:F:/images/6.jpg)

* @param outPut 添加水印后图片输出路径(如:F:/images/)

* @param imageName 图片名称(如:11111)

* @param imageType 图片类型(如:jpg)

* @param color 水印文字的颜色

* @param word 水印文字

* @param degree 水印文字旋转角度,为null表示不旋转

*/

public static String markImageByMoreText(String source,String output,String imageName,String imageType,Color color,String word,Integer degree) {

String result = "添加文字水印出错";

try {

//读取原图片信息

File file = new File(source);

if (!file.isFile()) {

return file + " 不是一个图片文件!";

}

Image img = ImageIO.read(file);

//图片宽

int width = img.getWidth(null);

//图片高

int height = img.getHeight(null);

//文字大小

int size = 50;

//加水印

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics2D g = bi.createGraphics();

g.drawImage(img, 0, 0, width, height, null);

//设置水印字体样式

Font font = new Font("宋体", Font.PLAIN, size);

//根据图片的背景设置水印颜色

g.setColor(color);

int x = width/3;

int y = size;

int space = height/size;

for(int i=0;i

//如果最后一个坐标的y轴比height高,直接退出

if((y+size)>height){

break;

}

if (null != degree) {

//设置水印旋转

g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);

}

g.setFont(font);

//水印位置

g.drawString(word, x, y);

y+=(2*size);

}

g.dispose();

//输出图片

File sf = new File(output, imageName+"."+imageType);

ImageIO.write(bi, imageType, sf); // 保存图片

result = "图片完成添加Word水印";

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 给图片添加单个文字水印、可设置水印文字旋转角度

* @param source 需要添加水印的图片路径(如:F:/images/6.jpg)

* @param outPut 添加水印后图片输出路径(如:F:/images/)

* @param imageName 图片名称(如:11111)

* @param imageType 图片类型(如:jpg)

* @param color 水印文字的颜色

* @param word 水印文字

* @param degree 水印文字旋转角度,为null表示不旋转

*/

public static String markImageBySingleText(String source,String output,String imageName,String imageType,Color color,String word,Integer degree) {

String result = "添加文字水印出错";

try {

//读取原图片信息

File file = new File(source);

if (!file.isFile()) {

return file + " 不是一个图片文件!";

}

Image img = ImageIO.read(file);

int width = img.getWidth(null);

int height = img.getHeight(null);

//加水印

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics2D g = bi.createGraphics();

g.drawImage(img, 0, 0, width, height, null);

//设置水印字体样式

Font font = new Font("宋体", Font.PLAIN, 50);

//根据图片的背景设置水印颜色

g.setColor(color);

if (null != degree) {

//设置水印旋转

g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);

}

g.setFont(font);

int x = width/3;

int y = height/2;

//水印位置

g.drawString(word, x, y);

g.dispose();

//输出图片

File sf = new File(output, imageName+"."+imageType);

ImageIO.write(bi, imageType, sf); // 保存图片

result = "图片完成添加Word水印";

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 给图片加马赛克

* @param source 原图片路径(如:F:/images/6.jpg)

* @param output 打马赛克后,图片保存的路径(如:F:/images/)

* @param imageName 图片名称(如:11111)

* @param imageType 图片类型(如:jpg)

* @param size 马赛克尺寸,即每个矩形的宽高

* @return

*/

public static String markImageByMosaic(String source,String output,String imageName,String imageType,int size){

String result = "图片打马赛克出错";

try{

File file = new File(source);

if (!file.isFile()) {

return file + " 不是一个图片文件!";

}

BufferedImage img = ImageIO.read(file); // 读取该图片

int width = img.getWidth(null); //原图片宽

int height = img.getHeight(null); //原图片高

BufferedImage bi = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);

//马赛克格尺寸太大或太小

if (width < size || height < size) {

return "马赛克格尺寸太大";

}

if(size<=0){

return "马赛克格尺寸太小";

}

int xcount = 0; //x方向绘制个数

int ycount = 0; //y方向绘制个数

if (width % size == 0) {

xcount = width / size;

} else {

xcount = width / size + 1;

}

if (height % size == 0) {

ycount = height / size;

} else {

ycount = height / size + 1;

}

int x = 0; //x坐标

int y = 0;

//y坐标

//绘制马赛克(绘制矩形并填充颜色)

Graphics2D g = bi.createGraphics();

for (int i = 0; i < xcount; i++) {

for (int j = 0; j < ycount; j++) {

//马赛克矩形格大小

int mwidth = size;

int mheight = size;

if(i==xcount-1){ //横向最后一个不够一个size

mwidth = width-x;

}

if(j == ycount-1){ //纵向最后一个不够一个size

mheight = height-y;

}

//矩形颜色取中心像素点RGB值

int centerX = x;

int centerY = y;

if (mwidth % 2 == 0) {

centerX += mwidth / 2;

} else {

centerX += (mwidth - 1) / 2;

}

if (mheight % 2 == 0) {

centerY += mheight / 2;

} else {

centerY += (mheight - 1) / 2;

}

Color color = new Color(img.getRGB(centerX, centerY));

g.setColor(color);

g.fillRect(x, y, mwidth, mheight);

y = y + size;// 计算下一个矩形的y坐标

}

y = 0;// 还原y坐标

x = x + size;// 计算x坐标

}

g.dispose();

File sf = new File(output, imageName+"."+imageType);

ImageIO.write(bi, imageType, sf); // 保存图片

result = "打马赛克成功";

}catch(Exception e){

e.printStackTrace();

}

return result;

}

}

希望本文所述对大家java程序设计有所帮助。

java水印_Java实现给图片添加图片水印,文字水印及马赛克的方法示例相关推荐

  1. 给图片添加多条文字水印和图片水印

    原始图: 水印原始模板: 添加水印后的效果图 package com.sjaco.hy.api.test;import java.awt.image.BufferedImage; import jav ...

  2. 怎样边下载宝贝图片边给图片添加自己的文字水印或图片水印

    今天小编要介绍一个比较常用的技巧,就是如何在下载商品图片时,给商品主图添加一些个性的水印或文字?一起来看看具体的操作步骤吧. 首先复制要抓取的商品链接地址 小编使用的下载图片工具(载图助手),打开进入 ...

  3. java pdf水印排布问题_java实现图片和pdf添加铺满文字水印

    依赖jar包 com.itextpdf itextpdf 5.3.2 com.itextpdf itext-asian 5.2.0 一,图片添加铺满水印 ======================= ...

  4. 图片添加图片水印和文字水印

    title: 图片添加图片水印和文字水印 tags: 图片处理,水印,图片水印,文字水印 date: 2018-10-29 20:15:10 grammar_cjkRuby: true 前言:最近需要 ...

  5. java 多文字水印_Java 如何给Word文档添加多行文字水印

    前言html 我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印.关于文本水印,以前那篇教程里主要指的是单行字体的水印,而在操做Word文档时,有时也会碰到须要添 ...

  6. java怎么给文档加水印_Java 如何给Word文档添加多行文字水印

    前言 我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印.关于文本水印,之前那篇教程里主要指的是单行字体的水印,而在操作Word文档时,有时也会碰到需要添加多行文 ...

  7. 图片添加二维码水印教程

    本博客介绍一下用jdk awt实现图片加文字水印和图片水印的方法 一.图片文字水印 import java.awt.AlphaComposite; import java.awt.Color; imp ...

  8. 通过Python的PIL库给图片添加图片水印

    文章目录 前言 一.素材准备 1.原图 2.水印图 二.使用PIL库给图片添加图片水印 1.引入库 2.定义图片路径 3.打开原图 4.打开水印图片 5.计算水印图片大小 6.计算原图大小 7.调整水 ...

  9. 怎么给图片添加贴纸?介绍几个简单的方法

    不知道大家会不会跟我有同样的想法,不论是经过精心调整拍摄出来的照片,还是平时随手一拍的照片,要发到社交软件上时,都想要添加一些有趣的贴纸或者文字进去.如果照片的内容过于空乏,添加贴纸文字进去会增加照片 ...

最新文章

  1. Codeforces Round #641 (Div. 2)A~E题解(数论场)
  2. MapReduce原理与设计思想
  3. X86智能相机视觉系统解决方案
  4. jdbc获取一行字符串_JDBC基础
  5. CVPR2014: DeepID解读
  6. 流程型企业SCM、ERP、MES、PCS如何集成?
  7. Sentinel底层LongAdder的计数实现
  8. Costco的中国门徒已经参透了零售成功秘笈
  9. idea 部署 web tomcat
  10. 【XSY2731】Div 数论 杜教筛 莫比乌斯反演
  11. doc 问卷调查模板表_问卷调查表格式范本.doc
  12. matlab最小二乘法拟合原理,最小二乘法曲线拟合_原理及matlab实现
  13. hadoop大数据处理平台与案例
  14. 无线射频专题《IEEE 802.11协议讲解1@路由高级配置项,Beacon周期、RTS阈值、DTIM》
  15. 赛灵思计算平台ACAP技术细节全揭秘
  16. regulator linux,关于linux regulator dirver(1) fixed regulator:
  17. 传奇引擎注册服务器,GeeM2引擎架设传奇不能注册账号 进不去游戏
  18. JavaWeb-10课Filter过滤器
  19. 蜂鸣器播放音乐 fpga实现
  20. 机场文员面试从何下手

热门文章

  1. MySQL-distinct关键字
  2. 由椰树椰汁广告想到,关于美女营销
  3. L1-070 吃火锅 (15 分) 吃火锅 (字符串)
  4. C#学习笔记:总结提升
  5. Latex十字 标记
  6. 苹果手机无线里的手动配置服务器,怎么设置苹果手机safari浏览器联网
  7. PTA L1-060:心理阴影面积 (python)
  8. Python实现八大排序算法(转载)+ 桶排序(原创)
  9. html 栅格表单,col 栅格布局
  10. 中国古代兵器与兵书·秦汉军阵