一、原图片

二、打水印(文字)

import javax.imageio.imageio;
import java.awt.*;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.fileoutputstream;

public class imageutils {

    // 水印字体
    private static final font font = new font("微软雅黑", font.plain, 14);

    // 透明度
    private static final alphacomposite composite = alphacomposite.getinstance(alphacomposite.src_over, 0.6f);

    // 水印之间的间隔
    private static final int xmove = 150;

    // 水印之间的间隔
    private static final int ymove = 200;
    
    /**
     * 打水印(文字)
     *
     * @param srcimgpath       源文件地址
     * @param font             字体
     * @param markcontentcolor 水印颜色
     * @param watermarkcontent 水印内容
     */
    public static void markwithcontent(string srcimgpath, font font, color markcontentcolor, string watermarkcontent) {
        fileoutputstream fos = null;
        try {
            // 读取原图片信息
            file srcfile = new file(srcimgpath);
            bufferedimage srcimg = imageio.read(srcfile);

            // 图片宽、高
            int imgwidth = srcimg.getwidth();
            int imgheight = srcimg.getheight();

            // 图片缓存
            bufferedimage bufimg = new bufferedimage(imgwidth, imgheight, bufferedimage.type_int_rgb);

            // 创建绘图工具
            graphics2d g = bufimg.creategraphics();

            // 画入原始图像
            g.drawimage(srcimg, 0, 0, imgwidth, imgheight, null);

            // 设置水印颜色
            g.setcolor(markcontentcolor);

            // 设置水印透明度
            g.setcomposite(composite);

            // 设置倾斜角度
            g.rotate(math.toradians(-35), (double) bufimg.getwidth() / 2, (double) bufimg.getheight() / 2);

            // 设置水印字体
            g.setfont(font);

            // 消除java.awt.font字体的锯齿
            g.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);

            int x = -imgwidth / 2;
            int y;

            // 字体长度
            int markwidth = font.getsize() * gettextlength(watermarkcontent);
            // 字体高度
            int markheight = font.getsize();

            // 循环添加水印
            while (x < imgwidth * 1.5) {
                y = -imgheight / 2;
                while (y < imgheight * 1.5) {
                    g.drawstring(watermarkcontent, x, y);
                    y += markheight + ymove;
                }
                x += markwidth + xmove;
            }

            // 释放画图工具
            g.dispose();

            // 输出图片
            fos = new fileoutputstream(srcfile);
            imageio.write(bufimg, "jpg", fos);
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (exception e) {
                    e.printstacktrace();
                }
            }
        }
    }


    //计算水印文本长度
    //1、中文长度即文本长度 2、英文长度为文本长度二分之一
    public static int gettextlength(string text) {
        //水印文字长度
        int length = text.length();

        for (int i = 0; i < text.length(); i++) {
            string s = string.valueof(text.charat(i));
            if (s.getbytes().length > 1) {
                length++;
            }
        }
        length = length % 2 == 0 ? length / 2 : length / 2 + 1;
        return length;
    }
    
    
    public static void main(string[] args) {
        imageutils.markwithcontent("c:\\users\\lbb\\pictures\\test.jpg", font, color.darkgray, "水印文字");
    }
}

打了水印后的效果

三、打水印(图片)

import javax.imageio.imageio;
import java.awt.*;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.fileoutputstream;

public class imageutils {

    // 透明度
    private static final alphacomposite composite = alphacomposite.getinstance(alphacomposite.src_over, 0.6f);

    // 水印之间的间隔
    private static final int xmove = 150;

    // 水印之间的间隔
    private static final int ymove = 200;

    /**
     * 打水印(图片)
     *
     * @param srcimgpath  源图片路径
     * @param markimgpath 水印图片路径
     */
    public static void markwithimg(string srcimgpath, string markimgpath) {
        fileoutputstream fos = null;

        try {
            // 读取原始图像
            file srcfile = new file(srcimgpath);
            bufferedimage srcimg = imageio.read(srcfile);

            // 原始宽度
            int srcimgwidth = srcimg.getwidth();
            // 原始高度
            int srcimgheight = srcimg.getheight();

            // 最终图像
            bufferedimage bufimg = new bufferedimage(srcimgwidth, srcimgheight, bufferedimage.type_int_rgb);

            // 创建绘图工具
            graphics2d g = bufimg.creategraphics();

            // 画入原始图像
            g.drawimage(srcimg, 0, 0, srcimgwidth, srcimgheight, null);

            // 读取水印图片
            bufferedimage markimg = imageio.read(new file(markimgpath));

            // 图片宽、高
            int markimgwidth = markimg.getwidth();
            int markimgheight = markimg.getheight();

            // 设置水印透明度
            g.setcomposite(composite);

            // 设置倾斜角度
            g.rotate(math.toradians(-35), (double) bufimg.getwidth() / 2, (double) bufimg.getheight() / 2);

            // 循环添加水印
            int x = -srcimgwidth / 2;
            int y;
            while (x < srcimgwidth * 1.5) {
                y = -srcimgheight / 2;
                while (y < srcimgheight * 1.5) {
                    g.drawimage(markimg, x, y, null);
                    y += markimgheight + ymove;
                }
                x += markimgwidth + xmove;
            }

            // 释放画图工具
            g.dispose();

            // 输出图片
            fos = new fileoutputstream(srcfile);
            imageio.write(bufimg, "jpg", fos);
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (exception e) {
                    e.printstacktrace();
                }
            }
        }
    }
    

    public static void main(string[] args) {
        imageutils.markwithimg("c:\\users\\lbb\\pictures\\test.jpg", "c:\\users\\lbb\\pictures\\mark.png");
    }
}

下面是水印图片

下面是打了水印后的效果

到此这篇关于教你怎么用java实现给图片打上水印的文章就介绍到这了,更多相关java实现给图片打上水印内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!