教你怎么用Java實(shí)現(xiàn)給圖片打上水印
一、原圖片
二、打水印(文字)
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; public class ImageUtils { // 水印字體 private static final Font FONT = new Font("微軟雅黑", Font.PLAIN, 14); // 透明度 private static final AlphaComposite COMPOSITE = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f); // 水印之間的間隔 private static final int XMOVE = 150; // 水印之間的間隔 private static final int YMOVE = 200; /** * 打水印(文字) * * @param srcImgPath 源文件地址 * @param font 字體 * @param markContentColor 水印顏色 * @param waterMarkContent 水印內(nèi)容 */ public static void markWithContent(String srcImgPath, Font font, Color markContentColor, String waterMarkContent) { FileOutputStream fos = null; try { // 讀取原圖片信息 File srcFile = new File(srcImgPath); BufferedImage srcImg = ImageIO.read(srcFile); // 圖片寬、高 int imgWidth = srcImg.getWidth(); int imgHeight = srcImg.getHeight(); // 圖片緩存 BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); // 創(chuàng)建繪圖工具 Graphics2D g = bufImg.createGraphics(); // 畫(huà)入原始圖像 g.drawImage(srcImg, 0, 0, imgWidth, imgHeight, null); // 設(shè)置水印顏色 g.setColor(markContentColor); // 設(shè)置水印透明度 g.setComposite(COMPOSITE); // 設(shè)置傾斜角度 g.rotate(Math.toRadians(-35), (double) bufImg.getWidth() / 2, (double) bufImg.getHeight() / 2); // 設(shè)置水印字體 g.setFont(font); // 消除java.awt.Font字體的鋸齒 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int x = -imgWidth / 2; int y; // 字體長(zhǎng)度 int markWidth = FONT.getSize() * getTextLength(waterMarkContent); // 字體高度 int markHeight = FONT.getSize(); // 循環(huán)添加水印 while (x < imgWidth * 1.5) { y = -imgHeight / 2; while (y < imgHeight * 1.5) { g.drawString(waterMarkContent, x, y); y += markHeight + YMOVE; } x += markWidth + XMOVE; } // 釋放畫(huà)圖工具 g.dispose(); // 輸出圖片 fos = new FileOutputStream(srcFile); ImageIO.write(bufImg, "jpg", fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } } //計(jì)算水印文本長(zhǎng)度 //1、中文長(zhǎng)度即文本長(zhǎng)度 2、英文長(zhǎng)度為文本長(zhǎng)度二分之一 public static int getTextLength(String text) { //水印文字長(zhǎng)度 int length = text.length(); for (int i = 0; i < text.length(); i++) { String s = String.valueOf(text.charAt(i)); if (s.getBytes().length > 1) { length++; } } length = length % 2 == 0 ? length / 2 : length / 2 + 1; return length; } public static void main(String[] args) { ImageUtils.markWithContent("C:\\Users\\lbb\\Pictures\\test.jpg", FONT, Color.darkGray, "水印文字"); } }
打了水印后的效果
三、打水印(圖片)
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; public class ImageUtils { // 透明度 private static final AlphaComposite COMPOSITE = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f); // 水印之間的間隔 private static final int XMOVE = 150; // 水印之間的間隔 private static final int YMOVE = 200; /** * 打水印(圖片) * * @param srcImgPath 源圖片路徑 * @param markImgPath 水印圖片路徑 */ public static void markWithImg(String srcImgPath, String markImgPath) { FileOutputStream fos = null; try { // 讀取原始圖像 File srcFile = new File(srcImgPath); BufferedImage srcImg = ImageIO.read(srcFile); // 原始寬度 int srcImgWidth = srcImg.getWidth(); // 原始高度 int srcImgHeight = srcImg.getHeight(); // 最終圖像 BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB); // 創(chuàng)建繪圖工具 Graphics2D g = bufImg.createGraphics(); // 畫(huà)入原始圖像 g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null); // 讀取水印圖片 BufferedImage markImg = ImageIO.read(new File(markImgPath)); // 圖片寬、高 int markImgWidth = markImg.getWidth(); int markImgHeight = markImg.getHeight(); // 設(shè)置水印透明度 g.setComposite(COMPOSITE); // 設(shè)置傾斜角度 g.rotate(Math.toRadians(-35), (double) bufImg.getWidth() / 2, (double) bufImg.getHeight() / 2); // 循環(huán)添加水印 int x = -srcImgWidth / 2; int y; while (x < srcImgWidth * 1.5) { y = -srcImgHeight / 2; while (y < srcImgHeight * 1.5) { g.drawImage(markImg, x, y, null); y += markImgHeight + YMOVE; } x += markImgWidth + XMOVE; } // 釋放畫(huà)圖工具 g.dispose(); // 輸出圖片 fos = new FileOutputStream(srcFile); ImageIO.write(bufImg, "jpg", fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } } public static void main(String[] args) { ImageUtils.markWithImg("C:\\Users\\lbb\\Pictures\\test.jpg", "C:\\Users\\lbb\\Pictures\\mark.png"); } }
下面是水印圖片
下面是打了水印后的效果
到此這篇關(guān)于教你怎么用Java實(shí)現(xiàn)給圖片打上水印的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)給圖片打上水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 全角半角字符轉(zhuǎn)換的方法實(shí)例
這篇文章主要介紹了java 全角半角字符轉(zhuǎn)換的方法,大家參考使用吧2013-11-11通過(guò)原理解析Spring mvc的內(nèi)置編碼過(guò)濾器
這篇文章通過(guò)原理主要給大家解析了關(guān)于Spring mvc的內(nèi)置編碼過(guò)濾器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-09-09Java多線(xiàn)程中的Interrupt簡(jiǎn)析
這篇文章主要介紹了Java多線(xiàn)程中的Interrupt簡(jiǎn)析,Interrupt 的其作用是"中斷"線(xiàn)程, 但實(shí)際上線(xiàn)程仍會(huì)繼續(xù)運(yùn)行, 這是一個(gè)非常容易混淆的概念,Interrupt 的真正作用是給線(xiàn)程對(duì)象設(shè)置一個(gè)中斷標(biāo)記, 并不會(huì)影響線(xiàn)程的正常運(yùn)行,需要的朋友可以參考下2023-09-09java基于數(shù)據(jù)庫(kù)實(shí)現(xiàn)全局唯一ID的示例
本文主要介紹了java基于數(shù)據(jù)庫(kù)實(shí)現(xiàn)全局唯一ID的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04java使用httpclient發(fā)送post請(qǐng)求示例
這篇文章主要介紹了java使用httpclient發(fā)送post請(qǐng)求示例,依賴(lài)JSON、HTTPClient等jar包,需要的朋友可以參考下2014-02-02SpringBoot自動(dòng)配置實(shí)現(xiàn)流程詳細(xì)分析
這篇文章主要介紹了SpringBoot自動(dòng)配置原理分析,SpringBoot是我們經(jīng)常使用的框架,那么你能不能針對(duì)SpringBoot實(shí)現(xiàn)自動(dòng)配置做一個(gè)詳細(xì)的介紹。如果可以的話(huà),能不能畫(huà)一下實(shí)現(xiàn)自動(dòng)配置的流程圖。牽扯到哪些關(guān)鍵類(lèi),以及哪些關(guān)鍵點(diǎn)2022-12-12基于Java實(shí)現(xiàn)馬踏棋盤(pán)游戲算法
這篇文章主要為大家詳細(xì)介紹了基于Java實(shí)現(xiàn)馬踏棋盤(pán)游戲算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02