Java分享--给图片添加文字水印(文字可旋转)

这篇文章主要介绍了Java实现给图片添加文字水印,文字水印的方法,涉及java针对图片的读取、水印添加设置等相关操作技巧,需要的朋友可以参考下 。

      很多时候项目中的图片需要一定的版权,就是人家拿出去用的时候不希望是原始图片,应该是要使用我们自己添加标识的一些图片,那么就想到了可以给图片添加文字水印、旋转等功能。

      直接上代码,这个是添加文字水印的方法,注意的一点是 文字添加的位置你可以自己设置,根据当前的图片原始高度和宽度来设置,这个方法里面 我是设置的添加在图片右下角位置。

package com.myFirstSpring.test; 

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/** 
 * @author tqf
 * @version 创建时间:2020-4-3 上午10:49:02 
 * 类说明:图片添加水印   文字&图片水印 
 */
public class ImageRemarkUtil {

    // 水印透明度
    private static float alpha = 0.5f;
    // 水印横向位置
    private static int positionWidth = 150;
    // 水印纵向位置
    private static int positionHeight = 300;
    // 水印文字字体
    private static Font font = new Font("宋体", Font.BOLD, 35);
    // 水印文字颜色
    private static Color color = Color.red;

    /**
     * 
     * @param alpha
     *            水印透明度
     * @param positionWidth
     *            水印横向位置
     * @param positionHeight
     *            水印纵向位置
     * @param font
     *            水印文字字体
     * @param color
     *            水印文字颜色
     */
    public static void setImageMarkOptions(float alpha, int positionWidth,
            int positionHeight, Font font, Color color) {
        if (alpha != 0.0f)
            ImageRemarkUtil.alpha = alpha;
        if (positionWidth != 0)
            ImageRemarkUtil.positionWidth = positionWidth;
        if (positionHeight != 0)
            ImageRemarkUtil.positionHeight = positionHeight;
        if (font != null)
            ImageRemarkUtil.font = font;
        if (color != null)
            ImageRemarkUtil.color = color;
    }

   

    /**
     * 给图片添加水印文字、可设置水印文字的旋转角度
     * 
     * @param logoText
     * @param srcImgPath
     * @param targerPath
     * @param degree
     */
    public static void markImageByText(String logoText, String srcImgPath,
            String targerPath, Integer degree) {

        InputStream is = null;
        OutputStream os = null;
        try {
            // 1、源图片
            Image srcImg = ImageIO.read(new File(srcImgPath));
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);
            // 4、设置水印旋转
            if (null != degree) {
    			g.rotate(Math.toRadians(degree),
                (double) buffImg.getWidth() / 2,
                (double) buffImg.getHeight() / 2);
            }
            // 5、设置水印文字颜色
            g.setColor(color);
            // 6、设置水印文字Font
            g.setFont(new Font("宋体", Font.BOLD, 20));
            // 7、设置水印文字透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
            // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
            int  width = srcImg.getWidth(null)-100;
            int  heigth = srcImg.getHeight(null) - 20;
            g.drawString(logoText, width, heigth);
            // 9、释放资源
            g.dispose();
            // 10、生成图片
            os = new FileOutputStream(targerPath);
            ImageIO.write(buffImg, "JPG", os);
            System.out.println("图片完成添加水印文字");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is)
                    is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
    	//原始图片路径
        String srcImgPath = "d:/demo.png";  //只需要这一张图片 然后生成4张图片
        //添加的文本
        String logoText = "心觉咨询";

        String targerTextPath = "d:/demo_wz.png";  //添加文字水印之后的图片存放路径
        String targerTextPath2 = "d:/demo_wz2.png";//添加文字 文字旋转-45 水印之后的图片存放路径

        System.out.println("给图片添加水印文字开始...");
        // 给图片添加水印文字
        markImageByText(logoText, srcImgPath, targerTextPath,null);
        
        // 给图片添加水印文字,水印文字旋转-45
        markImageByText(logoText, srcImgPath, targerTextPath2, 45);
        System.out.println("给图片添加水印文字结束...");
        

    }

}
 

下面是上面的2个方法生成的添加水印之后的图片,第一张是没有旋转文字的,第二张是旋转了文字-45度

好了以上写的关于图片添加文字水印方面的方法,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果对你有用 点赞评论收藏 加关注^_^ 

发布了102 篇原创文章 · 获赞 131 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/tanqingfu1/article/details/105291999