图片水印的添加

由于项目中需要对图片添加水印,思路大致如下:

1.创建缓存图片对象(BufferedImage)

2.创建Java绘图工具对象(Graphics2D)

3.使用绘图工具对象将原图绘制到缓存图片

4.使用绘图工具将水印(文字)绘制到缓存图片对象中

5.创建图像编码工具类

6.使用图像编码工具类,输出缓存图像到目标图片文件

代码示例如下:

public class MarkService {

    public static final String MARK_TEXT = "xxxxxxxxxx";

    public static final String FONT_NAME = "微软雅黑";

    public static final int FONT_STYLE = Font.PLAIN;

    /**
     * 文字水印像素大小 默认高度
     */

    public static final Color FONT_COLOR = Color.red;

    public static final float ALPHA = 0.3f;

    public static final int x = 0;

    public static final int y = 0;

    public static int getTextLength(String text){

        int length = text.length();

        for(int i=0;i<text.length();i++){

            String str = String.valueOf(text.charAt(i));

            if(str.getBytes().length>1)

                length++;
        }

        length = (length%2==0) ? length/2:length/2+1;

        return length;

    }


    public void mark(String srcImgPath, String outImgPath, Color markContentColor, String waterMarkContent) {
        try {

            /**
             * 读取原图片信息
             */
            File srcImgFile = new File(srcImgPath);

            Image srcImg = ImageIO.read(srcImgFile);

            int srcImgWidth = srcImg.getWidth(null);

            int srcImgHeight = srcImg.getHeight(null);

            /**
             * 加水印
             */
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);

            Graphics2D g = bufImg.createGraphics();

            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);

            g.setColor(markContentColor);

            int FONT_SIZE = (srcImgWidth * 2 / 3) / getTextLength(waterMarkContent);

            g.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));

            g.drawString(waterMarkContent, (srcImgWidth - getWatermarkLength(waterMarkContent, g)) / 2, srcImgHeight - 5);

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));

            g.dispose();

            /**
             * 输出图片
             */
            FileOutputStream outImgStream = new FileOutputStream(outImgPath);

            ImageIO.write(bufImg, "jpg", outImgStream);

            outImgStream.flush();

            outImgStream.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {

        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }


    public static void main(String args[]){

        new MarkService().mark("C:/Users/Administrator/Desktop/lufei.jpg", "C:/Users/Administrator/Desktop/lufei1.jpg", Color.red, MARK_TEXT );

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值