ffmepg started to learn java to ten pictures add a watermark


    
    
  1. Package Penalty for 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" ; // source address of the picture
  14. String targetImg= "D: \\ 123.jpg" ; // new storage address
  15. addWatermark(sourceImg, targetImg);
  16. }
  17. / * Set the text watermark
  18. * @Param sourceImg source image path
  19. * @Param targetImg saved image path
  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 new the Font ( "bold, Arial" , . 1 , the 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. // set the watermark coordinate
  35. g.drawString( "Test" , 3196 , 150 );
  36. g.drawString( "Gender: Male" , 3196 , 150 + ( 120 * 1 ));
  37. g.drawString( "Schools: XXX University" , 3196 , 150 + ( 120 * 2 ));
  38. g.setColor(Color.white);
  39. font = new new the Font ( "bold, Arial" , . 1 , 120 );
  40. g.setFont(font);
  41. g.drawString( "Guangdong Province, Shenzhen (SHENZHEN)" , 3000 , 2020 );
  42. g.dispose ();
  43. // output picture
  44. FileOutputStream outImgStream = new FileOutputStream(targetImg);
  45. ImageIO.write(bufImg, "jpg", outImgStream);
  46. System.out.println( "Add a watermark to complete" );
  47. outImgStream.flush();
  48. outImgStream.close();
  49. }
  50. }

Renderings: 

 Some friends may exist, no additional text watermark effect problem, it may be pictures of width and height have a problem, need to adjust the code of x, y coordinates.

Note: the upper left corner of the picture for the origin of the coordinate (0,0) such as testing two characters coordinates (3196, 150)

My artwork is 4090 * 2160

Click on the link to join a group chat java exchange group []: 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>
    

Reprinted from: https: //blog.csdn.net/qq_16855077/article/details/90378173

Published 42 original articles · won praise 115 · views 10000 +

  
  
  1. Package Penalty for 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. }

Guess you like

Origin blog.csdn.net/luoyong_blog/article/details/104497728