* @Description:图片水印<br>
 *  图片水印实现思路:<br>
 *  1.创建缓存图片对象--BufferedImage<br>
 *  2.创建java绘图工具对象--Graphics2D<br>
 *  3.使用绘图工具对象将原图绘制到缓存图片对象<br>
 *  4.使用绘图工具将水印(文字或图片)绘制到缓存图片对象<br>
 *  5.创建图片编码工具类--JPEGImageEncoder(由于java7以后jdk默认读取不到JPEGImageEncoder的rt.jar使用ImageIO实现编码输出)<br>
 *  6.使用图片编码工具类数据缓存图像到目标图片文件<br>

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
* @ClassName:WaterMarkServiceImpl.java
* @Description:图片水印
*  图片水印实现思路:
*  1.创建缓存图片对象--BufferedImage
*  2.创建java绘图工具对象--Graphics2D
*  3.使用绘图工具对象将原图绘制到缓存图片对象
*  4.使用绘图工具将水印(文字或图片)绘制到缓存图片对象
*  5.创建图片编码工具类--JPEGImageEncoder(由于java7以后jdk默认读取不到JPEGImageEncoder的rt.jar使用ImageIO实现编码输出)
*  6.使用图片编码工具类数据缓存图像到目标图片文件
* @author yunfei.qi
* @date 2016年12月15日 上午11:30:12
*/
public class WaterMarkServiceImpl implements WaterMarkService{
//水印文字
public static final String MARK_TEXT="测试水印";
//字体
public static final String FONT_NAME="华文行楷";
//字体样式
public static final int FONT_STYLE=Font.BOLD;
//字体大小 单位像素
public static final int FONT_SIZE=120;
//文字颜色
public static final Color FONT_COLOR=Color.BLUE;
//文字水印透明度 30%
public static float ALPHA=0.3F;
//绘制位置横坐标
public static final int X=120;
//绘制位置纵坐标
public static final int Y=120;
public static final String  BASE_PATH="C:\\tempDir\\";
public static final String  MARK_LOGO_IMAGE="C:\\tempDir\\logo.png";
public static final String  FORMAT_NAME="jpg";
/**
* 文字水印
* @param image
* @param imageFileName
* @return
*/
public String waterMarkBySingleText(File image,String imageFileName){
String logoFileName="2_logo_single_text_"+imageFileName;
OutputStream outs=null;
try {
//通过ImageIO获取图像文件的像素大小 即宽/高
Image imageTemp=ImageIO.read(image);
int width=imageTemp.getWidth(null);
int height=imageTemp.getHeight(null);
//1.创建缓存图片对象--BufferedImage
/**
* new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
* width width of the created image
* height height of the created image
* imageType type of the created image
*/
BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2.创建java绘图工具对象--Graphics2D
Graphics2D graphics2D=bufferedImage.createGraphics();
//3.使用绘图工具对象将原图绘制到缓存图片对象
/**
* img the specified image to be drawn. This method does nothing if img is null.
* x the x coordinate.
* y the y coordinate.
* width the width of the rectangle.
* height the height of the rectangle.
* observer object to be notified as more of the image is converted.
*/
graphics2D.drawImage(imageTemp, 0, 0, width,height,null);
//设置水印文字 字体 样式 大小
graphics2D.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
///设置水印文字颜色
graphics2D.setColor(FONT_COLOR);
/**
* 获取水印文本的高和宽度
* 高度:即文字的像素大小 -->FONT_SIZE
* 宽度:中文字符 1:1 即中文字符-->FONT_SIZE 英文字符1:2-->FONT_SIZE/2
*/
int waterMarkWidth=FONT_SIZE*getTextLength(MARK_TEXT);
int waterMarkHeight=FONT_SIZE;
//计算图片尺寸和水印尺寸差异
int widthDiff=width-waterMarkWidth;
int heightDiff=height-waterMarkHeight;
int x=X;
int y=Y;
if(x>widthDiff){
x=widthDiff;
}
if(y>heightDiff){
y=heightDiff;
}
//设置水印透明度 0.3
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
//4.使用绘图工具将水印(文字或图片)绘制到缓存图片对象
graphics2D.drawString(MARK_TEXT, x, y+FONT_SIZE);
//释放工具
graphics2D.dispose();
outs=new FileOutputStream(BASE_PATH+logoFileName);
//5.创建图片编码工具类--JPEGImageEncoder
ImageIO.write(bufferedImage, FORMAT_NAME, outs);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(outs!=null){
try {
outs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return logoFileName;
}
/**
* 获取文本宽度
* @param text
* @return
*/
public static int getTextLength(String text){
if(text==null || "".equals(text)){
return 0;
}
int length=text.length();
for(int i=0;i0){
length++;
}
}
length=length%2==0?length/2:length/2+1;
return length;
}
/**
* @param image
* @param imageFileName
* @return
*/
public String waterMarkBySingleImage(File image,String imageFileName){
String logoFileName="logo_single_image_"+imageFileName;
OutputStream outs=null;
String logoPath=MARK_LOGO_IMAGE;
try {
//通过ImageIO获取图像文件的像素大小 即宽/高
Image imageTemp=ImageIO.read(image);
int width=imageTemp.getWidth(null);
int height=imageTemp.getHeight(null);
//1.创建缓存图片对象--BufferedImage
/**
* new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
* width width of the created image
* height height of the created image
* imageType type of the created image
*/
BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2.创建java绘图工具对象--Graphics2D
Graphics2D graphics2D=bufferedImage.createGraphics();
//3.使用绘图工具对象将原图绘制到缓存图片对象
/**
* img the specified image to be drawn. This method does nothing if img is null.
* x the x coordinate.
* y the y coordinate.
* width the width of the rectangle.
* height the height of the rectangle.
* observer object to be notified as more of the image is converted.
*/
graphics2D.drawImage(imageTemp, 0, 0, width,height,null);
File logo=new File(logoPath);
Image logoImage=ImageIO.read(logo);
/**
* 获取水印图片的高和宽度
*/
int waterMarkWidth=logoImage.getWidth(null);
int waterMarkHeight=logoImage.getHeight(null);
//计算图片尺寸和水印尺寸差异
int widthDiff=width-waterMarkWidth;
int heightDiff=height-waterMarkHeight;
int x=X;
int y=Y;
if(x>widthDiff){
x=widthDiff;
}
if(y>heightDiff){
y=heightDiff;
}
//设置水印透明度 0.3
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
//4.使用绘图工具将水印(文字或图片)绘制到缓存图片对象
graphics2D.drawImage(logoImage, x, y,null);
//释放工具
graphics2D.dispose();
outs=new FileOutputStream(BASE_PATH+logoFileName);
//5.创建图片编码工具类--JPEGImageEncoder
ImageIO.write(bufferedImage, FORMAT_NAME, outs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(outs!=null){
try {
outs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return logoFileName;
}
public String waterMarkByMultiText(File image,String imageFileName){
String logoFileName="logo_multi_text_"+imageFileName;
OutputStream outs=null;
try {
//通过ImageIO获取图像文件的像素大小 即宽/高
Image imageTemp=ImageIO.read(image);
int width=imageTemp.getWidth(null);
int height=imageTemp.getHeight(null);
//1.创建缓存图片对象--BufferedImage
/**
* new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
* width width of the created image
* height height of the created image
* imageType type of the created image
*/
BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2.创建java绘图工具对象--Graphics2D
Graphics2D graphics2D=bufferedImage.createGraphics();
//3.使用绘图工具对象将原图绘制到缓存图片对象
/**
* img the specified image to be drawn. This method does nothing if img is null.
* x the x coordinate.
* y the y coordinate.
* width the width of the rectangle.
* height the height of the rectangle.
* observer object to be notified as more of the image is converted.
*/
graphics2D.drawImage(imageTemp, 0, 0, width,height,null);
//设置水印文字 字体 样式 大小
graphics2D.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
///设置水印文字颜色
graphics2D.setColor(FONT_COLOR);
//设置水印透明度 0.3
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
//将画布旋转 Math.toRadians(30)弧度
graphics2D.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);
/**
* 获取水印文本的高和宽度
* 高度:即文字的像素大小 -->FONT_SIZE
* 宽度:中文字符 1:1 即中文字符-->FONT_SIZE 英文字符1:2-->FONT_SIZE/2
*/
int waterMarkWidth=FONT_SIZE*getTextLength(MARK_TEXT);
int waterMarkHeight=FONT_SIZE;
int x=-width/2;
int y=-height/2;
while(x0){
for(int i=0;i

java 实现图片水印 文字水印相关推荐

  1. JAVA - base64图片加文字水印

    场景为:前端传入转码后的base64图片字符串,后台加水印并转为图片,再上传 使用postman调试接口时,总会出现400bad request的情况 若是把图片转码的base64编码放在header ...

  2. JAVA 给图片添加文字水印

    水印操作有很多,例如:给图片添加文字.图片水印,给pdf文件添加水印,给文件加盖公章,这类需求还是时常会遇到的,今天就简单记录一下给图片添加文字水印的demo,仅供大家参考,后续会写别的情况的添加水印 ...

  3. Java给图片添加文字水印

    闲着没事,研究了下图片水印的事儿,图片水印虽然恶心,而且大大的影响了图片的美观,试想一下,一张美女的性感写真照,下方来了个大大的水印"XXXX所有",看着那猥琐的文字水印,是不是很 ...

  4. Java给图片添加文字,水印,文件或者http图片地址,可消除文字锯齿

    记一次Java给图片添加水印方法 Java给图片在指定位置加水印的小工具, 可操作文件, 或者http地址图片,转base64或者直接输出都可 核心代码先贴出来 水印基本信息的一个封装 import ...

  5. 用Java给图片添加文字水印

    代码: public static void main(String[] args) throws IOException {Color color=new Color(255,0,0);Font f ...

  6. ImageUtils.java:图片处理工具类[裁剪/图片水印/文字水印/缩放补白/Base64加密解密]

    用到的jar包: commons-beanutils-1.8.0.jar commons-codec-1.10.jar package com.zhenzhigu.commons.util;   im ...

  7. Java图片加文字水印

    Java图片加文字水印 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.I ...

  8. Java分享--给图片添加文字水印(文字可旋转)

    这篇文章主要介绍了Java实现给图片添加文字水印,文字水印的方法,涉及java针对图片的读取.水印添加设置等相关操作技巧,需要的朋友可以参考下 . 很多时候项目中的图片需要一定的版权,就是人家拿出去用 ...

  9. SpringBoot html转pdf 支持中文、图片水印+文字水印、页眉页脚 flying-saucer-pdf-itext5 + freemarker

    使用 flying-saucer-pdf-itext5加freemarker生成pdf,支持中文.图片水印+文字水印.页眉页脚. 引入jar包 <!-- freemarker --> &l ...

  10. android水印控件,Android图片添加文字水印并保存水印文字图片到指定文件

    Android图片添加文字水印并保存水印文字图片到指定文件package zhangphil.test;import android.graphics.Bitmap;import android.gr ...

最新文章

  1. java二级选择题要对一半吗_据说一半以上的java程序员会出错的题
  2. Ajax表单提交给C#后台选中的checkbox值
  3. 联想云:借助云计算助力中国企业数字化转型
  4. 凉了!张三同学没答好,熬夜整理最新大厂Java高频面试题
  5. 状态模式(Strategy Pattern)
  6. matlab图像的读取和保存
  7. GCC编译器的使用方法
  8. SSM+Bootstrap+MYSQL演唱会网上订票系统
  9. 中国民用航空飞行学院 - 人事工资薪酬管理系统
  10. macbookpro2011安装单系统win10
  11. 【FXCG】多头陷阱知多少
  12. Segmentation
  13. html5人脸拼图,layout拼图
  14. Python中Selenium模块的使用
  15. 融云发送图片消息_融云开发者文档
  16. 世界各国当日数据探索性分析
  17. python tkinter Entry的使用
  18. 4G 5G 频段介绍
  19. 小白的高德地图初体验(一) —— 打点
  20. 《统计学习方法》 第十七章 潜在语义分析

热门文章

  1. base64 文件格式判断 图片类型判断 js
  2. Redis中的Stream的实现Radix Tree源码解读
  3. 上传图片转为base64码再以url形式传值
  4. 复现论文常用函数(一)tf.one_hot,tf.train.batch,tf.train.shuffle_batch,数据读取机制,获取文件路径,Bunch等
  5. UnicodeTOGB,能够将Unicode串转换成GB码
  6. java.lang.SecurityException: com.example.rxtest was not granted this permission: android.permission
  7. 第一天的学习内容----Excel自动化处理
  8. 兴趣专业测试软件,测试你的专业兴趣是什么
  9. MVC进阶学习--View和Controller之间的数据传递(一)
  10. 8cm等于多少像素_PPT尺寸你们都设置成多少(我问的不是分辨率像素,而是长、高尺寸)?...