java 实现图片水印 文字水印

本文介绍了如何使用Java实现图片水印功能,主要步骤包括:创建BufferedImage对象,利用Graphics2D绘制原图和水印,最后通过JPEGImageEncoder或ImageIO将处理后的图像保存到目标文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 * @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(x 0){ for(int i=0;i

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值