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);

    }

 

 

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

 

posted @ 2018-07-25 10:53  748573200000  阅读(1414)  评论(0编辑  收藏  举报