教你怎么用Java實(shí)現(xiàn)給圖片打上水印
更新時(shí)間:2021年04月30日 15:02:09 作者:牙刷冰冰
這篇文章主要介紹了教你怎么用Java實(shí)現(xiàn)給圖片打上水印,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
一、原圖片
二、打水印(文字)
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(); // 畫入原始圖像 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; // 字體長度 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; } // 釋放畫圖工具 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ì)算水印文本長度 //1、中文長度即文本長度 2、英文長度為文本長度二分之一 public static int getTextLength(String text) { //水印文字長度 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(); // 畫入原始圖像 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; } // 釋放畫圖工具 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-11java基于數(shù)據(jù)庫實(shí)現(xiàn)全局唯一ID的示例
本文主要介紹了java基于數(shù)據(jù)庫實(shí)現(xiàn)全局唯一ID的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04java使用httpclient發(fā)送post請(qǐng)求示例
這篇文章主要介紹了java使用httpclient發(fā)送post請(qǐng)求示例,依賴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ì)的介紹。如果可以的話,能不能畫一下實(shí)現(xiàn)自動(dòng)配置的流程圖。牽扯到哪些關(guān)鍵類,以及哪些關(guān)鍵點(diǎn)2022-12-12