教你怎么用Java實現(xiàn)給圖片打上水印
更新時間:2021年04月30日 15:02:09 作者:牙刷冰冰
這篇文章主要介紹了教你怎么用Java實現(xiàn)給圖片打上水印,文中有非常詳細的代碼示例,對正在學習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();
}
}
}
}
//計算水印文本長度
//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實現(xiàn)給圖片打上水印的文章就介紹到這了,更多相關(guān)Java實現(xiàn)給圖片打上水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java基于數(shù)據(jù)庫實現(xiàn)全局唯一ID的示例
本文主要介紹了java基于數(shù)據(jù)庫實現(xiàn)全局唯一ID的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
java使用httpclient發(fā)送post請求示例
這篇文章主要介紹了java使用httpclient發(fā)送post請求示例,依賴JSON、HTTPClient等jar包,需要的朋友可以參考下2014-02-02

