zoukankan      html  css  js  c++  java
  • java 为图片添加水印(文字水印、图片水印)

    1.情景展示

      如何使用java实现为图片添加水印的功能?

    2.导包

      使用jdk即可实现功能开发,不需要额外的jar包。

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;

    3.添加图片水印

    /**
     * 为图片添加水印
     * @explain 文字水印、图片水印
     * @author Marydon
     * @creationTime 2020年5月15日上午10:30:01
     * @version 1.0
     * @since
     * @email marydon20170307@163.com
     */
    public class Watermark {
    
        /**
         * 把图片印刷到图片上
         * @param pressImg
         *            水印文件
         * @param targetImg
         *            目标文件
         * @param x
         *            x坐标
         * @param y
         *            y坐标
         */
        public final static void pressImage(String pressImg, String targetImg, int x, int y) {
            try {
                // 目标文件
                File _file = new File(targetImg);
                Image src = ImageIO.read(_file);
    
                int wideth = src.getWidth(null);
                int height = src.getHeight(null);
                BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
                Graphics g = image.createGraphics();
                g.drawImage(src, 0, 0, wideth, height, null);
    
                // 水印文件
                File _filebiao = new File(pressImg);
                Image src_biao = ImageIO.read(_filebiao);
                int weight_biao = src_biao.getWidth(null);
                int height_biao = src_biao.getHeight(null);
    
                g.drawImage(src_biao, x, y, weight_biao, height_biao, null);
    
                // 水印结束
                g.dispose();
                FileOutputStream out = new FileOutputStream(targetImg);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(image);
                out.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    4.添加文字水印

    /**
     * 打印文字水印图片
     * @param pressText
     *            文字
     * @param targetImg
     *            目标图片
     * @param fontName
     *            字体名
     * @param fontStyle
     *            字体样式
     * @param color
     *            字体颜色
     * @param fontSize
     *            字体大小
     * @param x
     *            偏移量
     * @param y
     *            偏移量
     */
    public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color,
    		int fontSize, int x, int y) {
    	try {
    		File _file = new File(targetImg);
    		Image src = ImageIO.read(_file);
    		int weidth = src.getWidth(null);
    		int height = src.getHeight(null);
    		BufferedImage image = new BufferedImage(weidth, height, BufferedImage.TYPE_INT_RGB);
    		Graphics g = image.createGraphics();
    		g.drawImage(src, 0, 0, weidth, height, null);
    		g.setColor(color);
    		g.setFont(new Font(fontName, fontStyle, fontSize));
    		g.drawString(pressText, x, y);
    		g.dispose();
    		FileOutputStream out = new FileOutputStream(targetImg);
    		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    		encoder.encode(image);
    		out.close();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
    

    5.效果展示  

      原图片

      图片水印

       将上面的水印添加到原图片右下角

    public static void main(String[] args) {
        String waterMarkImgPath = "C:\Users\Marydon\Desktop\水印-博客2.png";
        String targetImgPath = "C:\Users\Marydon\Desktop\TIM图片20200515162045.jpg";
        pressImage(waterMarkImgPath, targetImgPath, 880, 560);
    }  

      文字水印

    public static void main(String[] args) {
        pressText("@博人传", "C:\Users\Marydon\Desktop\TIM图片20200515162045.jpg", "宋体", 0, new Color(248, 248, 255), 50, 1100, 570);
    }
    

      说明:

      如上面的效果展示的那样,要想将水印打印到图片指定位置,进行动态设置的话,需要:

      第一,原图片大小;

      第二,水印大小;

      满足这两个条件才能将水印打印到图片的指定位置,也就是可以进行动态位移。 

      其中,图片水印可以进行动态设置,而文字水印则实现不了,因为我们无法获取文字水印的大小。

      调试水印输出位置也是个细致活呀。

      另外,水印的偏移量一般情况下均为正值,图片左上角为起点0,0,不同于数学上的Y轴,向下偏移用正值表示。

    写在最后

      哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

     相关推荐:

  • 相关阅读:
    解决bash: less: command not found
    IDEA-相关插件使用
    如何理解多租户架构?
    mybatis自动生成model、dao及对应的mapper.xml文件
    IDEA设置提示生成序列化ID
    [DUBBO] qos-server can not bind localhost:22222错误解决
    @NotNull,@NotEmpty,@NotBlank区别
    (三)IDEA使用,功能面板
    PHP实现自己活了多少岁
    使用PHP函数输出前一天的时间和后一天的时间
  • 原文地址:https://www.cnblogs.com/Marydon20170307/p/12895754.html
Copyright © 2011-2022 走看看