编辑文章 公众号爬虫 null <meta name="referrer" content="no-referrer" /> <p> </p> <div id="js_article" class="rich_media"> <div id="js_top_ad_area" class="top_banner"> </div> <div class="rich_media_inner"> <div id="page-content" class="rich_media_area_primary"> <div class="rich_media_area_primary_inner"> <div id="js_content" class="rich_media_content js_underline_content autoTypeSetting24psection " style="visibility: show;"> <section style="padding-right: 10px; padding-left: 10px; outline: 0px; white-space: normal; background-color: #ffffff; font-size: 16px; word-break: break-word; text-align: left; line-height: 1.25; color: #2b2b2b; letter-spacing: 2px; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.04) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.04) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; visibility: visible;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" data-mpa-powered-by="yiban.io"> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px; visibility: visible;" data-tool="mdnice编辑器">本文介绍java实现在图片上加文字水印的方法,水印可以是图片或者文字,操作方便。</p> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px; visibility: visible;" data-tool="mdnice编辑器">java实现给图片添加水印实现步骤:</p> <ul class="list-paddingleft-1" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; outline: 0px; font-size: 15px; color: #595959; list-style-type: circle; visibility: visible;" data-tool="mdnice编辑器"> <li style="outline: 0px; visibility: visible;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px; visibility: visible;">获取原图片对象信息(本地图片或网络图片)</section> </li> <li style="outline: 0px; visibility: visible;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px; visibility: visible;">添加水印(设置水印颜色、字体、坐标等)</section> </li> <li style="outline: 0px; visibility: visible;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px; visibility: visible;">处理输出目标图片</section> </li> </ul> <h2 style="margin-top: 30px; margin-bottom: 15px; outline: 0px; font-weight: bold; font-size: 22px; color: black; border-bottom: 4px solid #40b8fa; visibility: visible;" data-tool="mdnice编辑器"><span style="margin-left: 25px; outline: 0px; display: flex; color: #40b8fa; font-size: 20px; visibility: visible;">一、java实现给图片添加文字水印</span></h2> <h3 style="margin-top: 20px; margin-bottom: 20px; outline: 0px; font-weight: bold; font-size: 17px; color: black; text-align: center; visibility: visible;" data-tool="mdnice编辑器"><span style="padding-bottom: 2px; outline: 0px; border-bottom: 2px solid rgba(79, 177, 249, 0.65); color: #2b2b2b; visibility: visible;">1.获取原图片对象信息</span></h3> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px; visibility: visible;" data-tool="mdnice编辑器">第一步:获取需要处理的图片</p> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px; visibility: visible;" data-tool="mdnice编辑器">获取图片的方式,通常由两种:</p> <ul class="list-paddingleft-1" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; outline: 0px; font-size: 15px; color: #595959; list-style-type: circle; visibility: visible;" data-tool="mdnice编辑器"> <li style="outline: 0px; visibility: visible;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px; visibility: visible;">一种是通过下载到本地,从本地读取(本地图片);</section> </li> <li style="outline: 0px; visibility: visible;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px; visibility: visible;">另外一种是通过网络地址进行读取(网络图片)</section> </li> </ul> <h4 style="margin-top: 30px; margin-bottom: 15px; outline: 0px; font-weight: bold; font-size: 18px; color: black; visibility: visible;" data-tool="mdnice编辑器"><span style="outline: 0px; height: 16px; line-height: 16px; font-size: 16px; visibility: visible;">1.1 读取本地图片</span></h4> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center; visibility: visible;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" referrer="no-referrer" src="https://i-blog.csdnimg.cn/blog_migrate/400aa006c61a43a387f7ea42c7e05a0b.png" width="641" height="404" data-ratio="0.6302652106084243" data-type="png" data-w="641" /></figure> <p> </p> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">通过代码实现读取本地目录(F:\image\1.png)下图片,代码如下:</p> <pre style="margin-top: 10px; margin-bottom: 10px; outline: 0px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="padding: 15px 16px 16px; outline: 0px; overflow-x: auto; color: #dddddd; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px; letter-spacing: 0px; background: #272822; border-radius: 5px;"><span style="outline: 0px; color: #75715e; line-height: 26px;">// 读取原图片信息 得到文件</span><br style="outline: 0px;" />File srcImgFile = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> File(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"F:/image/1.png"</span>);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//将文件对象转化为图片对象</span><br style="outline: 0px;" />Image srcImg = ImageIO.read(srcImgFile);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//获取图片的宽</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> srcImgWidth = srcImg.getWidth(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//获取图片的高</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> srcImgHeight = srcImg.getHeight(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" />System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片的宽:"</span>+srcImgWidth);<br style="outline: 0px;" />System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片的高:"</span>+srcImgHeight);<br style="outline: 0px;" /></code></pre> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">代码效果如下:</p> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" referrer="no-referrer" src="https://i-blog.csdnimg.cn/blog_migrate/65170fd5c742a64854abfbacc9f050cf.png" width="817" height="680" data-ratio="0.8314814814814815" data-type="png" data-w="1080" /></figure> <h4 style="margin-top: 30px; margin-bottom: 15px; outline: 0px; font-weight: bold; font-size: 18px; color: black;" data-tool="mdnice编辑器"><span style="outline: 0px; height: 16px; line-height: 16px; font-size: 16px;">1.2 读取网络图片</span></h4> <pre style="margin-top: 10px; margin-bottom: 10px; outline: 0px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="padding: 15px 16px 16px; outline: 0px; overflow-x: auto; color: #dddddd; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px; letter-spacing: 0px; background: #272822; border-radius: 5px;"><span style="outline: 0px; color: #75715e; line-height: 26px;">//创建一个URL对象,获取网络图片的地址信息</span><br style="outline: 0px;" />URL url = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> URL(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"https://pngimg.com/distr/img/ukraine.png"</span>);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//将URL对象输入流转化为图片对象 (url.openStream()方法,获得一个输入流)</span><br style="outline: 0px;" />Image srcImg = ImageIO.read(url.openStream());<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//获取图片的宽</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> srcImgWidth = srcImg.getWidth(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//获取图片的高</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> srcImgHeight = srcImg.getHeight(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" />System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片的宽:"</span>+srcImgWidth);<br style="outline: 0px;" />System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片的高:"</span>+srcImgHeight);<br style="outline: 0px;" /></code></pre> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">代码效果如下:</p> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" referrer="no-referrer" src="https://i-blog.csdnimg.cn/blog_migrate/bff4c8c956caf930a5656b127a2f1c79.png" width="817" height="497" data-ratio="0.6083333333333333" data-type="png" data-w="1080" /></figure> <h3 style="margin-top: 20px; margin-bottom: 20px; outline: 0px; font-weight: bold; font-size: 17px; color: black; text-align: center;" data-tool="mdnice编辑器"><span style="padding-bottom: 2px; outline: 0px; border-bottom: 2px solid rgba(79, 177, 249, 0.65); color: #2b2b2b;">2.添加水印</span></h3> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">通过上面的步骤,我们已经获取到了原始图片信息,下面需要创建一个画笔进行水印的添加。水印包含文字水印、图片水印。</p> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">画笔可以设置水印颜色、字体大小、字体样式等。</p> <pre style="margin-top: 10px; margin-bottom: 10px; outline: 0px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="padding: 15px 16px 16px; outline: 0px; overflow-x: auto; color: #dddddd; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px; letter-spacing: 0px; background: #272822; border-radius: 5px;">BufferedImage bufImg = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// 加水印</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//创建画笔</span><br style="outline: 0px;" />Graphics2D g = bufImg.createGraphics();<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//srcImg 为上面获取到的原始图片的图片对象</span><br style="outline: 0px;" />g.drawImage(srcImg, <span style="outline: 0px; line-height: 26px;">0</span>, <span style="outline: 0px; line-height: 26px;">0</span>, srcImgWidth, srcImgHeight, <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//根据图片的背景设置水印颜色</span><br style="outline: 0px;" />g.setColor(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> Color(<span style="outline: 0px; line-height: 26px;">255</span>,<span style="outline: 0px; line-height: 26px;">255</span>,<span style="outline: 0px; line-height: 26px;">255</span>,<span style="outline: 0px; line-height: 26px;">128</span>));<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//设置字体 画笔字体样式为微软雅黑,加粗,文字大小为60pt</span><br style="outline: 0px;" />g.setFont(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> Font(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"微软雅黑"</span>, Font.BOLD, <span style="outline: 0px; line-height: 26px;">60</span>));<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//设置水印的坐标</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//int x=200;</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//int y=200;</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> x=(srcImgWidth - getWatermarkLength(waterMarkContent, g)) / <span style="outline: 0px; line-height: 26px;">2</span>;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> y=srcImgHeight / <span style="outline: 0px; line-height: 26px;">2</span>;<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//画出水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标</span><br style="outline: 0px;" />g.drawString(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片来源:https://image.baidu.com/"</span>, x, y);<br style="outline: 0px;" />g.dispose();<br style="outline: 0px;" /></code></pre> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">getWatermarkLength方法用于计算水印内容的长度:</p> <pre style="margin-top: 10px; margin-bottom: 10px; outline: 0px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="padding: 15px 16px 16px; outline: 0px; overflow-x: auto; color: #dddddd; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px; letter-spacing: 0px; background: #272822; border-radius: 5px;"><span style="outline: 0px; color: #75715e; line-height: 26px;">/**<br style="outline: 0px;" /> * 获取水印文字的长度<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@param</span> waterMarkContent<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@param</span> g<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@return</span><br style="outline: 0px;" /> */</span><br style="outline: 0px;" /><span style="outline: 0px; line-height: 26px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">public</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">static</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> <span style="outline: 0px; color: #a6e22e; font-weight: bold; line-height: 26px;">getWatermarkLength</span><span style="outline: 0px; line-height: 26px;">(String waterMarkContent, Graphics2D g)</span> </span>{<br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">return</span> g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), <span style="outline: 0px; line-height: 26px;">0</span>, waterMarkContent.length());<br style="outline: 0px;" />}<br style="outline: 0px;" /></code></pre> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器"><span style="outline: 0px; color: #3594f7; font-weight: bold;">Font 字体说明:</span></p> <ul class="list-paddingleft-1" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; outline: 0px; font-size: 15px; color: #595959; list-style-type: circle;" data-tool="mdnice编辑器"> <li style="outline: 0px;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px;">Font 类的构造函数为: <code style="margin-right: 2px; margin-left: 2px; padding-right: 2px; padding-left: 2px; outline: 0px; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #3594f7; background: rgba(59, 170, 250, 0.1); border-radius: 2px; height: 21px; line-height: 22px;">public Font(String familyName, int style, int size)</code></section> </li> <li style="outline: 0px;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px;">参数说明:第一个参数为字体类型,第二个参数为字体风格,第三个参数为字体大小</section> </li> </ul> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器"><span style="outline: 0px; color: #3594f7; font-weight: bold;">字体的风格有:</span></p> <ul class="list-paddingleft-1" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; outline: 0px; font-size: 15px; color: #595959; list-style-type: circle;" data-tool="mdnice编辑器"> <li style="outline: 0px;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px;">Font.PLAIN(普通)</section> </li> <li style="outline: 0px;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px;">Font.BOLD(加粗)</section> </li> <li style="outline: 0px;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px;">Font.ITALIC(斜体)</section> </li> <li style="outline: 0px;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px;">Font.BOLD+Font.ITALIC(粗斜体)</section> </li> </ul> <blockquote style="margin-top: 20px; margin-bottom: 20px; padding: 10px 10px 10px 20px; outline: 0px; border-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgba(64, 184, 250, 0.4); color: #595959; font-size: 0.9em; box-sizing: inherit; overflow: auto; text-size-adjust: inherit; line-height: 1.55em; border-radius: 6px; background: rgba(64, 184, 250, 0.1);" data-tool="mdnice编辑器"><span style="outline: 0px; color: rgba(64, 184, 250, 0.5); font-size: 34px; line-height: 1; font-weight: bold;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; outline: 0px; font-size: 14px; word-spacing: 2px; line-height: 26px;">size字体大小 默认单位是pt(磅),数字越大,字就越大</p> <span style="outline: 0px; float: right; color: rgba(64, 184, 250, 0.5);">❞</span></blockquote> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">需要注意是 水印坐标位置。设置不当,就看不到水印效果。</p> <h5 style="margin-top: 30px; margin-bottom: 15px; outline: 0px; font-weight: bold; color: black;" data-tool="mdnice编辑器">如何确定水印位置 ?</h5> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器"><span style="outline: 0px; color: #3594f7; font-weight: bold;">方法一:设置固定值</span></p> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">①首先,我们要知道图片上的坐标的表示法。具体如下:</p> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" referrer="no-referrer" src="https://i-blog.csdnimg.cn/blog_migrate/4c2a85da52b1d00788de5229c91aee83.png" width="424" height="292" data-ratio="0.6886792452830188" data-type="png" data-w="424" /></figure> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">②将图片保存到本地,然后选中图片点击右键,编辑,选择“画图”软件打开</p> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" referrer="no-referrer" src="https://i-blog.csdnimg.cn/blog_migrate/838948f80e9e57f33d2652a25cba14f5.png" width="817" height="382" data-ratio="0.467280163599182" data-type="png" data-w="978" /></figure> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">③将鼠标移动到想要添加水印的位置,左下角可以看到鼠标点击位置对应的坐标值,拿到这个坐标值写入程序即可</p> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" referrer="no-referrer" src="https://i-blog.csdnimg.cn/blog_migrate/bd969f4c170935564c7bdb2c68e10c0b.png" width="817" height="870" data-ratio="1.0638888888888889" data-type="png" data-w="1080" /></figure> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器"><span style="outline: 0px; color: #3594f7; font-weight: bold;">方法二:根据原图大小进行设置,如放置在原图的中间位置</span></p> <ul class="list-paddingleft-1" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; outline: 0px; font-size: 15px; color: #595959; list-style-type: circle;" data-tool="mdnice编辑器"> <li style="outline: 0px;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px;">x轴坐标:(原始图的宽度 - 水印的宽度) / 2</section> </li> <li style="outline: 0px;"> <section style="margin-top: 5px; margin-bottom: 5px; outline: 0px; line-height: 26px; font-size: 14px;">y轴坐标:(原始图的高度 - 水印的高度) / 2</section> </li> </ul> <h3 style="margin-top: 20px; margin-bottom: 20px; outline: 0px; font-weight: bold; font-size: 17px; color: black; text-align: center;" data-tool="mdnice编辑器"><span style="padding-bottom: 2px; outline: 0px; border-bottom: 2px solid rgba(79, 177, 249, 0.65); color: #2b2b2b;">3.获取目标图片</span></h3> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">经过上面的操作后,我们的图片添加文字就已经处理完成了。但他现在还保存在Java对象中。</p> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">我们想要看得到效果,需要进行处理,保存图片到本地。</p> <pre style="margin-top: 10px; margin-bottom: 10px; outline: 0px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="padding: 15px 16px 16px; outline: 0px; overflow-x: auto; color: #dddddd; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px; letter-spacing: 0px; background: #272822; border-radius: 5px;"><span style="outline: 0px; color: #75715e; line-height: 26px;">//待存储的地址</span><br style="outline: 0px;" />String tarImgPath=<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"F:/image/t.png"</span>;<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// 输出图片</span><br style="outline: 0px;" />FileOutputStream outImgStream = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> FileOutputStream(tarImgPath);<br style="outline: 0px;" />ImageIO.write(bufImg, <span style="outline: 0px; color: #a6e22e; line-height: 26px;">"png"</span>, outImgStream);<br style="outline: 0px;" />System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"添加水印完成"</span>);<br style="outline: 0px;" />outImgStream.flush();<br style="outline: 0px;" />outImgStream.close();<br style="outline: 0px;" /></code></pre> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">执行效果:</p> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">执行,目标目录下多了一个t.png的图片:<img style="display: block; margin-left: auto; margin-right: auto;" referrer="no-referrer" src="https://i-blog.csdnimg.cn/blog_migrate/10d0b98d5d781823a7f6dcc8f66187b0.png" alt="" width="584" height="368" /></p> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"></figure> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">t.png打开可以看到添加的文字水印,水印添加成功:</p> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" referrer="no-referrer" src="https://i-blog.csdnimg.cn/blog_migrate/99eef16f156fb7989109b6c0253e5601.png" width="817" height="673" data-ratio="0.8240740740740741" data-type="png" data-w="1080" /></figure> <h3 style="margin-top: 20px; margin-bottom: 20px; outline: 0px; font-weight: bold; font-size: 17px; color: black; text-align: center;" data-tool="mdnice编辑器"><span style="padding-bottom: 2px; outline: 0px; border-bottom: 2px solid rgba(79, 177, 249, 0.65); color: #2b2b2b;">4.完成代码</span></h3> <pre style="margin-top: 10px; margin-bottom: 10px; outline: 0px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="padding: 15px 16px 16px; outline: 0px; overflow-x: auto; color: #dddddd; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px; letter-spacing: 0px; background: #272822; border-radius: 5px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">package</span> com.example.listdemo.utils;<br style="outline: 0px;" /><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> javax.imageio.ImageIO;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.awt.*;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.awt.image.BufferedImage;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.io.File;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.io.FileOutputStream;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.io.IOException;<br style="outline: 0px;" /><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">/**<br style="outline: 0px;" /> * 图片添加水印<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@author</span> qzz<br style="outline: 0px;" /> */</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">public</span> <span style="outline: 0px; line-height: 26px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">class</span> <span style="outline: 0px; font-weight: bold; color: white; line-height: 26px;">ImageUtils</span> </span>{<br style="outline: 0px;" /><br style="outline: 0px;" /> <span style="outline: 0px; line-height: 26px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">public</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">static</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">void</span> <span style="outline: 0px; color: #a6e22e; font-weight: bold; line-height: 26px;">main</span><span style="outline: 0px; line-height: 26px;">(String[] args)</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">throws</span> IOException </span>{<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">// 读取原图片信息 得到文件(本地图片)</span><br style="outline: 0px;" /> File srcImgFile = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> File(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"F:/image/1.png"</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//将文件对象转化为图片对象</span><br style="outline: 0px;" /> Image srcImg = ImageIO.read(srcImgFile);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//获取图片的宽</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> srcImgWidth = srcImg.getWidth(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//获取图片的高</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> srcImgHeight = srcImg.getHeight(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片的宽:"</span>+srcImgWidth);<br style="outline: 0px;" /> System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片的高:"</span>+srcImgHeight);<br style="outline: 0px;" /><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//创建一个URL对象,获取网络图片的地址信息(网络图片)</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// URL url = new URL("https://pngimg.com/distr/img/ukraine.png");</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //将URL对象输入流转化为图片对象 (url.openStream()方法,获得一个输入流)</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// Image srcImg = ImageIO.read(url.openStream());</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //获取图片的宽</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// int srcImgWidth = srcImg.getWidth(null);</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //获取图片的高</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// int srcImgHeight = srcImg.getHeight(null);</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// System.out.println("图片的宽:"+srcImgWidth);</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// System.out.println("图片的高:"+srcImgHeight);</span><br style="outline: 0px;" /><br style="outline: 0px;" /><br style="outline: 0px;" /><br style="outline: 0px;" /> BufferedImage bufImg = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">// 加水印</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//创建画笔</span><br style="outline: 0px;" /> Graphics2D g = bufImg.createGraphics();<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//绘制原始图片</span><br style="outline: 0px;" /> g.drawImage(srcImg, <span style="outline: 0px; line-height: 26px;">0</span>, <span style="outline: 0px; line-height: 26px;">0</span>, srcImgWidth, srcImgHeight, <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//-------------------------文字水印 start----------------------------</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//根据图片的背景设置水印颜色</span><br style="outline: 0px;" /> g.setColor(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> Color(<span style="outline: 0px; line-height: 26px;">255</span>,<span style="outline: 0px; line-height: 26px;">255</span>,<span style="outline: 0px; line-height: 26px;">255</span>,<span style="outline: 0px; line-height: 26px;">128</span>));<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//设置字体 画笔字体样式为微软雅黑,加粗,文字大小为60pt</span><br style="outline: 0px;" /> g.setFont(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> Font(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"微软雅黑"</span>, Font.BOLD, <span style="outline: 0px; line-height: 26px;">60</span>));<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//水印内容</span><br style="outline: 0px;" /> String waterMarkContent=<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片来源:https://image.baidu.com/"</span>;<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//设置水印的坐标(为原图片中间位置)</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> x=(srcImgWidth - getWatermarkLength(waterMarkContent, g)) / <span style="outline: 0px; line-height: 26px;">2</span>;<br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> y=srcImgHeight / <span style="outline: 0px; line-height: 26px;">2</span>;<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//画出水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标</span><br style="outline: 0px;" /> g.drawString(waterMarkContent, x, y);<br style="outline: 0px;" /> g.dispose();<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//-------------------------文字水印 end----------------------------</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//待存储的地址</span><br style="outline: 0px;" /> String tarImgPath=<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"F:/image/t.png"</span>;<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">// 输出图片</span><br style="outline: 0px;" /> FileOutputStream outImgStream = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> FileOutputStream(tarImgPath);<br style="outline: 0px;" /> ImageIO.write(bufImg, <span style="outline: 0px; color: #a6e22e; line-height: 26px;">"png"</span>, outImgStream);<br style="outline: 0px;" /> System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"添加水印完成"</span>);<br style="outline: 0px;" /> outImgStream.flush();<br style="outline: 0px;" /> outImgStream.close();<br style="outline: 0px;" /><br style="outline: 0px;" /> }<br style="outline: 0px;" /> <br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">/**<br style="outline: 0px;" /> * 获取水印文字的长度<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@param</span> waterMarkContent<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@param</span> g<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@return</span><br style="outline: 0px;" /> */</span><br style="outline: 0px;" /> <span style="outline: 0px; line-height: 26px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">public</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">static</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> <span style="outline: 0px; color: #a6e22e; font-weight: bold; line-height: 26px;">getWatermarkLength</span><span style="outline: 0px; line-height: 26px;">(String waterMarkContent, Graphics2D g)</span> </span>{<br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">return</span> g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), <span style="outline: 0px; line-height: 26px;">0</span>, waterMarkContent.length());<br style="outline: 0px;" /> }<br style="outline: 0px;" />}<br style="outline: 0px;" /></code></pre> <h2 style="margin-top: 30px; margin-bottom: 15px; outline: 0px; font-weight: bold; font-size: 22px; color: black; border-bottom: 4px solid #40b8fa;" data-tool="mdnice编辑器"><span style="margin-left: 25px; outline: 0px; display: flex; color: #40b8fa; font-size: 20px;">二、java实现给图片添加图片水印</span></h2> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">下载水印图片到本地:</p> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">水印图片位置:F:\image\s.png</p> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" style="margin: 20px auto; outline: 0px; border-radius: 6px; display: block; object-fit: contain; box-shadow: #999999 2px 4px 7px; width: 585px !important; visibility: visible !important;" src="https://mmbiz.qpic.cn/sz_mmbiz_png/JfTPiahTHJhpqAugFA9ZqGB1Rq2WVne1qncrNju23Nqp5MGRpGMAxjq5Shr7yIjKwX9ssoZJnicHRuZHImficOvbw/640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1" data-ratio="0.5658119658119658" data-type="png" data-w="585" /></figure> <h3 style="margin-top: 20px; margin-bottom: 20px; outline: 0px; font-weight: bold; font-size: 17px; color: black; text-align: center;" data-tool="mdnice编辑器"><span style="padding-bottom: 2px; outline: 0px; border-bottom: 2px solid rgba(79, 177, 249, 0.65); color: #2b2b2b;">1.添加图片水印方法</span></h3> <pre style="margin-top: 10px; margin-bottom: 10px; outline: 0px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="padding: 15px 16px 16px; outline: 0px; overflow-x: auto; color: #dddddd; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px; letter-spacing: 0px; background: #272822; border-radius: 5px;"><span style="outline: 0px; color: #75715e; line-height: 26px;">// 水印文件</span><br style="outline: 0px;" />String waterMarkImage=<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"F:/image/s.png"</span>;<br style="outline: 0px;" />Image srcWaterMark = ImageIO.read(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> File(waterMarkImage));<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//获取水印图片的宽度</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> widthWaterMark= srcWaterMark.getWidth(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//获取水印图片的高度</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> heightWaterMark = srcWaterMark.getHeight(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//设置 alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字</span><br style="outline: 0px;" />g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, <span style="outline: 0px; line-height: 26px;">0.9f</span>));<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">//绘制水印图片 坐标为中间位置</span><br style="outline: 0px;" />g.drawImage(srcWaterMark, (srcImgWidth - widthWaterMark) / <span style="outline: 0px; line-height: 26px;">2</span>,<br style="outline: 0px;" /> (srcImgHeight - heightWaterMark) / <span style="outline: 0px; line-height: 26px;">2</span>, widthWaterMark, heightWaterMark, <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// 水印文件结束</span><br style="outline: 0px;" />g.dispose();<br style="outline: 0px;" /></code></pre> <h3 style="margin-top: 20px; margin-bottom: 20px; outline: 0px; font-weight: bold; font-size: 17px; color: black; text-align: center;" data-tool="mdnice编辑器"><span style="padding-bottom: 2px; outline: 0px; border-bottom: 2px solid rgba(79, 177, 249, 0.65); color: #2b2b2b;">2.完成代码</span></h3> <pre style="margin-top: 10px; margin-bottom: 10px; outline: 0px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="padding: 15px 16px 16px; outline: 0px; overflow-x: auto; color: #dddddd; display: -webkit-box; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; font-size: 12px; letter-spacing: 0px; background: #272822; border-radius: 5px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">package</span> com.example.listdemo.utils;<br style="outline: 0px;" /><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> javax.imageio.ImageIO;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.awt.*;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.awt.image.BufferedImage;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.io.File;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.io.FileOutputStream;<br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">import</span> java.io.IOException;<br style="outline: 0px;" /><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">/**<br style="outline: 0px;" /> * 图片添加水印<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@author</span> qzz<br style="outline: 0px;" /> */</span><br style="outline: 0px;" /><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">public</span> <span style="outline: 0px; line-height: 26px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">class</span> <span style="outline: 0px; font-weight: bold; color: white; line-height: 26px;">ImageUtils</span> </span>{<br style="outline: 0px;" /><br style="outline: 0px;" /> <span style="outline: 0px; line-height: 26px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">public</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">static</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">void</span> <span style="outline: 0px; color: #a6e22e; font-weight: bold; line-height: 26px;">main</span><span style="outline: 0px; line-height: 26px;">(String[] args)</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">throws</span> IOException </span>{<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">// 读取原图片信息 得到文件(本地图片)</span><br style="outline: 0px;" /> File srcImgFile = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> File(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"F:/image/1.png"</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//将文件对象转化为图片对象</span><br style="outline: 0px;" /> Image srcImg = ImageIO.read(srcImgFile);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//获取图片的宽</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> srcImgWidth = srcImg.getWidth(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//获取图片的高</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> srcImgHeight = srcImg.getHeight(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片的宽:"</span>+srcImgWidth);<br style="outline: 0px;" /> System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"图片的高:"</span>+srcImgHeight);<br style="outline: 0px;" /><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//创建一个URL对象,获取网络图片的地址信息(网络图片)</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// URL url = new URL("https://pngimg.com/distr/img/ukraine.png");</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //将URL对象输入流转化为图片对象 (url.openStream()方法,获得一个输入流)</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// Image srcImg = ImageIO.read(url.openStream());</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //获取图片的宽</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// int srcImgWidth = srcImg.getWidth(null);</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //获取图片的高</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// int srcImgHeight = srcImg.getHeight(null);</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// System.out.println("图片的宽:"+srcImgWidth);</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// System.out.println("图片的高:"+srcImgHeight);</span><br style="outline: 0px;" /><br style="outline: 0px;" /><br style="outline: 0px;" /><br style="outline: 0px;" /> BufferedImage bufImg = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">// 加水印</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//创建画笔</span><br style="outline: 0px;" /> Graphics2D g = bufImg.createGraphics();<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//绘制原始图片</span><br style="outline: 0px;" /> g.drawImage(srcImg, <span style="outline: 0px; line-height: 26px;">0</span>, <span style="outline: 0px; line-height: 26px;">0</span>, srcImgWidth, srcImgHeight, <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//-------------------------文字水印 start----------------------------</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //根据图片的背景设置水印颜色</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// g.setColor(new Color(255,255,255,128));</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //设置字体 画笔字体样式为微软雅黑,加粗,文字大小为60pt</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// g.setFont(new Font("微软雅黑", Font.BOLD, 60));</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// String waterMarkContent="图片来源:https://image.baidu.com/";</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //设置水印的坐标(为原图片中间位置)</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// int x=(srcImgWidth - getWatermarkLength(waterMarkContent, g)) / 2;</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// int y=srcImgHeight / 2;</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// //画出水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// g.drawString(waterMarkContent, x, y);</span><br style="outline: 0px;" /><span style="outline: 0px; color: #75715e; line-height: 26px;">// g.dispose();</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//-------------------------文字水印 end----------------------------</span><br style="outline: 0px;" /><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//-------------------------图片水印 start----------------------------</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">// 水印文件</span><br style="outline: 0px;" /> String waterMarkImage=<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"F:/image/s.png"</span>;<br style="outline: 0px;" /> Image srcWaterMark = ImageIO.read(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> File(waterMarkImage));<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//获取水印图片的宽度</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> widthWaterMark= srcWaterMark.getWidth(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//获取水印图片的高度</span><br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> heightWaterMark = srcWaterMark.getHeight(<span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//设置 alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字</span><br style="outline: 0px;" /> g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, <span style="outline: 0px; line-height: 26px;">0.9f</span>));<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//绘制水印图片 坐标为中间位置</span><br style="outline: 0px;" /> g.drawImage(srcWaterMark, (srcImgWidth - widthWaterMark) / <span style="outline: 0px; line-height: 26px;">2</span>,<br style="outline: 0px;" /> (srcImgHeight - heightWaterMark) / <span style="outline: 0px; line-height: 26px;">2</span>, widthWaterMark, heightWaterMark, <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">null</span>);<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">// 水印文件结束</span><br style="outline: 0px;" /> g.dispose();<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//-------------------------图片水印 end----------------------------</span><br style="outline: 0px;" /><br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">//待存储的地址</span><br style="outline: 0px;" /> String tarImgPath=<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"F:/image/t.png"</span>;<br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">// 输出图片</span><br style="outline: 0px;" /> FileOutputStream outImgStream = <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">new</span> FileOutputStream(tarImgPath);<br style="outline: 0px;" /> ImageIO.write(bufImg, <span style="outline: 0px; color: #a6e22e; line-height: 26px;">"png"</span>, outImgStream);<br style="outline: 0px;" /> System.out.println(<span style="outline: 0px; color: #a6e22e; line-height: 26px;">"添加水印完成"</span>);<br style="outline: 0px;" /> outImgStream.flush();<br style="outline: 0px;" /> outImgStream.close();<br style="outline: 0px;" /><br style="outline: 0px;" /> }<br style="outline: 0px;" /> <br style="outline: 0px;" /> <span style="outline: 0px; color: #75715e; line-height: 26px;">/**<br style="outline: 0px;" /> * 获取水印文字的长度<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@param</span> waterMarkContent<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@param</span> g<br style="outline: 0px;" /> * <span style="outline: 0px; font-weight: bold; line-height: 26px;">@return</span><br style="outline: 0px;" /> */</span><br style="outline: 0px;" /> <span style="outline: 0px; line-height: 26px;"><span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">public</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">static</span> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">int</span> <span style="outline: 0px; color: #a6e22e; font-weight: bold; line-height: 26px;">getWatermarkLength</span><span style="outline: 0px; line-height: 26px;">(String waterMarkContent, Graphics2D g)</span> </span>{<br style="outline: 0px;" /> <span style="outline: 0px; color: #f92672; font-weight: bold; line-height: 26px;">return</span> g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), <span style="outline: 0px; line-height: 26px;">0</span>, waterMarkContent.length());<br style="outline: 0px;" /> }<br style="outline: 0px;" />}<br style="outline: 0px;" /></code></pre> <h3 style="margin-top: 20px; margin-bottom: 20px; outline: 0px; font-weight: bold; font-size: 17px; color: black; text-align: center;" data-tool="mdnice编辑器"><span style="padding-bottom: 2px; outline: 0px; border-bottom: 2px solid rgba(79, 177, 249, 0.65); color: #2b2b2b;">3.代码执行效果</span></h3> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img class="rich_pages wxw-img" style="margin: 20px auto; outline: 0px; border-radius: 6px; display: block; object-fit: contain; box-shadow: #999999 2px 4px 7px; width: 677px !important; visibility: visible !important;" src="https://mmbiz.qpic.cn/sz_mmbiz_png/JfTPiahTHJhpqAugFA9ZqGB1Rq2WVne1q7K7iaxYicMibibxP6jPVWaQ8rhJ2bVPqhmS0KvwsPk8gYsEiaV9ia06hh8gg/640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1" data-ratio="0.33240740740740743" data-type="png" data-w="1080" /></figure> <p style="margin-top: 10px; margin-bottom: 10px; padding-top: 8px; padding-bottom: 8px; outline: 0px; line-height: 26px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">t.png图片文件中 含有 水印图片,说明 图片水印添加成功!</p> </section> <section style="margin-bottom: 0px; padding-right: 10px; padding-left: 10px; outline: 0px; white-space: normal; color: black; font-size: 16px; letter-spacing: 0px; text-align: left; background-color: #ffffff; line-height: 1.6; word-break: break-word; font-family: PingFangSC-Light;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com"> <h3 style="outline: 0px; font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: 0.544px; text-align: right;" data-tool="mdnice编辑器"><em style="outline: 0px; color: #888888; font-size: 12px; letter-spacing: 0.5px;">来源:blog.csdn.net/qq_26383975/</em><em style="outline: 0px; color: #888888; font-size: 12px; letter-spacing: 0.5px;">article/details/125996277</em></h3> <p style="outline: 0px;"> </p> </section> <blockquote style="margin-top: 20px; margin-bottom: 20px; padding: 10px 10px 10px 1em; outline: 0px; border-left-width: 2px; border-left-color: #888888; color: #777777; font-size: 0.9em; white-space: normal; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; letter-spacing: normal; text-align: left; widows: 1; caret-color: #3c3c3c; border-top: none; border-bottom: none; overflow: auto; background: rgba(0, 0, 0, 0.05); border-right: 2px solid #888888; visibility: visible;" data-tool="mdnice编辑器"> <p style="padding-top: 8px; padding-bottom: 8px; outline: 0px; font-size: 16px; text-align: justify; color: black; line-height: 26px; visibility: visible;">文明发言,以<code style="margin-right: 2px; margin-left: 2px; padding: 2px 4px; outline: 0px; font-size: 14px; border-radius: 4px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #009688; visibility: visible;">交流技术</code>、<code style="margin-right: 2px; margin-left: 2px; padding: 2px 4px; outline: 0px; font-size: 14px; border-radius: 4px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #009688; visibility: visible;">职位内推</code>、<code style="margin-right: 2px; margin-left: 2px; padding: 2px 4px; outline: 0px; font-size: 14px; border-radius: 4px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #009688; visibility: visible;">行业探讨</code>为主</p> </blockquote> <figure style="margin-top: 10px; margin-bottom: 10px; outline: 0px; white-space: normal; color: #000000; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 16px; letter-spacing: normal; text-align: left; widows: 1; caret-color: #3c3c3c; background-color: #ffffff; display: flex; flex-direction: column; justify-content: center; align-items: center; visibility: visible;" data-tool="mdnice编辑器"></figure> <p style="display: none;"> </p> </div> </div> </div> </div> </div> 标题: 分类: 请选择分类(必选) 博客 酷玩 分享 相册 标签: 按 Tab、回车或逗号把输入的文字变成标签;点 × 或在输入框为空时按退格删除;最多 8 个 封面图: 上传图片 建议尺寸 800x400,图片大小不超过 1MB 展开更多设置(关键词、摘要、转载、评论) 关键词: 摘要: 本文介绍java实现在图片上加文字水印的方法,水印可以是图片或者文字,操作方便。 java实现给图片添加水印实现步骤: 获取原图片对象信息(本地图片或网络图片) 添加水印(设置水印颜色、字体、坐标等... 选项: 是否转载 启用评论 勾选「是否转载」可标注文章来源,原文链接可留空。 正文: B I U H2 H3 引用 • 列表 1. 列表 链接 图片 清格式 源码 该文章是富文本(HTML)写的,已自动切到富文本编辑器,不会按 markdown 解析;要精确改 HTML 点「源码」。 保存修改