亲宝软件园·资讯

展开

JAVA图片水印 JAVA图片水印开发案例详解

老爷爷 人气:0
想了解JAVA图片水印开发案例详解的相关内容吗,老爷爷在本文为您仔细讲解JAVA图片水印的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java图片水印,java单文字水印,java单图片水印,java多文字水印,下面大家一起来学习吧。

写在最前面

上周零零碎碎花了一周的时间研究水印的开发,现在终于写了个入门级的Demo,做下笔记同时分享出来供大家参考。

Demo是在我上次写的 JAVA实用案例之文件导入导出(POI方式) 框架基础上搭建的,基于Spring+SpringMVC。如果有错误还请大家指正。

最后源码地址在:https://github.com/allanzhuo/myport.git

简单介绍

水印开发是web开发中一种比较常见的功能,实现的代码很简单,具体的实现步骤我也会以代码为基础详细讲述。其实以我个人的理解,我把水印的类型和开发流程分为以下几种。

水印的类型:

单文字水印
单图片水印
多文字水印
多图片水印

水印的开发流程:

效果图:

上传页:

原图:

单文字水印:

单图片水印:

多文字水印:

多图片水印:

单文字水印开发

所谓但文字水印,就是在一张图片上添加一条文字水印。其中我们主要的流程是通过ImageIO工具类解码对应的图片,然后创建BufferImage对象,通过BufferImage对象创建Graphics2D对象,再通过Graphics2D对象绘制原图到BufferImage对象。然后,我们还可以使用Graphics2D对象来设置水印的相关信息,如水印内容、字体大小、字体风格等。
这里需要说明的是我们需要计算水印文本的宽度,中文长度即文本宽度,英文长度为文本宽度的二分之一。具体可以参考我源码中的相关内容。

 //计算水印文本长度
 //1、中文长度即文本长度 2、英文长度为文本长度二分之一
 public int getTextLength(String text){
 //水印文字长度
 int length = 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;
 return length;
 }

//添加单条文字水印方法
public String textWaterMark(MultipartFile myFile,String imageFileName) {
InputStream is =null;
OutputStream os =null;
int X = 636;
int Y = 700;

 try {
  //使用ImageIO解码图片
  Image image = ImageIO.read(myFile.getInputStream());
  //计算原始图片宽度长度
  int width = image.getWidth(null);
  int height = image.getHeight(null);
  //创建图片缓存对象
  BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
  //创建java绘图工具对象
  Graphics2D graphics2d = bufferedImage.createGraphics();
  //参数主要是,原图,坐标,宽高
  graphics2d.drawImage(image, 0, 0, width, height, null);
  graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
  graphics2d.setColor(FONT_COLOR);

  //使用绘图工具将水印绘制到图片上
  //计算文字水印宽高值
  int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT);
  int waterHeight = FONT_SIZE;
  //计算水印与原图高宽差
  int widthDiff = width-waterWidth;
  int heightDiff = height-waterHeight;
  //水印坐标设置
  if (X > widthDiff) {
  X = widthDiff;
  }
  if (Y > heightDiff) {
  Y = heightDiff;
  }
  //水印透明设置
  graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
  //纵坐标在下方,不增加字体高度会靠上
  graphics2d.drawString(MARK_TEXT, X, Y+FONT_SIZE);

  graphics2d.dispose();
  os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName);
  //创建图像编码工具类
  JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
  //使用图像编码工具类,输出缓存图像到目标文件
  en.encode(bufferedImage);
  if(is!=null){ 
  is.close();
  }
  if(os!=null){
  os.close();
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 return "success";
 }

单图片水印开发

单图片水印和上面单文字的代码流程大致一致,这里只讲解不同之处。
首先我们需要获得水印图片的路径,然后创建水印文件对象,同样通过ImageIO工具类解码水印图片,中间我们就不需要计算文本长宽了,因为单文字中的长宽即是我们水印图片的长宽。

  //水印图片路径
  //水印坐标设置
  String logoPath = "/img/logo.png";
  String realPath = request.getSession().getServletContext().getRealPath(logoPath);
  File logo = new File(realPath);
  Image imageLogo = ImageIO.read(logo);
  int widthLogo = imageLogo.getWidth(null);
  int heightLogo = imageLogo.getHeight(null);
  int widthDiff = width-widthLogo;
  int heightDiff = height-heightLogo;
  //水印坐标设置
  if (X > widthDiff) {
  X = widthDiff;
  }
  if (Y > heightDiff) {
  Y = heightDiff;
  }
  //水印透明设置
  graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
  graphics2d.drawImage(imageLogo, X, Y, null);

多文字水印开发

其实多文字水印开发和单文字也是类似的,主要的不同点是我们需要将BufferImage对象进行旋转。因为绘制水印并不支持旋转水印绘制,所以我们需要对原图进行旋转绘制,然后通过循环,我们就可以将一个文字水印多次绘制在原图上了。

 //旋转原图,注意旋转角度为弧度制。后面两个参数为旋转的坐标中心
  graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);

  int x = -width/2;
  int y = -height/2;

  while(x < width*1.5){
  y = -height/2;
  while(y < height*1.5){
   graphics2d.drawString(MARK_TEXT, x, y);
   y+=waterHeight+100;
  }
  x+=waterWidth+100;
  }

多图片水印开发

与上文相同,多图片水印需要先读取水印图片,然后对水印设置透明度,在对原图进行旋转,然后通过循环,我们就可以将一个图片水印多次绘制在原图上。

 //水印图片路径
  String logoPath = "/img/logo.png";
  String realPath = request.getSession().getServletContext().getRealPath(logoPath);
  File logo = new File(realPath);
  Image imageLogo = ImageIO.read(logo);
  int widthLogo = imageLogo.getWidth(null);
  int heightLogo = imageLogo.getHeight(null);
  
  //水印透明设置
  graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
  
  graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);
  
  int x = -width/2;
  int y = -height/2;

  while(x < width*1.5){
  y = -height/2;
  while(y < height*1.5){
   graphics2d.drawImage(imageLogo, x, y, null);
   y+=heightLogo+100;
  }
  x+=widthLogo+100;
  }

业务类完整代码:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.allan.service.WaterMarkService;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
@Service
public class WaterMarkServiceImpl implements WaterMarkService{
 //定义上传的文件夹
 private static final String UPLOAD_PATH = "E:/save";
 //定义水印文字样式
 private static final String MARK_TEXT = "小卖铺的老爷爷";
 private static final String FONT_NAME = "微软雅黑";
 private static final int FONT_STYLE = Font.BOLD;
 private static final int FONT_SIZE = 60;
 private static final Color FONT_COLOR = Color.black;


 private static final float ALPHA = 0.3F;


 //1、上传图片
 public String uploadImage(MultipartFile myFile,String imageFileName) {
 InputStream is =null;
 OutputStream os =null;
 try{
 is = myFile.getInputStream();
 os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName);
 byte[] buffer =new byte[1024];
 int len = 0;

 while ((len=is.read(buffer))>0){
 os.write(buffer);
 }

 }catch(Exception e){
 e.printStackTrace();
 }finally{
 if(is!=null){
 try {
  is.close();
 } catch (IOException e) {

  e.printStackTrace();
 }
 }
 if(os!=null){
 try {
  os.close();
 } catch (IOException e2) {
  e2.printStackTrace();
 }
 }
 }

 return "success";

 }
 //添加单条文字水印
 public String textWaterMark(MultipartFile myFile,String imageFileName) {
 InputStream is =null;
 OutputStream os =null;
 int X = 636;
 int Y = 700;

 try {
 Image image = ImageIO.read(myFile.getInputStream());
 //计算原始图片宽度长度
 int width = image.getWidth(null);
 int height = image.getHeight(null);
 //创建图片缓存对象
 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
 //创建java绘图工具对象
 Graphics2D graphics2d = bufferedImage.createGraphics();
 //参数主要是,原图,坐标,宽高
 graphics2d.drawImage(image, 0, 0, width, height, null);
 graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
 graphics2d.setColor(FONT_COLOR);

 //使用绘图工具将水印绘制到图片上
 //计算文字水印宽高值
 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT);
 int waterHeight = FONT_SIZE;
 //计算水印与原图高宽差
 int widthDiff = width-waterWidth;
 int heightDiff = height-waterHeight;
 //水印坐标设置
 if (X > widthDiff) {
 X = widthDiff;
 }
 if (Y > heightDiff) {
 Y = heightDiff;
 }
 //水印透明设置
 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
 graphics2d.drawString(MARK_TEXT, X, Y+FONT_SIZE);

 graphics2d.dispose();
 os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName);
 //创建图像编码工具类
 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
 //使用图像编码工具类,输出缓存图像到目标文件
 en.encode(bufferedImage);
 if(is!=null){ 
 is.close();
 }
 if(os!=null){
 os.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 return "success";
 }

 //添加单图片水印
 public String imageWaterMark(MultipartFile myFile,String imageFileName,HttpServletRequest request) {
 InputStream is =null;
 OutputStream os =null;
 int X = 636;
 int Y = 763;

 try {
 Image image = ImageIO.read(myFile.getInputStream());
 //计算原始图片宽度长度
 int width = image.getWidth(null);
 int height = image.getHeight(null);
 //创建图片缓存对象
 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
 //创建java绘图工具对象
 Graphics2D graphics2d = bufferedImage.createGraphics();
 //参数主要是,原图,坐标,宽高
 graphics2d.drawImage(image, 0, 0, width, height, null);
 graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
 graphics2d.setColor(FONT_COLOR);

 //水印图片路径
 String logoPath = "/img/logo.png";
 String realPath = request.getSession().getServletContext().getRealPath(logoPath);
 File logo = new File(realPath);
 Image imageLogo = ImageIO.read(logo);
 int widthLogo = imageLogo.getWidth(null);
 int heightLogo = imageLogo.getHeight(null);
 int widthDiff = width-widthLogo;
 int heightDiff = height-heightLogo;
 //水印坐标设置
 if (X > widthDiff) {
 X = widthDiff;
 }
 if (Y > heightDiff) {
 Y = heightDiff;
 }
 //水印透明设置
 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
 graphics2d.drawImage(imageLogo, X, Y, null);

 graphics2d.dispose();
 os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName);
 //创建图像编码工具类
 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
 //使用图像编码工具类,输出缓存图像到目标文件
 en.encode(bufferedImage);
 if(is!=null){ 
 is.close();
 }
 if(os!=null){
 os.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 return "success";
 }
 //添加多条文字水印
 public String moreTextWaterMark(MultipartFile myFile,String imageFileName) {
 InputStream is =null;
 OutputStream os =null;
 int X = 636;
 int Y = 763;

 try {
 Image image = ImageIO.read(myFile.getInputStream());
 //计算原始图片宽度长度
 int width = image.getWidth(null);
 int height = image.getHeight(null);
 //创建图片缓存对象
 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
 //创建java绘图工具对象
 Graphics2D graphics2d = bufferedImage.createGraphics();
 //参数主要是,原图,坐标,宽高
 graphics2d.drawImage(image, 0, 0, width, height, null);
 graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
 graphics2d.setColor(FONT_COLOR);

 //使用绘图工具将水印绘制到图片上
 //计算文字水印宽高值
 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT);
 int waterHeight = FONT_SIZE;

 //水印透明设置
 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
 graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);

 int x = -width/2;
 int y = -height/2;

 while(x < width*1.5){
 y = -height/2;
 while(y < height*1.5){
  graphics2d.drawString(MARK_TEXT, x, y);
  y+=waterHeight+100;
 }
 x+=waterWidth+100;
 }
 graphics2d.dispose();

 os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName);
 //创建图像编码工具类
 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
 //使用图像编码工具类,输出缓存图像到目标文件
 en.encode(bufferedImage);
 if(is!=null){ 
 is.close();
 }
 if(os!=null){
 os.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 return "success";
 }

 //多图片水印
 public String moreImageWaterMark(MultipartFile myFile,String imageFileName,HttpServletRequest request) {
 InputStream is =null;
 OutputStream os =null;
 int X = 636;
 int Y = 763;

 try {
 Image image = ImageIO.read(myFile.getInputStream());
 //计算原始图片宽度长度
 int width = image.getWidth(null);
 int height = image.getHeight(null);
 //创建图片缓存对象
 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
 //创建java绘图工具对象
 Graphics2D graphics2d = bufferedImage.createGraphics();
 //参数主要是,原图,坐标,宽高
 graphics2d.drawImage(image, 0, 0, width, height, null);
 graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
 graphics2d.setColor(FONT_COLOR);

 //水印图片路径
 String logoPath = "/img/logo.png";
 String realPath = request.getSession().getServletContext().getRealPath(logoPath);
 File logo = new File(realPath);
 Image imageLogo = ImageIO.read(logo);
 int widthLogo = imageLogo.getWidth(null);
 int heightLogo = imageLogo.getHeight(null);
 
 //水印透明设置
 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
 
 graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);
 
 int x = -width/2;
 int y = -height/2;

 while(x < width*1.5){
 y = -height/2;
 while(y < height*1.5){
  graphics2d.drawImage(imageLogo, x, y, null);
  y+=heightLogo+100;
 }
 x+=widthLogo+100;
 }
 graphics2d.dispose();
 os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName);
 //创建图像编码工具类
 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
 //使用图像编码工具类,输出缓存图像到目标文件
 en.encode(bufferedImage);
 if(is!=null){ 
 is.close();
 }
 if(os!=null){
 os.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 return "success";
 }

 //计算水印文本长度
 //1、中文长度即文本长度 2、英文长度为文本长度二分之一
 public int getTextLength(String text){
 //水印文字长度
 int length = 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;
 return length;
 }
}

最后再说明下,本Demo是在上次的文件导入导出的框架基础上编写的,源码中有些其它Demo的代码,主要使用的类有WaterMarkController.java、WaterMarkService.java、WaterMarkServiceImpl.java,因为代码中我是硬编码到E:/save文件夹下的,如果要运行的话,还请先新建此文件夹,或者改为其他文件夹也行。

源码地址:https://github.com/allanzhuo/myport.git

加载全部内容

相关教程
猜你喜欢
用户评论