最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot實現(xiàn)圖片上傳/加水印一把梭操作實例代碼

 更新時間:2018年11月15日 09:00:26   作者:王 帥  
這篇文章主要給大家介紹了關(guān)于Spring Boot實現(xiàn)圖片上傳/加水印一把梭操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

概述

很多網(wǎng)站的圖片為了版權(quán)考慮都加有水印,尤其是那些圖片類網(wǎng)站。自己正好最近和圖片打交道比較多,因此就探索了一番基于 Spring Boot這把利器來實現(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ù)

里面僅一個上傳圖片的方法: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ù)

里面就一個主要的 watermarkAdd方法,代碼后面寫有詳細解釋

@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)添加多個水印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;
}

}

代碼思路解釋如下:

可以對照代碼中的標示數(shù)字和下面的解釋進行理解:

① 創(chuàng)建緩存圖片

② 創(chuàng)建繪圖工具

③ 將原圖繪制到緩存圖片

④ 將水印logo繪制到緩存圖片

⑤ 創(chuàng)建圖像編碼工具類

⑥ 編碼緩存圖像生成目標圖片

可見思路清晰易懂!

編寫 圖片上傳/處理 控制器

我們在該控制器代碼中將上述的 圖片上傳服務(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;
}
}

實際實驗與效果展示

我們用 Postman工具來輔助我們發(fā)出 localhost:9999/watermarktest 請求,進行圖片上傳的操作:


Postman發(fā)請求進行圖片上傳

之后我們再去項目的資源目錄下查看上傳的原圖 和 加完水印后圖片的效果如下:

原圖


加完水印后的圖片

喔唷,這水印 Logo是不是打的有點多…

不過這下終于不用害怕別人對您的圖片侵權(quán)啦 !

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論

桂平市| 榆林市| 奈曼旗| 资源县| 集安市| 潮安县| 东阳市| 商城县| 清水河县| 湘乡市| 西藏| 从江县| 舞钢市| 甘谷县| 毕节市| 金寨县| 二手房| 本溪市| 霍城县| 娱乐| 涞源县| 塔河县| 泗水县| 壤塘县| 化州市| 永川市| 名山县| 周口市| 大姚县| 婺源县| 洞头县| 呼伦贝尔市| 明星| 尼玛县| 威海市| 安岳县| 镇赉县| 汕头市| 中西区| 长兴县| 阿拉尔市|