ffmepg入门学习十 java给图片添加水印


    
    
  1. package com.qihui.qxj.utils;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.Image;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import javax.imageio.ImageIO;
  11. public class Main {
  12. public static void main(String[] args) throws IOException {
  13. String sourceImg= "D:\\test.jpg"; //源图片地址
  14. String targetImg= "D:\\123.jpg"; //新存储的地址
  15. addWatermark(sourceImg, targetImg);
  16. }
  17. /** 设置文字水印
  18. * @param sourceImg 源图片路径
  19. * @param targetImg 保存的图片路径
  20. * @throws IOException
  21. */
  22. public static void addWatermark(String sourceImg, String targetImg) throws IOException {
  23. File srcImgFile = new File(sourceImg);
  24. Image srcImg = ImageIO.read(srcImgFile);
  25. int srcImgWidth = srcImg.getWidth( null);
  26. int srcImgHeight = srcImg.getHeight( null);
  27. int fontSize= 75;
  28. Font font = new Font( "黑体,Arial", 1, fontSize);
  29. BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
  30. Graphics2D g = bufImg.createGraphics();
  31. g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
  32. g.setColor(Color.BLACK);
  33. g.setFont(font);
  34. //设置水印的坐标
  35. g.drawString( "测试", 3196, 150);
  36. g.drawString( "性别:男", 3196, 150+( 120* 1));
  37. g.drawString( "毕业学校:XXX大学", 3196, 150+( 120* 2));
  38. g.setColor(Color.white);
  39. font = new Font( "黑体,Arial", 1, 120);
  40. g.setFont(font);
  41. g.drawString( "广东省深圳(SHENZHEN)", 3000, 2020);
  42. g.dispose();
  43. // 输出图片
  44. FileOutputStream outImgStream = new FileOutputStream(targetImg);
  45. ImageIO.write(bufImg, "jpg", outImgStream);
  46. System.out.println( "添加水印完成");
  47. outImgStream.flush();
  48. outImgStream.close();
  49. }
  50. }

效果图: 

 可能有些朋友会存在,增加的文字水印没有效果的问题,那可能是图片的宽和高度有问题,需要调整代码的x,y坐标。

注意:图片的左上角为坐标的原点(0,0)  例如测试两个文字的坐标为(3196, 150)

我的原图为4090*2160

点击链接加入群聊【java交流群】:https://jq.qq.com/?_wv=1027&k=5VZxg3k

                        <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count"></span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/qq_16855077">
                    <img src="https://profile.csdnimg.cn/C/0/3/3_qq_16855077" class="avatar_pic" username="qq_16855077">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/1x/6.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_16855077" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">程序猿学社</a></span>
                                            </div>
                    <div class="text"><span>发布了277 篇原创文章</span> · <span>获赞 514</span> · <span>访问量 21万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=qq_16855077" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                    </div>
                            </div>
                    </div>
    

转载自:https://blog.csdn.net/qq_16855077/article/details/90378173

发布了42 篇原创文章 · 获赞 115 · 访问量 1万+

  
  
  1. package com.qihui.qxj.utils;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.Image;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import javax.imageio.ImageIO;
  11. public class Main {
  12. public static void main(String[] args) throws IOException {
  13. String sourceImg= "D:\\test.jpg"; //源图片地址
  14. String targetImg= "D:\\123.jpg"; //新存储的地址
  15. addWatermark(sourceImg, targetImg);
  16. }
  17. /** 设置文字水印
  18. * @param sourceImg 源图片路径
  19. * @param targetImg 保存的图片路径
  20. * @throws IOException
  21. */
  22. public static void addWatermark(String sourceImg, String targetImg) throws IOException {
  23. File srcImgFile = new File(sourceImg);
  24. Image srcImg = ImageIO.read(srcImgFile);
  25. int srcImgWidth = srcImg.getWidth( null);
  26. int srcImgHeight = srcImg.getHeight( null);
  27. int fontSize= 75;
  28. Font font = new Font( "黑体,Arial", 1, fontSize);
  29. BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
  30. Graphics2D g = bufImg.createGraphics();
  31. g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
  32. g.setColor(Color.BLACK);
  33. g.setFont(font);
  34. //设置水印的坐标
  35. g.drawString( "测试", 3196, 150);
  36. g.drawString( "性别:男", 3196, 150+( 120* 1));
  37. g.drawString( "毕业学校:XXX大学", 3196, 150+( 120* 2));
  38. g.setColor(Color.white);
  39. font = new Font( "黑体,Arial", 1, 120);
  40. g.setFont(font);
  41. g.drawString( "广东省深圳(SHENZHEN)", 3000, 2020);
  42. g.dispose();
  43. // 输出图片
  44. FileOutputStream outImgStream = new FileOutputStream(targetImg);
  45. ImageIO.write(bufImg, "jpg", outImgStream);
  46. System.out.println( "添加水印完成");
  47. outImgStream.flush();
  48. outImgStream.close();
  49. }
  50. }

猜你喜欢

转载自blog.csdn.net/luoyong_blog/article/details/104497728