zoukankan      html  css  js  c++  java
  • 图片左下角添加水印

    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    
    /**
     * 水印工具类
     *
     * @author Ade.Xiao 2021/1/28 11:11
     */
    public class AddWatermarkUtil {
        /**
         * 给图谱添加水印
         *
         * @param srcImgPath       源文件
         * @param outImgPath       目标文件
         * @param markContentColor 文字颜色
         * @param fontSize         文字大小
         * @param waterMarkContent 文字内容
         * @author Ade.Xiao 2021/1/28 11:17
         */
        private static void waterPress(String srcImgPath, String outImgPath, Color markContentColor, int fontSize, String waterMarkContent) {
            try {
                // || 做分隔换行
                String[] waterMarkContents = waterMarkContent.split("\|\|");
                // 读取原图片信息
                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);
                Font font = new Font("宋体", Font.PLAIN, fontSize);
                // 根据图片的背景设置水印颜色
                g.setColor(markContentColor);
                // 设置水印文字字体
                g.setFont(font);
                // 数组长度
                int contentLength = waterMarkContents.length;
                // 获取水印文字中最长的
                int maxLength = 0;
                for (int i = 0; i < contentLength; i++) {
                    int fontlen = getWatermarkLength(waterMarkContents[i], g);
                    if (maxLength < fontlen) {
                        maxLength = fontlen;
                    }
                }
    
                for (int j = 0; j < contentLength; j++) {
                    waterMarkContent = waterMarkContents[j];
                    int tempX = 10;
                    int tempY = fontSize;
                    // 单字符长度
                    int tempCharLen = 0;
                    // 单行字符总长度临时计算
                    int tempLineLen = 0;
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < waterMarkContent.length(); i++) {
                        char tempChar = waterMarkContent.charAt(i);
                        tempCharLen = getCharLen(tempChar, g);
                        tempLineLen += tempCharLen;
                        if (tempLineLen >= srcImgWidth) {
    
                            //左下角
                            // g.drawString(sb.toString(), 0, srcImgHeight - (contentLength - j - 1) * tempY);
                            //右下角
                            // g.drawString(sb.toString(), srcImgWidth - maxLength, srcImgHeight - (contentLength - j - 1) * tempY);
    
                            // 长度已经满一行,进行文字叠加
                            g.drawString(sb.toString(), tempX, tempY);
                            // 清空内容,重新追加
                            sb.delete(0, sb.length());
                            tempLineLen = 0;
                        }
                        // 追加字符
                        sb.append(tempChar);
                    }
                    // 通过设置后两个输入参数给水印定位
                    g.drawString(sb.toString(), 20, srcImgHeight - (contentLength - j - 1) * tempY - 50);
                }
                g.dispose();
    
                // 输出图片
                FileOutputStream outImgStream = new FileOutputStream(outImgPath);
                ImageIO.write(bufImg, "jpg", outImgStream);
                outImgStream.flush();
                outImgStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 字符长度
         *
         * @param c 字符
         * @param g 字符字体
         * @return
         * @author Ade.Xiao 2021/1/28 11:19
         */
        private static int getCharLen(char c, Graphics2D g) {
            return g.getFontMetrics(g.getFont()).charWidth(c);
        }
    
        /**
         * 获取水印文字总长度
         *
         * @paramwaterMarkContent水印的文字
         * @paramg
         * @return水印文字总长度
         * @author Ade.Xiao 2021/1/28 11:16
         */
        private static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
            return g.getFontMetrics(g.getFont())
                    .charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
        }
    
    
        public static void main(String[] args) {
            // 原图位置, 输出图片位置, 水印文字颜色, 水印文字
            String font = "2021年01月27日 13:00:34||宁夏回族自治州银川贺楠县xxx路7号||XXXXX贸易有限公司||(11.1231231,32.2222)";
            String inputAddress = "C:\Users\ALIENWARE-AREA-51M\Desktop\drivingLicenseBack.jpg";
            String outputAddress = "C:\Users\ALIENWARE-AREA-51M\Desktop\drivingLicenseBack2.jpg";
            Color color = Color.RED;
            waterPress(inputAddress, outputAddress, color, 50, font);
        }
    }
    

      

  • 相关阅读:
    Linux上查找最大文件的 3 种方法
    不念过去,不畏将来
    Weblogic/WAS之Full GC监控与计算
    EntityFramework:An error occurred while executing the command definition. See the inner exception for details.
    1051 复数乘法(C#)
    BACnet开发资料与调试工具
    JS设置cookie、读取cookie、删除cookie
    认识BACnet协议
    Unity WebGL请求Http接口出现的Cors跨域问题
    VS 2017 产品密钥
  • 原文地址:https://www.cnblogs.com/xiaojf/p/14339351.html
Copyright © 2011-2022 走看看