/** * 圖片天加文字水印(默認縮小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); }
等比縮放之后水印位置保持不變- -