用java實現給圖片增加圖片水印或者文字水印(也支援影片影像幀添加水印)

  • 2019 年 11 月 1 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/eguid_1/article/details/52973508

前言:

這是個很常用的操作,一般我們的網站在用戶上傳圖片時都會給圖片添加一個水印以防止其他站點盜圖的行為

實現功能:①給圖片增加文字水印②給圖片增加圖片水印

一、核心功能實現:

1、添加文字水印

// 加文字水印  	public void mark(BufferedImage bufImg, Image img, String text, Font font, Color color, int x, int y) {  		Graphics2D g = bufImg.createGraphics();  		g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);  		g.setColor(color);  		g.setFont(font);  		g.drawString(text, x, y);  		g.dispose();  	}

2、添加圖片水印

// 加圖片水印  	public void mark(BufferedImage bufImg, Image img, Image markImg, int width, int height, int x, int y) {  		Graphics2D g = bufImg.createGraphics();  		g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);  		g.drawImage(markImg, x, y, width, height, null);  		g.dispose();  	}

以上兩個方法實現了最核心的功能:給圖片添加水印。

補充:

為什麼要這麼寫?

考慮到該方法不僅可以用於給圖片文件增加水印,而且還可以給影片的每一幀影像也添加,所以為了方便不同場合重複使用,去除了不必要的依賴關係。

有了核心的兩個方法就我們可以給圖片文件增加水印了

二、功能性實現

1、給圖片增加文字水印

/**  	 * 給圖片增加文字水印  	 *  	 * @param imgPath  	 *            -要添加水印的圖片路徑  	 * @param outImgPath  	 *            -輸出路徑  	 * @param text-文字  	 * @param font  	 *            -字體  	 * @param color  	 *            -顏色  	 * @param x  	 *            -文字位於當前圖片的橫坐標  	 * @param y  	 *            -文字位於當前圖片的豎坐標  	 */  	public void mark(String imgPath, String outImgPath, String text, Font font, Color color, int x, int y) {  		try {  			// 讀取原圖片資訊  			File imgFile = null;  			Image img = null;  			if (imgPath != null) {  				imgFile = new File(imgPath);  			}  			if (imgFile != null && imgFile.exists() && imgFile.isFile() && imgFile.canRead()) {  				img = ImageIO.read(imgFile);  			}  			int imgWidth = img.getWidth(null);  			int imgHeight = img.getHeight(null);  			// 加水印  			BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);  			mark(bufImg, img, text, font, color, x, y);  			// 輸出圖片  			FileOutputStream outImgStream = new FileOutputStream(outImgPath);  			ImageIO.write(bufImg, "jpg", outImgStream);  			outImgStream.flush();  			outImgStream.close();  		} catch (Exception e) {  			e.printStackTrace();  		}  	}

2、給圖片增加圖片水印

/**  	 * 給圖片增加圖片水印  	 *  	 * @param inputImg  	 *            -源圖片,要添加水印的圖片  	 * @param markImg  	 *            - 水印圖片  	 * @param outputImg  	 *            -輸出圖片(可以是源圖片)  	 * @param width  	 *            - 水印圖片寬度  	 * @param height  	 *            -水印圖片高度  	 * @param x  	 *            -橫坐標,相對於源圖片  	 * @param y  	 *            -縱坐標,同上  	 */  	public void mark(String inputImg, String markImg, String outputImg, int width, int height, int x, int y) {  		// 讀取原圖片資訊  		File inputImgFile = null;  		File markImgFile = null;  		Image img = null;  		Image mark = null;  		try {  			if (inputImg != null && markImg != null) {  				inputImgFile = new File(inputImg);  				markImgFile = new File(markImg);  			}  			if (inputImgFile != null && inputImgFile.exists() && inputImgFile.isFile() && inputImgFile.canRead()) {    				img = ImageIO.read(inputImgFile);    			}  			if (markImgFile != null && markImgFile.exists() && markImgFile.isFile() && markImgFile.canRead()) {    				mark = ImageIO.read(markImgFile);    			}  			int imgWidth = img.getWidth(null);  			int imgHeight = img.getHeight(null);  			BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);  			mark(bufImg, img, mark, width, height, x, y);  			FileOutputStream outImgStream = new FileOutputStream(outputImg);  			ImageIO.write(bufImg, "jpg", outImgStream);  			outImgStream.flush();  			outImgStream.close();  		} catch (IOException e) {  			e.printStackTrace();  		}  	}

3、測試一下效果

文字水印的字體和顏色需要自行定義,這裡我們使用宋體,14號字體,顏色選擇橙色 – -!,坐標是x軸0,y軸等於字體的大小,也就是圖片的左上角。

public static void main(String[] args) {  		Font font = new Font("宋體", Font.PLAIN, 14);  		// 原圖位置, 輸出圖片位置, 水印文字顏色, 水印文字  		// new MarkText4J().mark("eguidMarkText2.jpg", "eguidMarkText2.jpg", "水印效果測試", font, Color.ORANGE, 0, 14);  		// 增加圖片水印  		new MarkText4J().mark("eguidMarkText2.jpg", "eguid.jpg", "eguidMarkText3.jpg", 40, 20, 0, 14);  	}

簡直棒極了,至此我們可以隨意的給圖片任意位置添加任意文字或者圖片水印了。