Spring Boot實(shí)現(xiàn)圖片上傳/加水印一把梭操作實(shí)例代碼
概述
很多網(wǎng)站的圖片為了版權(quán)考慮都加有水印,尤其是那些圖片類網(wǎng)站。自己正好最近和圖片打交道比較多,因此就探索了一番基于 Spring Boot這把利器來實(shí)現(xiàn)從 圖片上傳 → 圖片加水印 的一把梭操作!
本文內(nèi)容腦圖如下:
本文內(nèi)容腦圖
搭建 Spring Boot基礎(chǔ)工程
過程不再贅述了,這里給出 pom中的關(guān)鍵依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> </dependencies>
編寫文件上傳服務(wù)
主要就是編寫 ImageUploadService 服務(wù)
里面僅一個(gè)上傳圖片的方法:uploadImage 方法
/** * 功能:上傳圖片 * @param file 文件 * @param uploadPath 服務(wù)器上上傳文件的路徑 * @param physicalUploadPath 服務(wù)器上上傳文件的物理路徑 * @return 上傳文件的 URL相對地址 */ public String uploadImage( MultipartFile file, String uploadPath, String physicalUploadPath ) { String filePath = physicalUploadPath + file.getOriginalFilename(); try { File targetFile=new File(filePath); FileUtils.writeByteArrayToFile(targetFile, file.getBytes()); } catch (IOException e) { e.printStackTrace(); } return uploadPath + "/" + file.getOriginalFilename(); } }
編寫圖片加水印服務(wù)
編寫 ImageWatermarkService 服務(wù)
里面就一個(gè)主要的 watermarkAdd方法,代碼后面寫有詳細(xì)解釋
@Service public class ImageWatermarkService { /** * imgFile 圖像文件 * imageFileName 圖像文件名 * uploadPath 服務(wù)器上上傳文件的相對路徑 * realUploadPath 服務(wù)器上上傳文件的物理路徑 */ public String watermarkAdd( File imgFile, String imageFileName, String uploadPath, String realUploadPath ) { String imgWithWatermarkFileName = "watermark_" + imageFileName; OutputStream os = null; try { Image image = ImageIO.read(imgFile); int width = image.getWidth(null); int height = image.getHeight(null); BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); // ① Graphics2D g = bufferedImage.createGraphics(); // ② g.drawImage(image, 0, 0, width,height,null); // ③ String logoPath = realUploadPath + "/" + Const.LOGO_FILE_NAME; // 水印圖片地址 File logo = new File(logoPath); // 讀取水印圖片 Image imageLogo = ImageIO.read(logo); int markWidth = imageLogo.getWidth(null); // 水印圖片的寬度和高度 int markHeight = imageLogo.getHeight(null); g.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Const.ALPHA) ); // 設(shè)置水印透明度 g.rotate(Math.toRadians(-10), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); // 設(shè)置水印圖片的旋轉(zhuǎn)度 int x = Const.X; int y = Const.Y; int xInterval = Const.X_INTERVAL; int yInterval = Const.Y_INTERVAL; double count = 1.5; while ( x < width*count ) { // 循環(huán)添加多個(gè)水印logo y = -height / 2; while( y < height*count ) { g.drawImage(imageLogo, x, y, null); // ④ y += markHeight + yInterval; } x += markWidth + xInterval; } g.dispose(); os = new FileOutputStream(realUploadPath + "/" + imgWithWatermarkFileName); JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); // ⑤ en.encode(bufferedImage); // ⑥ } catch (Exception e) { e.printStackTrace(); } finally { if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return uploadPath + "/" + imgWithWatermarkFileName; } }
代碼思路解釋如下:
可以對照代碼中的標(biāo)示數(shù)字和下面的解釋進(jìn)行理解:
① 創(chuàng)建緩存圖片
② 創(chuàng)建繪圖工具
③ 將原圖繪制到緩存圖片
④ 將水印logo繪制到緩存圖片
⑤ 創(chuàng)建圖像編碼工具類
⑥ 編碼緩存圖像生成目標(biāo)圖片
可見思路清晰易懂!
編寫 圖片上傳/處理 控制器
我們在該控制器代碼中將上述的 圖片上傳服務(wù) 和 圖片加水印服務(wù) 給用起來:
@RestController public class WatermarkController { @Autowired private ImageUploadService imageUploadService; @Autowired private ImageWatermarkService watermarkService; @RequestMapping(value = "/watermarktest", method = RequestMethod.POST) public ImageInfo watermarkTest( @RequestParam("file") MultipartFile image ) { ImageInfo imgInfo = new ImageInfo(); String uploadPath = "static/images/"; // 服務(wù)器上上傳文件的相對路徑 String physicalUploadPath = getClass().getClassLoader().getResource(uploadPath).getPath(); // 服務(wù)器上上傳文件的物理路徑 String imageURL = imageUploadService.uploadImage( image, uploadPath, physicalUploadPath ); File imageFile = new File(physicalUploadPath + image.getOriginalFilename() ); String watermarkAddImageURL = watermarkService.watermarkAdd(imageFile, image.getOriginalFilename(), uploadPath, physicalUploadPath); imgInfo.setImageUrl(imageURL); imgInfo.setLogoImageUrl(watermarkAddImageURL); return imgInfo; } }
實(shí)際實(shí)驗(yàn)與效果展示
我們用 Postman工具來輔助我們發(fā)出 localhost:9999/watermarktest 請求,進(jìn)行圖片上傳的操作:
Postman發(fā)請求進(jìn)行圖片上傳
之后我們再去項(xiàng)目的資源目錄下查看上傳的原圖 和 加完水印后圖片的效果如下:
原圖
加完水印后的圖片
喔唷,這水印 Logo是不是打的有點(diǎn)多…
不過這下終于不用害怕別人對您的圖片侵權(quán)啦 !
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Spring Boot實(shí)現(xiàn)圖片上傳功能
- spring boot 圖片上傳與顯示功能實(shí)例詳解
- spring boot實(shí)現(xiàn)上傳圖片并在頁面上顯示及遇到的問題小結(jié)
- SpringBoot限制文件或圖片上傳大小的兩種配置方法
- spring boot實(shí)現(xiàn)圖片上傳和下載功能
- spring boot thymeleaf 圖片上傳web項(xiàng)目根目錄操作步驟
- 基于SpringBoot實(shí)現(xiàn)圖片上傳與顯示
- bootstrap fileinput組件整合Springmvc上傳圖片到本地磁盤
- Spring boot的上傳圖片功能實(shí)例詳解
相關(guān)文章
Java實(shí)現(xiàn)監(jiān)聽文件變化的三種方案詳解
這篇文章主要介紹了Java實(shí)現(xiàn)監(jiān)聽文件變化的三種方法,每種方案給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05淺談Java內(nèi)存區(qū)域與對象創(chuàng)建過程
下面小編就為大家?guī)硪黄獪\談Java內(nèi)存區(qū)域與對象創(chuàng)建過程。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07springboot 實(shí)現(xiàn)Http接口加簽、驗(yàn)簽操作方法
這篇文章主要介紹了springboot 實(shí)現(xiàn)Http接口加簽、驗(yàn)簽操作,服務(wù)之間接口調(diào)用,通過簽名作為安全認(rèn)證來保證API的安全性,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09Java中消息隊(duì)列任務(wù)的平滑關(guān)閉詳解
對于消息隊(duì)列的監(jiān)聽,我們一般使用Java寫一個(gè)獨(dú)立的程序,在Linux服務(wù)器上運(yùn)行。程序啟動后,通過消息隊(duì)列客戶端接收消息,放入一個(gè)線程池進(jìn)行異步處理,并發(fā)的快速處理。這篇文章主要給大家介紹了關(guān)于Java中消息隊(duì)列任務(wù)的平滑關(guān)閉的相關(guān)資料,需要的朋友可以參考下。2017-11-11Java實(shí)現(xiàn)將PDF轉(zhuǎn)為PDF/A
通過將PDF格式轉(zhuǎn)換為PDF/A格式,可保護(hù)文檔布局、格式、字體、大小等不受更改,從而實(shí)現(xiàn)文檔安全保護(hù)的目的,同時(shí)又能保證文檔可讀、可訪問。本文將為大家介紹如何實(shí)現(xiàn)這一轉(zhuǎn)換,需要的可以參考一下2022-01-01JavaWeb連接數(shù)據(jù)庫MySQL的操作技巧
數(shù)據(jù)庫是編程中重要的一部分,它囊括了數(shù)據(jù)操作,數(shù)據(jù)持久化等各方面。在每一門編程語言中都占有相當(dāng)大的比例。本次,小編以MySQL為例,使用mvc編程思想,給大家講解下javaweb對數(shù)據(jù)庫的操作2017-02-02