Java实现背景图片加自适应水印图片

由于每张图片的宽高比例不一致,希望使得水印在每张图上的尺寸可以跟随背景图变化,自动调整水印大小。避免每张背景的图所显示的logo水印不至于那么突兀。

一、导入所需Jar包

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.3</version> <!-- 请检查最新版本 --></dependency>

二、编写工具类

package com.example.file_handle.img_add_water_mark;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;/*** @author: reshui* description: 添加水印图片工具类* DateTime:2025/4/23-15:15*/
@Slf4j
public class ImageWaterMarkUtil {/*** 文件暂存地址*/public static final String TEMP_FILE_PATH = System.getProperty("java.io.tmpdir");/*** 水印添加后文件存放地址*/private static final String WATER_MARK_FILE_PATH = TEMP_FILE_PATH + File.separator + "water_mark_after";/*** 时间格式*/public static String TIMESTAMP_FORMAT = "yyyyMMddHHmmss";/*** 水印图片url*/public static String WATER_MARK_IMG_URL = "https://profile-avatar.csdnimg.cn/128f2647a3ac408eafb94c7a6706689b_weixin_42477252.jpg!1";/*** 添加水印图片** @param originImgList 原始图片url* @param ratio         水印图片占背景图片的比例*/public static List<File> imgUrlAddWaterMarkForFileResult(List<String> originImgList, String watermarkImgFilePath, float ratio) throws Exception {String parentFormatDateTimeStamp = DateUtil.format(new Date(), TIMESTAMP_FORMAT);String imgFilePath = WATER_MARK_FILE_PATH + File.separator + parentFormatDateTimeStamp;log.info("imgFilePath:{}", imgFilePath);List<File> fileUrlList = new ArrayList<>();for (int i = 0; i < originImgList.size(); i++) {String originImg = originImgList.get(i);String leafFormatDateTimeStamp = DateUtil.format(new Date(), TIMESTAMP_FORMAT);String fileName = imgFilePath + File.separator + leafFormatDateTimeStamp + "_water_" + i + ".jpg";File file = FileUtil.touch(fileName);imgUrlAddWaterMark(originImg, watermarkImgFilePath, file, ratio);fileUrlList.add(file);}return fileUrlList;}/*** 添加水印图片** @param originImgList 原始图片url* @param ratio         水印图片占背景图片的比例*/public static List<File> imgFileAddWaterMarkForFileResult(List<String> originImgList, String watermarkImgFilePath, float ratio) throws Exception {String parentFormatDateTimeStamp = DateUtil.format(new Date(), TIMESTAMP_FORMAT);String imgFilePath = WATER_MARK_FILE_PATH + File.separator + parentFormatDateTimeStamp;log.info("imgFilePath:{}", imgFilePath);List<File> fileUrlList = new ArrayList<>();for (int i = 0; i < originImgList.size(); i++) {String originImg = originImgList.get(i);String leafFormatDateTimeStamp = DateUtil.format(new Date(), TIMESTAMP_FORMAT);String fileName = imgFilePath + File.separator + leafFormatDateTimeStamp + "_water_" + i + ".jpg";File file = FileUtil.touch(fileName);imgFileAddWaterMark(originImg, watermarkImgFilePath, file, ratio);fileUrlList.add(file);}return fileUrlList;}/*** 添加水印图片* 根据图片url** @param originalImagePath  背景图片url* @param watermarkImagePath 水印图片url* @param outPutFile         输出文件本地地址* @param ratio              水印图片占背景图片的比例*/private static void imgUrlAddWaterMark(String originalImagePath, String watermarkImagePath, File outPutFile, float ratio) throws IOException {// 读取原始图片URL url = new URL(originalImagePath);BufferedImage originalImage = ImageIO.read(url);// 读取水印图片URL watermarkImageUrl = new URL(watermarkImagePath);BufferedImage watermark = ImgUtil.read(watermarkImageUrl);imgAddWaterMark(originalImage, watermark, outPutFile, ratio);}/*** 添加水印图片* 根据图片file** @param originalImagePath  背景图片url* @param watermarkImagePath 水印图片url* @param outPutFile         输出文件本地地址* @param ratio              水印图片占背景图片的比例*/private static void imgFileAddWaterMark(String originalImagePath, String watermarkImagePath, File outPutFile, float ratio) throws IOException {// 读取原始图片BufferedImage originalImage = ImageIO.read(FileUtil.file(originalImagePath));// 读取水印图片BufferedImage watermark = ImgUtil.read(FileUtil.file(watermarkImagePath));imgAddWaterMark(originalImage, watermark, outPutFile, ratio);}/*** @param originalImage 原始图片url* @param watermark     水印图片url* @param outPutFile    输出图片路径*/private static void imgAddWaterMark(BufferedImage originalImage, BufferedImage watermark, File outPutFile, float ratio) throws IOException {// 读取原始图片int originalWidth = originalImage.getWidth();int originalHeight = originalImage.getHeight();// 读取水印图片// 计算水印新尺寸(保持原始宽高比), 水印宽度占背景图片宽度的30%int newWatermarkWidth = (int) (originalWidth * ratio);int newWatermarkHeight = (int) (watermark.getHeight() * ((float) newWatermarkWidth / watermark.getWidth()));// 缩放水印图片Image scaledWatermark = watermark.getScaledInstance(newWatermarkWidth, newWatermarkHeight, Image.SCALE_SMOOTH);// 计算水印位置(中下,留出边距)200x右移,200y下移int margin = 20;int xOffset = 0;int yOffset = (originalHeight / 2) - newWatermarkHeight / 2 - margin;ImgUtil.pressImage(originalImage,FileUtil.file(outPutFile),scaledWatermark, //水印图片xOffset, //x坐标修正值。 默认在中间,偏移量相对于中间偏移yOffset, //y坐标修正值。 默认在中间,偏移量相对于中间偏移1.0f);}//    public static void main(String[] args) throws Exception {
//        ArrayList<String> objects = new ArrayList<>();
//        for (int i = 1; i <= 6; i++) {
//            objects.add("C:\\Users\\86138\\Desktop\\xxx\\水印\\" + i + ".jpg");
//        }
//        addWaterMarkForFileResult(objects, "C:\\Users\\86138\\Desktop\\xxx\\水印\\water_mark_1.png", 0.3f);
//    }public static void main(String[] args) throws Exception {ArrayList<String> objects = new ArrayList<>();objects.add("https://i-blog.csdnimg.cn/direct/9a914d3108114ec78d4aad04fad21fea.png");   imgUrlAddWaterMarkForFileResult(objects, WATER_MARK_IMG_URL, 0.3f);}
}

三、实现效果图展示

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/80389.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

代理ip和实际ip的区别和联系

在互联网时代&#xff0c;IP地址是每个设备连接网络的“身份证”&#xff0c;但你是否知道IP地址还分为‌代理IP‌和‌实际IP‌&#xff1f;它们各自扮演什么角色&#xff1f;为什么有人选择使用代理IP&#xff1f;实际IP又有哪些不可替代的作用&#xff1f;本文将深入解析代理…

mybatis-plus里的com.baomidou.mybatisplus.core.override.MybatisMapperProxy 类的详细解析

以下是 com.baomidou.mybatisplus.core.override.MybatisMapperProxy 类的详细解析&#xff1a; 1. 类的作用 MybatisMapperProxy 是 MyBatis-Plus 框架中用于实现 Mapper 接口动态代理的核心类。它继承自 MyBatis 的 MapperProxy&#xff0c;并扩展了以下功能&#xff1a; …

Memcached 主主复制架构搭建与 Keepalived 高可用实现

实验目的 掌握基于 repcached 的 Memcached 主主复制配置 实现通过 Keepalived 的 VIP 高可用机制 验证数据双向同步及故障自动切换能力 实验环境 角色IP 地址主机名虚拟 IP (VIP)主节点10.1.1.78server-a10.1.1.80备节点10.1.1.79server-b10.1.1.80 操作系统: CentOS 7 软…

如何成功防护T级超大流量的DDoS攻击

防护T级超大流量的DDoS攻击需要综合技术、架构与运营策略的多层次防御体系。以下是基于最新技术实践和行业案例总结的关键防护策略&#xff1a; 一、流量清洗与分布式处理 部署流量清洗中心 T级攻击的核心防御依赖于专业的流量清洗技术。通过部署分布式流量清洗集群&#xff0c…

基于SpringAI Alibaba实现RAG架构的深度解析与实践指南

一、RAG技术概述 1.1 什么是RAG技术 RAG&#xff08;Retrieval-Augmented Generation&#xff09;检索增强生成是一种将信息检索技术与生成式AI相结合的创新架构。它通过以下方式实现智能化内容生成&#xff1a; 知识检索阶段&#xff1a;从结构化/非结构化数据源中检索相关…

数字化技术的五个环节:大数据、云计算、人工智能、区块链、移动互联网

在21世纪的科技浪潮中&#xff0c;数字化技术以其强大的生命力和无限的潜力&#xff0c;正逐步重塑着我们的世界。大数据、云计算、人工智能、区块链、移动互联网&#xff0c;这五大数字化技术的环节&#xff0c;如同构建智慧未来的基石&#xff0c;每一方面都承载着推动社会进…

Java Web容器分类及对比

Java Web容器分类及对比 1. 按功能分类 (1) Servlet/JSP容器&#xff08;轻量级&#xff0c;仅支持Web层&#xff09; Tomcat 特点&#xff1a;轻量级、开源、仅支持Servlet/JSP规范&#xff0c;适合Web应用。 使用方式&#xff1a; // 通过Maven依赖启动Spring Boot应用&…

【Java并发】【LinkedBlockingQueue】适合初学体质的LinkedBlockingQueue入门

&#x1f44b;hi&#xff0c;我不是一名外包公司的员工&#xff0c;也不会偷吃茶水间的零食&#xff0c;我的梦想是能写高端CRUD &#x1f525; 2025本人正在沉淀中… 博客更新速度 &#x1f44d; 欢迎点赞、收藏、关注&#xff0c;跟上我的更新节奏 &#x1f4da;欢迎订阅专栏…

Python在AI虚拟教学视频开发中的核心技术与前景展望

Python在AI虚拟教学视频开发中的核心技术与前景展望 一、引言&#xff1a;AI虚拟教学的技术革新 随着教育数字化转型加速&#xff0c;AI虚拟教学视频凭借个性化、沉浸式体验成为教育科技的新风口。Python以其强大的多模态处理能力、丰富的开源生态和跨领域兼容性&#xff0c;成…

shadcn/radix-ui的tooltip高度定制arrow位置

尝试了半天&#xff0c;后来发现&#xff0c;不支持。。。。。就是不支持 那箭头只能居中 改side和align都没用&#xff0c;下面有在线实例 https://codesandbox.io/p/sandbox/radix-ui-slider-forked-zgn7hj?file%2Fsrc%2FApp.tsx%3A69%2C21 但是呢&#xff0c; 第一如果…

自动清空 maven 项目临时文件,vue 的 node_modules 文件

echo off setlocal enabledelayedexpansion :: vue 的 node_modules 太大 :: maven 打包后的 target 文件也很大&#xff0c; :: 有些项目日志文件也很大&#xff0c;导致磁盘空间不足了&#xff0c; :: 所以写了个脚本&#xff0c;只要配置一下各项目目录&#xff0c; :: 双击…

[Mybatis-plus]

简介 MyBatis-Plus &#xff08;简称 MP&#xff09;是一个 MyBatis的增强工具&#xff0c;在 MyBatis 的基础上只做增强不做改变。Mybatis-plus官网地址 注意&#xff0c;在引入了mybatis-plus之后&#xff0c;不要再额外引入mybatis和mybatis-spring&#xff0c;避免因为版本…

管理100个小程序-很难吗

20公里的徒步-真难 群里的伙伴发起了一场天目山20公里徒步的活动&#xff0c;想着14公里都轻松拿捏了&#xff0c;思考了30秒后&#xff0c;就借着春风带着老婆孩子就出发了。一开始溪流清澈见底&#xff0c;小桥流水没有人家&#xff1b;青山郁郁葱葱&#xff0c;枯藤老树没有…

大模型工业化元年:GPT-5开启通用AI新纪元,中国技术如何破局?

过去一周&#xff0c;AI领域的焦点无疑是OpenAI发布的GPT-5预览版&#xff0c;以及全球大模型技术从实验室迈向工业化的关键转折。这场变革不仅标志着通用人工智能&#xff08;AGI&#xff09;的进一步逼近&#xff0c;更掀起了全球产业链的竞争与反思。本文将从技术突破、产业…

软考【网络工程师】2023年5月上午题答案解析

1、固态硬盘的存储介质是()。 A 光盘 B 闪存 C 软盘 D 磁盘 答案是 B。 固态硬盘(Solid State Drive),简称 SSD,是用固态电子存储芯片阵列制成的硬盘,其存储介质是闪存(Flash Memory)。闪存具有非易失性,即在断电后仍能保留存储的数据,且读写速度快、抗震性强、能…

【速写】钩子与计算图

文章目录 前向钩子反向钩子的输入反向钩子的输出 前向钩子 下面是一个测试用的计算图的网络&#xff0c;这里因为模型是自定义的缘故&#xff0c;可以直接把前向钩子注册在模型类里面&#xff0c;这样会更加方便一些。其实像以前BERT之类的last_hidden_state以及pool_output之…

高级电影感户外街拍人像摄影后期Lr调色教程,手机滤镜PS+Lightroom预设下载!

调色介绍 高级电影感户外街拍人像摄影后期 Lr 调色&#xff0c;是运用 Adobe Lightroom 软件&#xff0c;对户外街拍的人像照片进行后期处理&#xff0c;以塑造出具有电影质感的独特视觉效果。此调色过程借助 Lr 丰富的工具与功能&#xff0c;从色彩、光影、对比度等多维度着手…

16.QT-Qt窗口-菜单栏|创建菜单栏|添加菜单|创建菜单项|添加分割线|添加快捷键|子菜单|图标|内存泄漏(C++)

Qt窗⼝是通过QMainWindow类来实现的。 QMainWindow是⼀个为⽤⼾提供主窗⼝程序的类&#xff0c;继承⾃QWidget类&#xff0c;并且提供了⼀个预定义的布局。QMainWindow包含⼀个菜单栏&#xff08;menu bar&#xff09;、多个⼯具栏(tool bars)、多个浮动窗⼝&#xff08;铆接部…

【kafka初学】启动执行命令

接上篇&#xff0c;启动&#xff1a;开两个cdm窗口 注意放的文件不要太深或者中文&#xff0c;会报命令行太长的错误 启动zookeeper bin\windows\zookeeper-server-start.bat config\zookeeper.properties2. 启动kafka-serve bin\windows\kafka-server-start.bat config\serv…

利用 Claw Cloud Run 免费应用部署前端网页

一、注册 使用注册180天的github账户注册Claw Cloud账户&#xff0c;可获得每月5$的免费配额官网链接 - https://run.claw.cloud/ &#xff08;ps&#xff1a;直接github账号登录应该就不用写了吧&#xff09; 二、创建应用 打开App Launchpad 点击Create AppCPU选0.1即可&a…