- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- 教你怎么用Java實(shí)現給圖片打上水印
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 水印內容 */ 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); // 設置水印顏色 g.setColor(markContentColor); // 設置水印透明度 g.setComposite(COMPOSITE); // 設置傾斜角度 g.rotate(Math.toRadians(-35), (double) bufImg.getWidth() / 2, (double) bufImg.getHeight() / 2); // 設置水印字體 g.setFont(font); // 消除java.awt.Font字體的鋸齒 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int x = -imgWidth / 2; int y; // 字體長(chá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(); } } } } //計算水印文本長(cháng)度 //1、中文長(cháng)度即文本長(cháng)度 2、英文長(cháng)度為文本長(cháng)度二分之一 public static int getTextLength(String text) { //水印文字長(chá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(); // 設置水印透明度 g.setComposite(COMPOSITE); // 設置傾斜角度 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í)現給圖片打上水印的文章就介紹到這了,更多相關(guān)Java實(shí)現給圖片打上水印內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站