图片添加水印

优化的图片添加水印问题,此为spring mvc老项目的方法经过优化而来,
boot框架的可以简单看看逻辑, mvc的直接用


    /**
     * 图片添加水印
     *
     * @param srcImgPath       需要添加水印的图片的路径
     * @param outImgPath       添加水印后图片输出路径
     * @param fontType         水印文字的字体
     * @param fontStyle        水印文字的字体份风格
     * @param markContentColor 水印文字的颜色
     * @param fontSize         水印的文字的大小
     * @param waterMarkContent 水印的文字内容
     */
    public void waterPress(String srcImgPath, String outImgPath, String fontType, int fontStyle, Color markContentColor, int fontSize, String waterMarkContent) {
        try {
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);
            Image srcImg = null;
            if (srcImgFile.exists() && srcImgFile.isFile() && srcImgFile.canRead()) {
                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);
            Font font = new Font(fontType, fontStyle, fontSize);
            //设置水印颜色
            g.setColor(markContentColor);
            g.setFont(font);
            // 抗锯齿
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int fontLength = getWatermarkLength(waterMarkContent, g);
            // 实际生成的水印文字,实际文字行数
            Double textLineCount = Math.ceil(Integer.valueOf(fontLength).doubleValue() / Integer.valueOf(srcImgWidth).doubleValue());
            // 实际所有的水印文字的高度
            int textHeight = textLineCount.intValue() * fontSize;
            // 相对与X的起始的位置
            int originX = 0;
            // 相对与Y的起始的位置
            int originY = 0;
            // 实际文字大于1行,则x则为默认起始0,
            if (1 == textLineCount.intValue()) {
                // 实际文字行数是1,1/2个图片高度,减去1/2个字符高度
                originY = srcImgHeight / 2 - fontSize / 2 +30;
                // 实际文字行数是1,计算x的居中的起始位置
                originX = (srcImgWidth - fontLength) / 2;
            } else {
                // 实际文字行数大于1,1/2个图片高度减去文字行数所需的高度
                originY = (srcImgHeight - textHeight) / 2 +30;
            }
            System.out.println("水印文字总长度:" + fontLength + ",图片宽度:" + srcImgWidth + ",字符个数:" + waterMarkContent.length());
            //文字叠加,自动换行叠加
            int tempX = originX;
            int tempY = originY;
            int tempCharLen = 0;//单字符长度
            int tempLineLen = 0;//单行字符总长度临时计算
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < waterMarkContent.length(); i++) {
                char tempChar = waterMarkContent.charAt(i);
                tempCharLen = getCharLen(tempChar, g);
                if (tempLineLen >= srcImgWidth) {
                    // 绘制前一行
                    g.drawString(stringBuffer.toString(), tempX, tempY);
                    //清空内容,重新追加
                    stringBuffer.delete(0, stringBuffer.length());
                    //文字长度已经满一行,Y的位置加1字符高度
                    tempY = tempY + fontSize;
                    tempLineLen = 0;
                }
                //追加字符
                stringBuffer.append(tempChar);
                tempLineLen += tempCharLen;
            }
            //最后叠加余下的文字
            g.drawString(stringBuffer.toString(), tempX, tempY);
            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(outImgPath);
            ImageIO.write(bufImg, "png", outImgStream);
            outImgStream.flush();
            outImgStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	
	
	    public int getCharLen(char c, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charWidth(c);
    }




    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }
	
	
	    public static void main(String[] args) {
        // 原图位置, 输出图片位置, 水印字体,水印文字样式,水印文字颜色, 水印文字大小,水印文字内容
        String fontType = "宋体";
        int fontStyle = Font.BOLD;
        int fontSize = 30;
        String font = "张三丰";
        new ImageMarkLogoUtil().waterPress("src/main/webapp/back/img/water-backgroud.png", "src/main/webapp/back/img/temp.png", fontType, fontStyle, Color.black, fontSize, font);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boJIke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值