JAVA圖片水印開(kāi)發(fā)案例詳解
寫(xiě)在最前面
上周零零碎碎花了一周的時(shí)間研究水印的開(kāi)發(fā),現(xiàn)在終于寫(xiě)了個(gè)入門(mén)級(jí)的Demo,做下筆記同時(shí)分享出來(lái)供大家參考。
Demo是在我上次寫(xiě)的 JAVA實(shí)用案例之文件導(dǎo)入導(dǎo)出(POI方式) 框架基礎(chǔ)上搭建的,基于Spring+SpringMVC。如果有錯(cuò)誤還請(qǐng)大家指正。
最后源碼地址在:https://github.com/allanzhuo/myport.git 。
簡(jiǎn)單介紹
水印開(kāi)發(fā)是web開(kāi)發(fā)中一種比較常見(jiàn)的功能,實(shí)現(xiàn)的代碼很簡(jiǎn)單,具體的實(shí)現(xiàn)步驟我也會(huì)以代碼為基礎(chǔ)詳細(xì)講述。其實(shí)以我個(gè)人的理解,我把水印的類(lèi)型和開(kāi)發(fā)流程分為以下幾種。
水印的類(lèi)型:
單文字水印
單圖片水印
多文字水印
多圖片水印
水印的開(kāi)發(fā)流程:
- 創(chuàng)建圖片緩存對(duì)象
- 創(chuàng)建Java繪圖工具對(duì)象
- 使用繪圖工具工具對(duì)象將原圖繪制到緩存圖片對(duì)象
- 使用繪圖工具對(duì)象將水?。ㄎ淖?圖片)繪制到緩存圖片
- 創(chuàng)建圖像編碼工具類(lèi)
- 使用圖像編碼工具類(lèi),輸出緩存圖像到目標(biāo)文件
效果圖:
上傳頁(yè):
原圖:
單文字水?。?/p>
單圖片水?。?/p>
多文字水?。?/p>
多圖片水?。?/p>
單文字水印開(kāi)發(fā)
所謂但文字水印,就是在一張圖片上添加一條文字水印。其中我們主要的流程是通過(guò)ImageIO工具類(lèi)解碼對(duì)應(yīng)的圖片,然后創(chuàng)建BufferImage對(duì)象,通過(guò)BufferImage對(duì)象創(chuàng)建Graphics2D對(duì)象,再通過(guò)Graphics2D對(duì)象繪制原圖到BufferImage對(duì)象。然后,我們還可以使用Graphics2D對(duì)象來(lái)設(shè)置水印的相關(guān)信息,如水印內(nèi)容、字體大小、字體風(fēng)格等。
這里需要說(shuō)明的是我們需要計(jì)算水印文本的寬度,中文長(zhǎng)度即文本寬度,英文長(zhǎng)度為文本寬度的二分之一。具體可以參考我源碼中的相關(guān)內(nèi)容。
//計(jì)算水印文本長(zhǎng)度 //1、中文長(zhǎng)度即文本長(zhǎng)度 2、英文長(zhǎng)度為文本長(zhǎng)度二分之一 public 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 String textWaterMark(MultipartFile myFile,String imageFileName) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 700; try { //使用ImageIO解碼圖片 Image image = ImageIO.read(myFile.getInputStream()); //計(jì)算原始圖片寬度長(zhǎng)度 int width = image.getWidth(null); int height = image.getHeight(null); //創(chuàng)建圖片緩存對(duì)象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //創(chuàng)建java繪圖工具對(duì)象 Graphics2D graphics2d = bufferedImage.createGraphics(); //參數(shù)主要是,原圖,坐標(biāo),寬高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //使用繪圖工具將水印繪制到圖片上 //計(jì)算文字水印寬高值 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT); int waterHeight = FONT_SIZE; //計(jì)算水印與原圖高寬差 int widthDiff = width-waterWidth; int heightDiff = height-waterHeight; //水印坐標(biāo)設(shè)置 if (X > widthDiff) { X = widthDiff; } if (Y > heightDiff) { Y = heightDiff; } //水印透明設(shè)置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); //縱坐標(biāo)在下方,不增加字體高度會(huì)靠上 graphics2d.drawString(MARK_TEXT, X, Y+FONT_SIZE); graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //創(chuàng)建圖像編碼工具類(lèi) JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用圖像編碼工具類(lèi),輸出緩存圖像到目標(biāo)文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; }
單圖片水印開(kāi)發(fā)
單圖片水印和上面單文字的代碼流程大致一致,這里只講解不同之處。
首先我們需要獲得水印圖片的路徑,然后創(chuàng)建水印文件對(duì)象,同樣通過(guò)ImageIO工具類(lèi)解碼水印圖片,中間我們就不需要計(jì)算文本長(zhǎng)寬了,因?yàn)閱挝淖种械拈L(zhǎng)寬即是我們水印圖片的長(zhǎng)寬。
//水印圖片路徑 //水印坐標(biāo)設(shè)置 String logoPath = "/img/logo.png"; String realPath = request.getSession().getServletContext().getRealPath(logoPath); File logo = new File(realPath); Image imageLogo = ImageIO.read(logo); int widthLogo = imageLogo.getWidth(null); int heightLogo = imageLogo.getHeight(null); int widthDiff = width-widthLogo; int heightDiff = height-heightLogo; //水印坐標(biāo)設(shè)置 if (X > widthDiff) { X = widthDiff; } if (Y > heightDiff) { Y = heightDiff; } //水印透明設(shè)置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.drawImage(imageLogo, X, Y, null);
多文字水印開(kāi)發(fā)
其實(shí)多文字水印開(kāi)發(fā)和單文字也是類(lèi)似的,主要的不同點(diǎn)是我們需要將BufferImage對(duì)象進(jìn)行旋轉(zhuǎn)。因?yàn)槔L制水印并不支持旋轉(zhuǎn)水印繪制,所以我們需要對(duì)原圖進(jìn)行旋轉(zhuǎn)繪制,然后通過(guò)循環(huán),我們就可以將一個(gè)文字水印多次繪制在原圖上了。
//旋轉(zhuǎn)原圖,注意旋轉(zhuǎn)角度為弧度制。后面兩個(gè)參數(shù)為旋轉(zhuǎn)的坐標(biāo)中心 graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawString(MARK_TEXT, x, y); y+=waterHeight+100; } x+=waterWidth+100; }
多圖片水印開(kāi)發(fā)
與上文相同,多圖片水印需要先讀取水印圖片,然后對(duì)水印設(shè)置透明度,在對(duì)原圖進(jìn)行旋轉(zhuǎn),然后通過(guò)循環(huán),我們就可以將一個(gè)圖片水印多次繪制在原圖上。
//水印圖片路徑 String logoPath = "/img/logo.png"; String realPath = request.getSession().getServletContext().getRealPath(logoPath); File logo = new File(realPath); Image imageLogo = ImageIO.read(logo); int widthLogo = imageLogo.getWidth(null); int heightLogo = imageLogo.getHeight(null); //水印透明設(shè)置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawImage(imageLogo, x, y, null); y+=heightLogo+100; } x+=widthLogo+100; }
業(yè)務(wù)類(lèi)完整代碼:
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.allan.service.WaterMarkService; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; @Service public class WaterMarkServiceImpl implements WaterMarkService{ //定義上傳的文件夾 private static final String UPLOAD_PATH = "E:/save"; //定義水印文字樣式 private static final String MARK_TEXT = "小賣(mài)鋪的老爺爺"; private static final String FONT_NAME = "微軟雅黑"; private static final int FONT_STYLE = Font.BOLD; private static final int FONT_SIZE = 60; private static final Color FONT_COLOR = Color.black; private static final float ALPHA = 0.3F; //1、上傳圖片 public String uploadImage(MultipartFile myFile,String imageFileName) { InputStream is =null; OutputStream os =null; try{ is = myFile.getInputStream(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); byte[] buffer =new byte[1024]; int len = 0; while ((len=is.read(buffer))>0){ os.write(buffer); } }catch(Exception e){ e.printStackTrace(); }finally{ if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e2) { e2.printStackTrace(); } } } return "success"; } //添加單條文字水印 public String textWaterMark(MultipartFile myFile,String imageFileName) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 700; try { Image image = ImageIO.read(myFile.getInputStream()); //計(jì)算原始圖片寬度長(zhǎng)度 int width = image.getWidth(null); int height = image.getHeight(null); //創(chuàng)建圖片緩存對(duì)象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //創(chuàng)建java繪圖工具對(duì)象 Graphics2D graphics2d = bufferedImage.createGraphics(); //參數(shù)主要是,原圖,坐標(biāo),寬高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //使用繪圖工具將水印繪制到圖片上 //計(jì)算文字水印寬高值 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT); int waterHeight = FONT_SIZE; //計(jì)算水印與原圖高寬差 int widthDiff = width-waterWidth; int heightDiff = height-waterHeight; //水印坐標(biāo)設(shè)置 if (X > widthDiff) { X = widthDiff; } if (Y > heightDiff) { Y = heightDiff; } //水印透明設(shè)置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.drawString(MARK_TEXT, X, Y+FONT_SIZE); graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //創(chuàng)建圖像編碼工具類(lèi) JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用圖像編碼工具類(lèi),輸出緩存圖像到目標(biāo)文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //添加單圖片水印 public String imageWaterMark(MultipartFile myFile,String imageFileName,HttpServletRequest request) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 763; try { Image image = ImageIO.read(myFile.getInputStream()); //計(jì)算原始圖片寬度長(zhǎng)度 int width = image.getWidth(null); int height = image.getHeight(null); //創(chuàng)建圖片緩存對(duì)象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //創(chuàng)建java繪圖工具對(duì)象 Graphics2D graphics2d = bufferedImage.createGraphics(); //參數(shù)主要是,原圖,坐標(biāo),寬高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //水印圖片路徑 String logoPath = "/img/logo.png"; String realPath = request.getSession().getServletContext().getRealPath(logoPath); File logo = new File(realPath); Image imageLogo = ImageIO.read(logo); int widthLogo = imageLogo.getWidth(null); int heightLogo = imageLogo.getHeight(null); int widthDiff = width-widthLogo; int heightDiff = height-heightLogo; //水印坐標(biāo)設(shè)置 if (X > widthDiff) { X = widthDiff; } if (Y > heightDiff) { Y = heightDiff; } //水印透明設(shè)置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.drawImage(imageLogo, X, Y, null); graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //創(chuàng)建圖像編碼工具類(lèi) JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用圖像編碼工具類(lèi),輸出緩存圖像到目標(biāo)文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //添加多條文字水印 public String moreTextWaterMark(MultipartFile myFile,String imageFileName) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 763; try { Image image = ImageIO.read(myFile.getInputStream()); //計(jì)算原始圖片寬度長(zhǎng)度 int width = image.getWidth(null); int height = image.getHeight(null); //創(chuàng)建圖片緩存對(duì)象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //創(chuàng)建java繪圖工具對(duì)象 Graphics2D graphics2d = bufferedImage.createGraphics(); //參數(shù)主要是,原圖,坐標(biāo),寬高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //使用繪圖工具將水印繪制到圖片上 //計(jì)算文字水印寬高值 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT); int waterHeight = FONT_SIZE; //水印透明設(shè)置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawString(MARK_TEXT, x, y); y+=waterHeight+100; } x+=waterWidth+100; } graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //創(chuàng)建圖像編碼工具類(lèi) JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用圖像編碼工具類(lèi),輸出緩存圖像到目標(biāo)文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //多圖片水印 public String moreImageWaterMark(MultipartFile myFile,String imageFileName,HttpServletRequest request) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 763; try { Image image = ImageIO.read(myFile.getInputStream()); //計(jì)算原始圖片寬度長(zhǎng)度 int width = image.getWidth(null); int height = image.getHeight(null); //創(chuàng)建圖片緩存對(duì)象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //創(chuàng)建java繪圖工具對(duì)象 Graphics2D graphics2d = bufferedImage.createGraphics(); //參數(shù)主要是,原圖,坐標(biāo),寬高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //水印圖片路徑 String logoPath = "/img/logo.png"; String realPath = request.getSession().getServletContext().getRealPath(logoPath); File logo = new File(realPath); Image imageLogo = ImageIO.read(logo); int widthLogo = imageLogo.getWidth(null); int heightLogo = imageLogo.getHeight(null); //水印透明設(shè)置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawImage(imageLogo, x, y, null); y+=heightLogo+100; } x+=widthLogo+100; } graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //創(chuàng)建圖像編碼工具類(lèi) JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用圖像編碼工具類(lèi),輸出緩存圖像到目標(biāo)文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //計(jì)算水印文本長(zhǎng)度 //1、中文長(zhǎng)度即文本長(zhǎng)度 2、英文長(zhǎng)度為文本長(zhǎng)度二分之一 public 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; } }
最后再說(shuō)明下,本Demo是在上次的文件導(dǎo)入導(dǎo)出的框架基礎(chǔ)上編寫(xiě)的,源碼中有些其它Demo的代碼,主要使用的類(lèi)有WaterMarkController.java、WaterMarkService.java、WaterMarkServiceImpl.java,因?yàn)榇a中我是硬編碼到E:/save文件夾下的,如果要運(yùn)行的話,還請(qǐng)先新建此文件夾,或者改為其他文件夾也行。
源碼地址:https://github.com/allanzhuo/myport.git
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Java類(lèi)的構(gòu)造方法詳解
這篇文章主要介紹了關(guān)于Java類(lèi)的構(gòu)造方法詳解的相關(guān)資料,需要的朋友可以參考下2023-01-01Spring Security如何基于Authentication獲取用戶信息
這篇文章主要介紹了Spring Security如何基于Authentication獲取用戶信息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Java中Comparable與Comparator的區(qū)別解析
這篇文章主要介紹了Java中Comparable與Comparator的區(qū)別解析,實(shí)現(xiàn)Comparable接口,重寫(xiě)compareTo方法,一般在實(shí)體類(lèi)定義的時(shí)候就可以選擇實(shí)現(xiàn)該接口,提供一個(gè)默認(rèn)的排序方式,供Arrays.sort和Collections.sort使用,需要的朋友可以參考下2024-01-01Java中TypeReference用法詳情說(shuō)明
這篇文章主要介紹了Java中TypeReference用法詳情說(shuō)明,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07Spring Boot Admin 動(dòng)態(tài)修改日志級(jí)別的方法步驟
這篇文章主要介紹了Spring Boot Admin 動(dòng)態(tài)修改日志級(jí)別的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java?C++題解leetcode764最大加號(hào)標(biāo)志示例
這篇文章主要為大家介紹了Java?C++題解leetcode764最大加號(hào)標(biāo)志示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01intellij idea使用git stash暫存一次提交的操作
這篇文章主要介紹了intellij idea使用git stash暫存一次提交的操作,具有很好的參考價(jià)值希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02Java字節(jié)碼操縱框架ASM圖文實(shí)例詳解
這篇文章主要為大家介紹了Java字節(jié)碼操縱框架ASM圖文實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07