Java实现图片添加单个文字水印、多个文字水印、图片水印、马赛克

版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/89380513

给图片添加文字水印

设置文字的位置、大小、样式、类型、透明度、颜色等

给图片添单个文字水印

/*
* 给图片添加单个文字水印类
* */
public class TextWatermarking {

    //定义图片水印字体类型
    public static final String FONT_NAME = "微软雅黑";


    //定义图片水印字体加粗、变细、倾斜等样式
    public static final int FONT_STYLE = Font.BOLD;

    //设置字体大小
    public static final int FONT_SIZE = 120;

    //设置文字透明程度
    public static float ALPHA = 0.3F;

    /**
     * 给图片添加单个文字水印、可设置水印文字旋转角度
     * source 需要添加水印的图片路径(如:F:/images/6.jpg)
     * outPut 添加水印后图片输出路径(如:F:/images/)
     * imageName 图片名称
     * imageType 图片类型
     * color 水印文字的颜色
     * word 水印文字
     * degree 水印文字旋转角度,为null表示不旋转
     */

    public Boolean markImageBySingleText(String sourcePath, String outputPath, String imageName, String imageType, Color color, String word, Integer degree) {

        try {

            //读取原图片信息
            File file = new File(sourcePath);

            if (!file.isFile()) {
                return false;
            }

            //获取源图像的宽度、高度
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);

            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            //创建绘图工具对象
            Graphics2D graphics2D = bufferedImage.createGraphics();
            //其中的0代表和原图位置一样
            graphics2D.drawImage(image, 0, 0, width, height, null);

            //设置水印文字(设置水印字体样式、粗细、大小)
            graphics2D.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));

            //设置水印颜色
            graphics2D.setColor(color);

            //设置水印透明度
            graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPHA));

            //设置水印旋转
            if (null != degree) {
                graphics2D.rotate(Math.toRadians(degree),(double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            int x = width - (FONT_SIZE * 4);
            int y = height/2;

            //进行绘制
            graphics2D.drawString(word, x, y);
            graphics2D.dispose();

            //输出图片
            File sf = new File(outputPath, imageName+"."+imageType);
            // 保存图片
            ImageIO.write(bufferedImage, imageType, sf);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}

给图片添加多个文字水印 

/*
* 给图片添加多个文字水印类
* */
public class TextWatermarking {

    //定义图片水印字体类型
    public static final String FONT_NAME = "微软雅黑";


    //定义图片水印字体加粗、变细、倾斜等样式
    public static final int FONT_STYLE = Font.BOLD;

    //设置字体大小
    public static final int FONT_SIZE = 120;

    //设置文字透明程度
    public static float ALPHA = 0.3F;

    /**
     * 给图片添加多个文字水印、可设置水印文字旋转角度
     * source 需要添加水印的图片路径
     * outPut 添加水印后图片输出路径
     * imageName 图片名称
     * imageType 图片类型
     * color 水印文字的颜色
     * word 水印文字
     * degree 水印文字旋转角度,为null表示不旋转
     */
    public Boolean markImageByMoreText(String sourcePath, String outputPath, String imageName, String imageType, Color color, String word, Integer degree) {
        try {
            //读取原图片信息
            File file = new File(sourcePath);

            if (!file.isFile()) {
                return false;
            }

            //获取源图像的宽度、高度
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);

            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            //创建绘图工具对象
            Graphics2D graphics2D = bufferedImage.createGraphics();
            //其中的0代表和原图位置一样
            graphics2D.drawImage(image, 0, 0, width, height, null);

            //设置水印文字(设置水印字体样式、粗细、大小)
            graphics2D.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));

            //设置水印颜色
            graphics2D.setColor(color);

            //设置水印透明度
            graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));

            //设置水印旋转
            if (null != degree) {
                graphics2D.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            int x = width / 3;
            int y = FONT_SIZE;
            int space = height / FONT_SIZE;
            for (int i = 0; i < space; i++) {
                //如果最后一个坐标的y轴比height高,直接退出
                if ((y + FONT_SIZE) > height) {
                    break;
                }
                //进行绘制
                graphics2D.drawString(word, x, y);
                y += (2 * FONT_SIZE);
            }

            graphics2D.dispose();

            //输出图片
            File sf = new File(outputPath, imageName + "." + imageType);
            // 保存图片
            ImageIO.write(bufferedImage, imageType, sf);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}

给图片添加图片水印

设置图片水印的位置、旋转角度等

给图片添单个图片水印

/*
* 给图片添加单个图片水印、可设置水印图片旋转角度
* */
public class ImageWatermark {

    /**
     *icon 水印图片路径(如:F:/images/icon.png)
     *source 没有加水印的图片路径(如:F:/images/6.jpg)
     *output 加水印后的图片路径(如:F:/images/)
     *imageName 图片名称(如:11111)
     *imageType 图片类型(如:jpg)
     *degree 水印图片旋转角度,为null表示不旋转
     */
    public Boolean markImageBySingleIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {

        try {
            File file = new File(source);
            File ficon = new File(icon);

            if (!file.isFile()) {
                return false;
            }

            //将icon加载到内存中
            Image ic = ImageIO.read(ficon);
            //icon高度
            int icheight = ic.getHeight(null);
            //将源图片读到内存中
            Image img = ImageIO.read(file);

            //图片宽
            int width = img.getWidth(null);
            //图片高
            int height = img.getHeight(null);

            BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            //创建一个指定 BufferedImage 的 Graphics2D 对象
            Graphics2D g = bi.createGraphics();

            //x,y轴默认是从0坐标开始
            int x = 0;
            int y = (height/2)-(icheight/2);

            //设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            //呈现一个图像,在绘制前进行从图像空间到用户空间的转换
            g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);

            if (null != degree) {
                //设置水印旋转
                g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
            }
            //水印图象的路径 水印一般为gif或者png的,这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(icon);
            //得到Image对象。
            Image con = imgIcon.getImage();
            //透明度,最小值为0,最大值为1
            float clarity = 0.6f;
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));
            //表示水印图片的坐标位置(x,y)
            //g.drawImage(con, 300, 220, null);
            g.drawImage(con, x, y, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            g.dispose();
            File sf = new File(output, imageName+"."+imageType);
            ImageIO.write(bi, imageType, sf); // 保存图片

        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}

给图片添加多个图片水印

/*
* 给图片不同位置添加多个图片水印、可设置水印图片旋转角度
* */
public class ImageWatermark {

    /**
     * icon 水印图片路径
     * source 没有加水印的图片路径
     * output 加水印后的图片路径
     * imageName 图片名称
     * imageType 图片类型
     * degree 水印图片旋转角度,为null表示不旋转
     */
    public Boolean markImageByMoreIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {

        try {
            File file = new File(source);
            
            File ficon = new File(icon);
            
            if (!file.isFile()) {
                return false;
            }
            //将icon加载到内存中
            Image ic = ImageIO.read(ficon);
            
            //icon高度
            int icheight = ic.getHeight(null);
            
            //将源图片读到内存中
            Image img = ImageIO.read(file);
            
            //图片宽
            int width = img.getWidth(null);
            //图片高
            int height = img.getHeight(null);
            
            BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            //创建一个指定 BufferedImage 的 Graphics2D 对象
            Graphics2D g = bi.createGraphics();
            
            //x,y轴默认是从0坐标开始
            int x = 0;
            int y = 0;
            
            //默认两张水印图片的间隔高度是水印图片的1/3
            int temp = icheight/3;
            int space = 1;
            if(height>=icheight){
                space = height/icheight;
                if(space>=2){
                    temp = y = icheight/2;
                    if(space==1||space==0){
                        x = 0;
                        y = 0;
                    }
                }
            }else{
                x = 0;
                y = 0;
            }
            
            //设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            
            //呈现一个图像,在绘制前进行从图像空间到用户空间的转换
            g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
            for(int i=0;i<space;i++){
                if (null != degree) {
                    //设置水印旋转
                    g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
                }
                //水印图象的路径 水印一般为gif或者png的,这样可设置透明度
                ImageIcon imgIcon = new ImageIcon(icon);
                //得到Image对象。
                Image con = imgIcon.getImage();
                //透明度,最小值为0,最大值为1
                float clarity = 0.6f;
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));
                //表示水印图片的坐标位置(x,y)
                //g.drawImage(con, 300, 220, null);
                g.drawImage(con, x, y, null);
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                y+=(icheight+temp);
            }
            g.dispose();
            File sf = new File(output, imageName+"."+imageType);
            ImageIO.write(bi, imageType, sf); // 保存图片
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}

给图片添加马赛克

设置马赛克的尺寸、每个马赛克的宽和高

/*
* 给图片添加马赛克
* */
public class MosaicImage {

    /**
     * source 原图片路径
     * output 打马赛克后,图片保存的路径
     * imageName 图片名称
     * imageType 图片类型
     * size 马赛克尺寸,即每个矩形的宽高
     */
    public Boolean markImageByMosaic(String source,String output,String imageName,String imageType,int size){

        try{
            File file = new File(source);
            if (!file.isFile()) {
                return false;
            }
            BufferedImage img = ImageIO.read(file); // 读取该图片
            int width = img.getWidth(null); //原图片宽
            int height = img.getHeight(null); //原图片高
            BufferedImage bi = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
            //马赛克格尺寸太大或太小
            if (width < size || height < size) {
                return false;
            }
            if(size<=0){
                return false;
            }
            int xcount = 0; //x方向绘制个数
            int ycount = 0; //y方向绘制个数

            if (width % size == 0) {
                xcount = width / size;
            } else {
                xcount = width / size + 1;
            }

            if (height % size == 0) {
                ycount = height / size;
            } else {
                ycount = height / size + 1;
            }

            int x = 0; //x坐标
            int y = 0;//y坐标

            //绘制马赛克(绘制矩形并填充颜色)
            Graphics2D g = bi.createGraphics();
            for (int i = 0; i < xcount; i++) {
                for (int j = 0; j < ycount; j++) {
                    //马赛克矩形格大小
                    int mwidth = size;
                    int mheight = size;
                    if(i==xcount-1){  //横向最后一个不够一个size
                        mwidth = width-x;
                    }

                    if(j == ycount-1){ //纵向最后一个不够一个size
                        mheight = height-y;
                    }

                    //矩形颜色取中心像素点RGB值
                    int centerX = x;
                    int centerY = y;

                    if (mwidth % 2 == 0) {
                        centerX += mwidth / 2;
                    } else {
                        centerX += (mwidth - 1) / 2;
                    }

                    if (mheight % 2 == 0) {
                        centerY += mheight / 2;
                    } else {
                        centerY += (mheight - 1) / 2;
                    }

                    Color color = new Color(img.getRGB(centerX, centerY));
                    g.setColor(color);
                    g.fillRect(x, y, mwidth, mheight);
                    y = y + size;// 计算下一个矩形的y坐标
                }

                y = 0;// 还原y坐标
                x = x + size;// 计算x坐标
            }
            g.dispose();
            File sf = new File(output, imageName+"."+imageType);
            ImageIO.write(bi, imageType, sf); // 保存图片

        }catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/89380513