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


    
    
  1. package com.qihui.qxj.utils;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import javax.imageio.ImageIO;
  12. import com.sun.image.codec.jpeg.JPEGCodec;
  13. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  14. public class Main {
  15. public static void main(String[] args) throws IOException {
  16. /* String sourceImg="D:\\test.jpg"; //源图片地址
  17. String targetImg="D:\\123.jpg"; //新存储的地址
  18. addWatermark(sourceImg, targetImg);*/
  19. pressImage( "d:/1557906953.jpg", "d:/1557814473(1).jpg");
  20. }
  21. /**
  22. * 把图片印刷到图片上
  23. *
  24. * @param pressImg --
  25. * 水印文件
  26. * @param targetImg --
  27. * 目标文件
  28. */
  29. public final static void pressImage(String pressImg, String targetImg) {
  30. try {
  31. //目标文件
  32. File _file = new File(targetImg);
  33. Image src = ImageIO.read(_file);
  34. int wideth = src.getWidth( null);
  35. int height = src.getHeight( null);
  36. BufferedImage image = new BufferedImage(wideth, height,
  37. BufferedImage.TYPE_INT_RGB);
  38. Graphics g = image.createGraphics();
  39. g.drawImage(src, 0, 0, wideth, height, null);
  40. //水印文件
  41. File _filebiao = new File(pressImg);
  42. Image src_biao = ImageIO.read(_filebiao);
  43. int wideth_biao = src_biao.getWidth( null);
  44. int height_biao = src_biao.getHeight( null);
  45. g.drawImage(src_biao, 3000,
  46. 2020, wideth_biao, height_biao, null);
  47. //水印文件结束
  48. g.dispose();
  49. FileOutputStream out = new FileOutputStream(targetImg);
  50. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  51. encoder.encode(image);
  52. out.close();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. /** 设置文字水印
  58. * @param sourceImg 源图片路径
  59. * @param targetImg 保存的图片路径
  60. * @throws IOException
  61. */
  62. public static void addWatermark(String sourceImg, String targetImg) throws IOException {
  63. File srcImgFile = new File(sourceImg);
  64. Image srcImg = ImageIO.read(srcImgFile);
  65. int srcImgWidth = srcImg.getWidth( null);
  66. int srcImgHeight = srcImg.getHeight( null);
  67. int fontSize= 75;
  68. Font font = new Font( "黑体,Arial", 1, fontSize);
  69. BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
  70. Graphics2D g = bufImg.createGraphics();
  71. g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
  72. g.setColor(Color.BLACK);
  73. g.setFont(font);
  74. //设置水印的坐标
  75. g.drawString( "测试", 3196, 150);
  76. g.drawString( "性别:男", 3196, 150+( 120* 1));
  77. g.drawString( "毕业学校:XXX大学", 3196, 150+( 120* 2));
  78. g.setColor(Color.white);
  79. font = new Font( "黑体,Arial", 1, 120);
  80. g.setFont(font);
  81. g.drawString( "广东省深圳(SHENZHEN)", 3000, 2020);
  82. g.dispose();
  83. // 输出图片
  84. FileOutputStream outImgStream = new FileOutputStream(targetImg);
  85. ImageIO.write(bufImg, "jpg", outImgStream);
  86. System.out.println( "添加水印完成");
  87. outImgStream.flush();
  88. outImgStream.close();
  89. }
  90. }

红色选中的部分是添加的水印。 

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

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

  
  
  1. package com.qihui.qxj.utils;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import javax.imageio.ImageIO;
  12. import com.sun.image.codec.jpeg.JPEGCodec;
  13. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  14. public class Main {
  15. public static void main(String[] args) throws IOException {
  16. /* String sourceImg="D:\\test.jpg"; //源图片地址
  17. String targetImg="D:\\123.jpg"; //新存储的地址
  18. addWatermark(sourceImg, targetImg);*/
  19. pressImage( "d:/1557906953.jpg", "d:/1557814473(1).jpg");
  20. }
  21. /**
  22. * 把图片印刷到图片上
  23. *
  24. * @param pressImg --
  25. * 水印文件
  26. * @param targetImg --
  27. * 目标文件
  28. */
  29. public final static void pressImage(String pressImg, String targetImg) {
  30. try {
  31. //目标文件
  32. File _file = new File(targetImg);
  33. Image src = ImageIO.read(_file);
  34. int wideth = src.getWidth( null);
  35. int height = src.getHeight( null);
  36. BufferedImage image = new BufferedImage(wideth, height,
  37. BufferedImage.TYPE_INT_RGB);
  38. Graphics g = image.createGraphics();
  39. g.drawImage(src, 0, 0, wideth, height, null);
  40. //水印文件
  41. File _filebiao = new File(pressImg);
  42. Image src_biao = ImageIO.read(_filebiao);
  43. int wideth_biao = src_biao.getWidth( null);
  44. int height_biao = src_biao.getHeight( null);
  45. g.drawImage(src_biao, 3000,
  46. 2020, wideth_biao, height_biao, null);
  47. //水印文件结束
  48. g.dispose();
  49. FileOutputStream out = new FileOutputStream(targetImg);
  50. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  51. encoder.encode(image);
  52. out.close();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. /** 设置文字水印
  58. * @param sourceImg 源图片路径
  59. * @param targetImg 保存的图片路径
  60. * @throws IOException
  61. */
  62. public static void addWatermark(String sourceImg, String targetImg) throws IOException {
  63. File srcImgFile = new File(sourceImg);
  64. Image srcImg = ImageIO.read(srcImgFile);
  65. int srcImgWidth = srcImg.getWidth( null);
  66. int srcImgHeight = srcImg.getHeight( null);
  67. int fontSize= 75;
  68. Font font = new Font( "黑体,Arial", 1, fontSize);
  69. BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
  70. Graphics2D g = bufImg.createGraphics();
  71. g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
  72. g.setColor(Color.BLACK);
  73. g.setFont(font);
  74. //设置水印的坐标
  75. g.drawString( "测试", 3196, 150);
  76. g.drawString( "性别:男", 3196, 150+( 120* 1));
  77. g.drawString( "毕业学校:XXX大学", 3196, 150+( 120* 2));
  78. g.setColor(Color.white);
  79. font = new Font( "黑体,Arial", 1, 120);
  80. g.setFont(font);
  81. g.drawString( "广东省深圳(SHENZHEN)", 3000, 2020);
  82. g.dispose();
  83. // 输出图片
  84. FileOutputStream outImgStream = new FileOutputStream(targetImg);
  85. ImageIO.write(bufImg, "jpg", outImgStream);
  86. System.out.println( "添加水印完成");
  87. outImgStream.flush();
  88. outImgStream.close();
  89. }
  90. }

猜你喜欢

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