一、JAVA圖片水印實現原理
1.1、JAVA圖片水印實現思路
1、創建緩存圖片對象。
2、創建Java繪圖工具對象。
3、使用繪圖工具對象將原圖繪製到緩存圖片對象。
4、使用繪圖工具將水印(文字/圖片)繪製到緩存圖片對象。
5、創建圖像編碼工具類。
6、使用圖像編碼工具類,輸出緩存圖像到目標圖片文件。
1.2、JAVA圖片水印使用工具類
1、BufferedImage 把對象儲存到緩存中,提高運行效率。
2、Graohics2D 對對象進行操作。
3、JPEGImageEncode 對對象進行編碼,把內存中的對象刻錄到我們的磁碟上。
二、實現圖片添加單個文字水印
/*
* 給圖片添加單個文字水印類
* */
publicclassTextWatermarking {
//定義圖片水印字體類型
publicstaticfinalString FONT_NAME = "微軟雅黑";
//定義圖片水印字體加粗、變細、傾斜等樣式
publicstaticfinalintFONT_STYLE= Font.BOLD;
//設置字體大小
publicstaticfinalintFONT_SIZE= 120;
//設置文字透明程度
publicstaticfloatALPHA= 0.3F;
publicstaticfinalintX=10;
publicstaticfinalintY=10;
/**
* 給圖片添加單個文字水印、可設置水印文字旋轉角度
* source 需要添加水印的圖片路徑(如:F:/images/6.jpg)
* outPut 添加水印後圖片輸出路徑(如:F:/images/)
* imageName 圖片名稱
* imageType 圖片類型
* color 水印文字的顏色
* word 水印文字
* degree 水印文字旋轉角度,為null表示不旋轉
*/
publicBoolean markImageBySingleText(String sourcePath, String outputPath, String imageName, String imageType, Color color, String word, Integer degree) {
try{
//讀取原圖片信息
File file = new File(sourcePath);
if(!file.isFile()) {
returnfalse;
}
//獲取源圖像的寬度、高度
Image image = ImageIO.read(file);
intwidth = image.getWidth(null);
intheight = image.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//創建繪圖工具對象
Graphics2D graphics2D = bufferedImage.createGraphics();
//其中的0代表和原圖位置一樣
graphics2D.drawImage(image, 0, 0, width, height, null);
//設置水印文字(設置水印字體樣式、粗細、大小)
graphics2D.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
//設置水印顏色
graphics2D.setColor(color);
//設置水印透明度
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPHA));
//設置水印旋轉
if(null != degree) {
graphics2D.rotate(Math.toRadians(degree),(double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
}
intwidth1=FONT_SIZE*getTextLength(word);
intheight1=FONT_SIZE;
intwidthDiff=width-width1;
intheightDiff=height-height1;
intx=X;
inty=Y;
if(x>widthDiff)
{
x=widthDiff;
}
if(y>heightDiff)
{
y=heightDiff;
}
//進行繪製
graphics2D.drawString(word, x, y+120);
graphics2D.dispose();
//輸出圖片
File sf = new File(outputPath, imageName+"."+imageType);
// 保存圖片
ImageIO.write(bufferedImage, imageType, sf);
} catch (Exception e) {
e.printStackTrace();
}
returntrue;
}
/**
* 如果水印內容超出判斷
*/
publicintgetTextLength(String text)
{
intlength=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;
returnlength;
}
}
三、實現圖片添加單個圖片水印
/*
* 給圖片添加單個圖片水印、可設置水印圖片旋轉角度
* */
publicclassImageWatermark {
publicstaticfinalintX=10;
publicstaticfinalintY=10;
/**
*icon 水印圖片路徑(如:F:/images/icon.png)
*source 沒有加水印的圖片路徑(如:F:/images/6.jpg)
*output 加水印後的圖片路徑(如:F:/images/)
*imageName 圖片名稱(如:11111)
*imageType 圖片類型(如:jpg)
*degree 水印圖片旋轉角度,為null表示不旋轉
*/
publicBoolean markImageBySingleIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {
try{
File file = new File(source);
File ficon = new File(icon);
if(!file.isFile()) {
returnfalse;
}
//將icon加載到內存中
Image ic = ImageIO.read(ficon);
//icon高度
inticheight= ic.getHeight(null);
//將源圖片讀到內存中
Image img = ImageIO.read(file);
//圖片寬
intwidth = img.getWidth(null);
//圖片高
intheight = img.getHeight(null);
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//創建一個指定 BufferedImage 的 Graphics2D 對象
Graphics2D g = bi.createGraphics();
//設置對線段的鋸齒狀邊緣處理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//呈現一個圖像,在繪製前進行從圖像空間到用戶空間的轉換
g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
if(null != degree) {
//設置水印旋轉
g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
}
//水印圖象的路徑 水印一般為gif或者png的,這樣可設置透明度
ImageIcon imgIcon = new ImageIcon(icon);
//得到Image對象。
Image con = imgIcon.getImage();
intwidth1=con.getWidth(null);
intheight1=con.getHeight(null);
intwidthDiff=width-width1;
intheightDiff=height-height1;
intx=X;
inty=Y;
if(x>widthDiff)
{
x=widthDiff;
}
if(y>heightDiff)
{
y=heightDiff;
}
//透明度,最小值為0,最大值為1
floatclarity = 0.6f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));
//表示水印圖片的坐標位置(x,y)
g.drawImage(con, x, y, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
File sf = new File(output, imageName+"."+imageType);
ImageIO.write(bi, imageType, sf); // 保存圖片
} catch (Exception e) {
e.printStackTrace();
}
returntrue;
}
四、實現圖片添加多個文字水印
/*
* 給圖片添加多個文字水印類
* */
publicclassTextWatermarking {
// 定義圖片水印字體類型
publicstaticfinalString FONT_NAME = "微軟雅黑";
// 定義圖片水印字體加粗、變細、傾斜等樣式
publicstaticfinalintFONT_STYLE= Font.BOLD;
// 設置字體大小
publicstaticfinalintFONT_SIZE= 120;
// 設置文字透明程度
publicstaticfloatALPHA= 0.3F;
/**
* 給圖片添加多個文字水印、可設置水印文字旋轉角度 source 需要添加水印的圖片路徑 outPut 添加水印後圖片輸出路徑 imageName 圖片名稱
* imageType 圖片類型 color 水印文字的顏色 word 水印文字 degree 水印文字旋轉角度,為null表示不旋轉
*/
publicBoolean markImageByMoreText(String sourcePath, String outputPath, String imageName, String imageType,
Color color, String word, Integer degree) {
try{
// 讀取原圖片信息
File file = newFile(sourcePath);
if(!file.isFile()) {
returnfalse;
}
// 獲取源圖像的寬度、高度
Image image = ImageIO.read(file);
intwidth = image.getWidth(null);
intheight = image.getHeight(null);
BufferedImage bufferedImage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 創建繪圖工具對象
Graphics2D graphics2D = bufferedImage.createGraphics();
// 其中的0代表和原圖位置一樣
graphics2D.drawImage(image, 0, 0, width, height, null);
// 設置水印文字(設置水印字體樣式、粗細、大小)
graphics2D.setFont(newFont(FONT_NAME, FONT_STYLE, FONT_SIZE));
// 設置水印顏色
graphics2D.setColor(color);
// 設置水印透明度
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
// 設置水印旋轉
if(null != degree) {
graphics2D.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2,
(double) bufferedImage.getHeight() / 2);
}
intwidth1 = FONT_SIZE * getTextLength(word);
intheight1 = FONT_SIZE;
intx = -width / 2;
inty = -height / 2;
while(x < width * 1.5) {
y = -height / 2;
while(y < height * 1.5) {
graphics2D.drawString(word, x, y);
y += height1 + 300;
}
x += width1 + 300;
}
graphics2D.dispose();
// 輸出圖片
File sf = newFile(outputPath, imageName + "." + imageType);
// 保存圖片
ImageIO.write(bufferedImage, imageType, sf);
} catch(Exception e) {
e.printStackTrace();
}
returntrue;
}
/**
* 如果水印內容超出判斷
*/
publicintgetTextLength(String text) {
intlength = 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;
returnlength;
}
}
五、JAVA實現添加多個圖片水印
publicclassMoreImageWatermark {
// 設置文字透明程度
publicstaticfloatALPHA= 0.3F;
/**
* 給圖片添加多個圖片水印、可設置水印圖片旋轉角度 logoPath 水印圖片的路徑 source 需要添加水印的圖片路徑 outPut
* 添加水印後圖片輸出路徑 imageName 圖片名稱 imageType 圖片類型 degree 水印文字旋轉角度,為null表示不旋轉
*/
publicBoolean markImageByMoreImage(String logoPath, String sourcePath, String outputPath, String imageName,
String imageType, Integer degree) {
try{
// 讀取原圖片信息
File file = newFile(sourcePath);
if(!file.isFile()) {
returnfalse;
}
// 獲取源圖像的寬度、高度
Image image = ImageIO.read(file);
intwidth = image.getWidth(null);
intheight = image.getHeight(null);
BufferedImage bufferedImage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 創建繪圖工具對象
Graphics2D graphics2D = bufferedImage.createGraphics();
// 其中的0代表和原圖位置一樣
graphics2D.drawImage(image, 0, 0, width, height, null);
// 設置水印透明度
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
// 設置水印旋轉
if(null != degree) {
graphics2D.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2,
(double) bufferedImage.getHeight() / 2);
}
File logo = newFile(logoPath);
Image logoimage = ImageIO.read(logo);
intwidth1 = logoimage.getWidth(null);
intheight1 = logoimage.getHeight(null);
intx = -width / 2;
inty = -height / 2;
while(x < width * 1.5) {
y = -height / 2;
while(y < height * 1.5) {
graphics2D.drawImage(logoimage, x, y, null);
y += height1 + 300;
}
x += width1 + 300;
}
graphics2D.dispose();
// 輸出圖片
File sf = newFile(outputPath, imageName + "." + imageType);
// 保存圖片
ImageIO.write(bufferedImage, imageType, sf);
} catch(Exception e) {
e.printStackTrace();
}
returntrue;
}
}