• java添加水印等比缩放



    /** * 图片天加文字水印(默认缩小scale) * 备注: * Positions.BOTTOM_RIGHT 表示水印位置 * * @param filePath 原图路径 * @param newFilePath 处理后新图片路径 * @param markText 水印文字 * @param scale 比例(0.1- 1.0) * @param transparency 水印透明度 * @param type 1 按比例 2按宽高 * @param widthSize 缩放宽 * @param heightSize 缩放高 */ public static boolean waterMarkWithText(String filePath, String newFilePath, String markText, float scale, float transparency, int widthSize, int heightSize, int type) { try { //先进行图片缩放 if (type == 1) { imgScale(scale, filePath,newFilePath); } else { imgSize(widthSize,heightSize, filePath,newFilePath); } //获取缩放图分辨率 File file = new File(newFilePath); BufferedImage imgBi = null; try { imgBi = ImageIO.read(file); } catch (Exception e) { e.printStackTrace(); } //图片原始宽度 int width = imgBi.getWidth(); //图片原始高度 int height = imgBi.getHeight(); //计算右下角水印高宽 int waterWidth = new Double(width *0.5).intValue(); int waterHeight = new Double(height).intValue(); ImgHandle im = new ImgHandle(); String randomNum = String.valueOf(System.currentTimeMillis()); BufferedImage bi = im.apply(imgBi, waterWidth, waterHeight, markText, 1, randomNum); BufferedImage bi2 = im.apply(imgBi, waterWidth, waterHeight, randomNum, 1, markText); Watermark watermark = new Watermark(Positions.BOTTOM_LEFT, bi, transparency); Watermark watermark2 = new Watermark(Positions.BOTTOM_RIGHT, bi2, transparency); Thumbnails.of(newFilePath).scale(1). watermark(watermark).toFile(newFilePath); watermark2(watermark2, newFilePath); return true; } catch (IOException e) { e.printStackTrace(); return false; } }
    /**
         * 生成的水印
         *
         * @param img
         * @return
         */
        public BufferedImage apply(BufferedImage img, int waterWidth, int waterHeight, String markText, float scale,
                                   String markText2) {
            int width = img.getWidth();
            int height = img.getHeight();
    
            BufferedImage imgWithWatermark = new BufferedImage(waterWidth, waterHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = imgWithWatermark.createGraphics();
    
            //设置透明  start
            imgWithWatermark = g.getDeviceConfiguration().createCompatibleImage(waterWidth, waterHeight, Transparency.TRANSLUCENT);
            g = imgWithWatermark.createGraphics();
            g.setColor(new Color(159, 160, 160));
            //设置透明  end
            int[] sizes = new int[]{60, 30, 20, 16, 14, 9,  8, 6, 4};
            int contentLength = 0;
            Font font = null;
            for (int i = 0; i < 8; i++) {
                //设置字体及大小
                font = new Font("Helvetica Regular", Font.BOLD, sizes[i]);
                g.setFont(font);
                g.drawRect(0, 0, 0, 0);
                contentLength = getWatermarkLength(markText + markText2, g);
                if (contentLength < width) {
                    //找到最合适的字体
                    break;
                }
            }
            //设置水印的坐标
            FontDesignMetrics metrics = FontDesignMetrics.getMetrics(g.getFont());
            int fontHeight = metrics.getHeight();
            int y = waterHeight - fontHeight;
            char[] data = markText.toCharArray();
            if (markText.contains("https")) {
                g.drawChars(data, 0, data.length, 4, y);
            } else {
    //这里的x1值需要根据你缩放后图片大小进行计算;
    int x1 = 0; if (width > 300) { x1 = 270; } else { x1 = 55; } g.drawChars(data, 0, data.length, x1, y); } return imgWithWatermark; }
    //添加水印
    public
    static boolean watermark2(Watermark watermark2, String filePath) { try { Thumbnails.of(filePath).scale(1). watermark(watermark2).toFile(filePath); return true; } catch (IOException e) { e.printStackTrace(); return false; } }
     /**
         * 计算水印文字宽度
         *
         * @param waterMarkContent
         * @param g
         * @return
         */
        public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
            return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
        }
    /**
         * 比例缩放
         *
         * @param scale
         * @param filePath
         * @param newFilePath
         * @return
         */
        public static boolean imgScale(float scale, String filePath, String newFilePath) {
            try {
                Thumbnails.of(filePath).scale(scale).toFile(newFilePath);
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 大小缩放
         * @param width
         * @param height
         * @param filePath
         * @param newFilePath
         * @return
         */
        public static boolean imgSize(int width,int height, String filePath, String newFilePath) {
            try {
                Thumbnails.of(filePath).size(width,height).toFile(newFilePath);
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
     public static void main(String[] args) {
            String filePath = "C:\Users\admin\Desktop\1000.jpg";
            String newFilePath = "C:\Users\admin\Desktop\800x800_2.jpg";
            boolean sc2 = waterMarkWithText(filePath, newFilePath, "https://licd.beijing2022.cn", 1f, 1.0f, 1000, 1000, 1);
    
        }

     

     

    等比缩放之后水印位置保持不变- -

  • 相关阅读:
    利用栈实现字符串中三种括号的匹配问题c++语言实现
    十进制数转N进制c++实现
    字符单链表识别数字,字母,其它字符,并分为三个循环链表的算法c++实现
    c++两数组合并算法
    c++顺序表(数组)查找最大最小值
    SSO单点登录、跨域重定向、跨域设置Cookie、京东单点登录实例分析
    php实现单点登录,顶级域名与子域名间共享Cookie实现单点登录原理
    php实现单点登录实例
    php实现SSO单点登录实例
    玩转音频、视频的利器:FFmpeg
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/9364707.html
Copyright © 2020-2023  润新知