采用Java自带的Image IO

废话不多说,上菜

1.  文字水印

 1 import sun.font.FontDesignMetrics;
2
3 import javax.imageio.ImageIO;
4 import java.awt.*;
5 import java.awt.image.BufferedImage;
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9
10 /**
11 * @Author ChengJianSheng
12 * @Date 2021/6/10
13 */
14 public class WatermarkUtil {
15
16 public static void main(String[] args) throws IOException {
17 addText("F:/1.jpeg", "我的梦想是成为火影");
18 }
19
20 /**
21 * 加文字水印
22 * @param srcPath 原文件路径
23 * @param content 文字内容
24 * @throws IOException
25 */
26 public static void addText(String srcPath, String content) throws IOException {
27 // 读取原图片信息
28 BufferedImage srcImage = ImageIO.read(new File(srcPath));
29 int width = srcImage.getWidth();
30 int height = srcImage.getHeight();
31
32 // 创建画笔,设置绘图区域
33 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
34 Graphics2D g = bufferedImage.createGraphics();
35 g.drawImage(srcImage, 0, 0, width, height, null);
36 // g.drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
37
38 g.setFont(new Font("宋体", Font.PLAIN, 32));
39 g.setColor(Color.RED);
40
41 // 计算文字长度
42 int len = g.getFontMetrics(g.getFont()).charsWidth(content.toCharArray(), 0, content.length());
43 FontDesignMetrics metrics = FontDesignMetrics.getMetrics(g.getFont());
44 int len2 = metrics.stringWidth(content);
45 System.out.println(len);
46 System.out.println(len2);
47
48 // 计算文字坐标
49 int x = width - len - 10;
50 int y = height - 20;
51 // int x = width - 2 * len;
52 // int y = height - 1 * len;
53
54 g.drawString(content, x, y);
55
56 g.dispose();
57
58 // 输出文件
59 FileOutputStream fos = new FileOutputStream("F:/2.png");
60 ImageIO.write(bufferedImage, "png", fos);
61 fos.flush();
62 fos.close();
63 }
64
65 }

可以设置文字透明度

 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.8f));

2.  旋转文字

 1 import sun.font.FontDesignMetrics;
2
3 import javax.imageio.ImageIO;
4 import java.awt.*;
5 import java.awt.geom.AffineTransform;
6 import java.awt.image.BufferedImage;
7 import java.io.File;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10
11 /**
12 * @Author ChengJianSheng
13 * @Date 2021/6/10
14 */
15 public class WatermarkUtil {
16
17 // 水印透明度
18 private static final float alpha = 0.5f;
19 // 水印文字字体
20 private static final Font font = new Font("宋体", Font.BOLD, 30);
21 // 水印文字颜色
22 private static final Color color = Color.RED;
23
24
25 public static void main(String[] args) throws IOException {
26 addText("F:/1.jpeg", "图片由木叶村提供,仅供忍者联军使用!");
27 }
28
29 /**
30 * 加文字水印
31 * @param srcPath 原文件路径
32 * @param content 文字内容
33 * @throws IOException
34 */
35 public static void addText(String srcPath, String content) throws IOException {
36 // 读取原图片信息
37 BufferedImage srcImage = ImageIO.read(new File(srcPath));
38 int width = srcImage.getWidth();
39 int height = srcImage.getHeight();
40
41 // 创建画笔,设置绘图区域
42 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
43 Graphics2D g = bufferedImage.createGraphics();
44 g.drawImage(srcImage, 0, 0, width, height, null);
45 // g.drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
46
47 // 旋转文字
48 AffineTransform affineTransform = g.getTransform();
49 affineTransform.rotate(Math.toRadians(-30), 0, 0);
50 Font rotatedFont = font.deriveFont(affineTransform);
51
52 g.setFont(rotatedFont); // 字体
53 g.setColor(color); // 颜色
54 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 透明度
55
56 // 计算文字长度
57 int len = g.getFontMetrics(g.getFont()).charsWidth(content.toCharArray(), 0, content.length());
58 FontDesignMetrics metrics = FontDesignMetrics.getMetrics(g.getFont());
59 int len2 = metrics.stringWidth(content);
60 System.out.println(len);
61 System.out.println(len2);
62
63 // 计算水印文字坐标
64 int x = width/5;
65 int y = height/3*2;
66
67 g.drawString(content, x, y);
68
69 g.dispose();
70
71 // 输出文件
72 FileOutputStream fos = new FileOutputStream("F:/2.png");
73 ImageIO.write(bufferedImage, "png", fos);
74 fos.flush();
75 fos.close();
76 }
77
78 }

画矩形框

 1 import sun.font.FontDesignMetrics;
2
3 import javax.imageio.ImageIO;
4 import java.awt.*;
5 import java.awt.image.BufferedImage;
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9
10 /**
11 * @Author ChengJianSheng
12 * @Date 2021/6/10
13 */
14 public class WatermarkUtil {
15
16 // 水印透明度
17 private static final float alpha = 0.5f;
18 // 水印文字字体
19 private static final Font font = new Font("宋体", Font.BOLD, 30);
20 // 水印文字颜色
21 private static final Color color = Color.RED;
22
23
24 public static void main(String[] args) throws IOException {
25 addText("F:/1.jpeg", "图片由木叶村提供,仅供忍者联军使用!");
26 }
27
28 /**
29 * 加文字水印
30 * @param srcPath 原文件路径
31 * @param content 文字内容
32 * @throws IOException
33 */
34 public static void addText(String srcPath, String content) throws IOException {
35 // 读取原图片信息
36 BufferedImage srcImage = ImageIO.read(new File(srcPath));
37 int width = srcImage.getWidth();
38 int height = srcImage.getHeight();
39
40 // 创建画笔,设置绘图区域
41 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
42 Graphics2D g = bufferedImage.createGraphics();
43 g.drawImage(srcImage, 0, 0, width, height, null);
44
45 g.setFont(font); // 字体
46 g.setColor(color); // 颜色
47 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 透明度
48
49 // 计算文字宽高度
50 FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
51 int textWidth = metrics.stringWidth(content); // 文字宽度
52 int textHeight = metrics.getHeight(); // 文字高度
53
54 // 计算文字坐标
55 int x = (width - textWidth) / 2;
56 int y = (height + textHeight) / 2;
57 // 写文字
58 g.drawString(content, x, y);
59
60 // 画矩形
61 int padding = 10; // 内边距
62 g.drawRect(x - padding/2, y - textHeight, textWidth + padding, textHeight + padding);
63
64 g.dispose();
65
66 // 输出文件
67 FileOutputStream fos = new FileOutputStream("F:/2.png");
68 ImageIO.write(bufferedImage, "png", fos);
69 fos.flush();
70 fos.close();
71 }
72
73 }

3.  旋转坐标轴

 1 import sun.font.FontDesignMetrics;
2
3 import javax.imageio.ImageIO;
4 import java.awt.*;
5 import java.awt.image.BufferedImage;
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9
10 /**
11 * @Author ChengJianSheng
12 * @Date 2021/6/10
13 */
14 public class WatermarkUtil {
15
16 // 水印透明度
17 private static final float alpha = 0.5f;
18 // 水印文字字体
19 private static final Font font = new Font("宋体", Font.BOLD, 30);
20 // 水印文字颜色
21 private static final Color color = Color.RED;
22
23
24 public static void main(String[] args) throws IOException {
25 addText("F:/1.jpeg", "图片由木叶村提供,仅供忍者联军使用!");
26 }
27
28 /**
29 * 加文字水印
30 * @param srcPath 原文件路径
31 * @param content 文字内容
32 * @throws IOException
33 */
34 public static void addText(String srcPath, String content) throws IOException {
35 // 读取原图片信息
36 BufferedImage srcImage = ImageIO.read(new File(srcPath));
37 int width = srcImage.getWidth();
38 int height = srcImage.getHeight();
39
40 // 创建画笔,设置绘图区域
41 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
42 Graphics2D g = bufferedImage.createGraphics();
43 g.drawImage(srcImage, 0, 0, width, height, null);
44
45 g.setFont(font); // 字体
46 g.setColor(color); // 颜色
47 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 透明度
48
49 // 计算文字宽高度
50 FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
51 int textWidth = metrics.stringWidth(content); // 文字宽度
52 int textHeight = metrics.getHeight(); // 文字高度
53
54 // 旋转坐标轴
55 g.translate(-width/5, height/4);
56 g.rotate(-30*Math.PI/180);
57
58 // 计算文字坐标
59 int x = (width - textWidth) / 2;
60 int y = (height + textHeight) / 2;
61 // 写文字
62 g.drawString(content, x, y);
63
64 // 画矩形
65 int padding = 10; // 内边距
66 g.drawRect(x - padding/2, y - textHeight, textWidth + padding, textHeight + padding);
67
68 g.dispose();
69
70 // 输出文件
71 FileOutputStream fos = new FileOutputStream("F:/2.png");
72 ImageIO.write(bufferedImage, "png", fos);
73 fos.flush();
74 fos.close();
75 }
76
77 }

结合下载功能,完整的代码如下:

 1 /**
2 * 下载
3 */
4 @GetMapping("/download")
5 public void download(@RequestParam("mediaId") String mediaId, HttpServletRequest request, HttpServletResponse response) throws IOException {
6 SysFile sysFile = sysFileService.getByMediaId(mediaId);
7 if (null == sysFile) {
8 throw new IllegalArgumentException("文件不存在");
9 }
10 String mimeType = request.getServletContext().getMimeType(sysFile.getPath());
11 System.out.println(mimeType);
12 response.setContentType(sysFile.getContentType());
13 response.setHeader(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=" + URLEncoder.encode(sysFile.getOriginalFilename(), "UTF-8"));
14 // FileInputStream fis = new FileInputStream(sysFile.getPath());
15 ServletOutputStream sos = response.getOutputStream();
16 WatermarkUtil.addText(sysFile.getPath(), "哈哈哈哈", sos);
17 // IOUtils.copy(fis, sos);
18 // IOUtils.closeQuietly(fis);
19 IOUtils.closeQuietly(sos);
20 }
 1
3 import sun.font.FontDesignMetrics;
4
5 import javax.imageio.ImageIO;
6 import java.awt.*;
7 import java.awt.image.BufferedImage;
8 import java.io.File;
9 import java.io.IOException;
10 import java.io.OutputStream;
11
12 /**
13 * @Author ChengJianSheng
14 * @Date 2021/6/10
15 */
16 public class WatermarkUtil {
17
18 // 水印透明度
19 private static final float alpha = 0.5f;
20 // 水印文字字体
21 private static final Font font = new Font("宋体", Font.BOLD, 30);
22 // 水印文字颜色
23 private static final Color color = Color.RED;
24
25 /**
26 * 加文字水印
27 * @param srcPath 原文件路径
28 * @param content 文字内容
29 * @throws IOException
30 */
31 public static void addText(String srcPath, String content, OutputStream outputStream) throws IOException {
32 // 读取原图片信息
33 BufferedImage srcImage = ImageIO.read(new File(srcPath));
34 int width = srcImage.getWidth();
35 int height = srcImage.getHeight();
36
37 // 画板
38 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
39 // 画笔
40 Graphics2D g = bufferedImage.createGraphics();
41 g.drawImage(srcImage, 0, 0, width, height, null);
42
43 g.setFont(font); // 字体
44 g.setColor(color); // 颜色
45 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 透明度
46
47 // 计算文字宽高度
48 FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
49 int textWidth = metrics.stringWidth(content); // 文字宽度
50 int textHeight = metrics.getHeight(); // 文字高度
51
52 // 旋转坐标轴
53 g.translate(-width/5, height/4);
54 g.rotate(-30*Math.PI/180);
55
56 // 计算文字坐标
57 int x = (width - textWidth) / 2;
58 int y = (height + textHeight) / 2;
59 // 写文字
60 g.drawString(content, x, y);
61
62 // 画矩形
63 int padding = 10; // 内边距
64 g.drawRect(x - padding/2, y - textHeight, textWidth + padding, textHeight + padding);
65
66 g.dispose();
67
68 // 输出
69 ImageIO.write(bufferedImage, "png", outputStream);
70 }
71
72 }

Java图片加水印的更多相关文章

  1. java 图片加水印,设置透明度。说明非常具体

    package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.aw ...

  2. Java图片加文字水印

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

  3. Java图片处理(二)图片加水印

    图片加水印,是通过图片重叠绘制实现的.实现代码如下: public static void press(String pressImg, String pressText, String target ...

  4. 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化

    <?php /* [LocoySpider] (C)2005-2010 Lewell Inc. 火车采集器 DedeCMS 5.7 UTF8 文章发布接口 Update content: 图片加 ...

  5. thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

    今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载 ...

  6. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  7. php 分享两种给图片加水印的方法

    本文章向码农们介绍 php 给图片加水印的两种方法,感兴趣的码农可以参考一下本文章的源代码. 方法一:PHP最简单的加水印方法 <?php // http://www.manongjc.com ...

  8. PHP给图片加水印

    <?php /** *图片加水印 *@param $srcImg 原图 *@param $waterImg 水印图片 *@param $savepath 保存路径 *@param $savena ...

  9. 如何用node.js批量给图片加水印

    上一篇我们讲了如何用node.js给图片加水印,但是只是给某一张图片加,并没有涉及到批量处理.这一篇,我们学习如果批量进行图片加水印处理. 一.准备工作: 首先,你要阅读完这篇文章:http://ww ...

  10. 使用 ImageEnView 给图片加水印,及建缩略图

    摘要: 使用 ImageEnView 给图片加水印,及建缩略图 {Power by hzqghost@21cn.com}unit CutWater; interface uses  Math,imag ...

随机推荐

  1. SV Interface and Program 2

    Clocking:激励的时序 memory检测start信号,当start上升沿的时候,如果write信号拉高之后,将data存储到mem中 start\write\addr\data - 四个信号是 ...

  2. Maven项目手动配置依赖项

    1.问题 很多时候,我们依靠其本身的识别功能,并不能很好的识别依赖项(尤其是指定版本),且对于一些位于 <\build>不能自动去下载,这时候我们就要去手动配置依赖项 2.解决 2.1 首 ...

  3. [转帖]Nginx 使用与异常处理

    http://jartto.wang/2017/04/15/nginx-exception-handling/ 以前总是偷懒使用 Http-Server 来启动一个本地服务,后来花时间学习了一下 Ng ...

  4. [转帖]Arm CPU风起,补齐国产大芯片最后一块拼图 | 甲子光年

    https://rmh.pdnews.cn/Pc/ArtInfoApi/article?id=30960014 最近一年,国产CPU火了. 今年6月24日,龙芯中科在科创板挂牌上市,公司主营自研Loo ...

  5. [转帖]oom-killer错误排查过程

    https://www.cnblogs.com/hphua/p/16395893.html 1.遇到的问题:应用在hi3536上跑一段不固定的时间,随之就会出现重启的现象:打印如下: app-run ...

  6. Linux 查找并且复制部分文件到其他目录的办法(find xargs {})

    最近经常需要从某些文件夹查找部分文件,然后复制到其他目录里面进行进一步的处理 shell 脚本一直在不断的学习中, 最近发现之前看文档还是有疏漏. find . -iname "*fi*&q ...

  7. 海量数据 vastbase G100 V2.2安装简单总结

    海量数据vastbase G100 V2.2 安装总结 背景说明 最近进行信创四期的数据库兼容性验证, 获取了海量数据的一个信创名录内的安装介质. 一直忙于出差, 今天晚上趁着冬至回家比较早在家里进行 ...

  8. 飞腾2000+上面银河麒麟v10 安装virt-manager创建虚拟机的操作过程

    操作系统安装完之后自带了repos 就可以执行大部分操作, 不需要修改包源 ###Kylin Linux Advanced Server 10 - os repo### [ks10-adv-os] n ...

  9. 拜占庭将军问题和 Raft 共识算法讲解

    作者: 京东物流 郭益如 导读 在分布式系统中, 什么是拜占庭将军问题?产生的场景和解决方案是什么?什么是 Raft 共识算法?Raft 算法是如何解决拜占庭将军问题的?其核心原理和算法逻辑是什么?除 ...

  10. 【JS 逆向百例】有道翻译接口参数逆向

    逆向目标 目标:有道翻译接口参数 主页:https://fanyi.youdao.com/ 接口:https://fanyi.youdao.com/translate_o?smartresult=di ...