100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > springboot实现上传图片添加水印

springboot实现上传图片添加水印

时间:2023-01-25 08:36:45

相关推荐

springboot实现上传图片添加水印

springboot实现上传图片添加水印

1.代码实现:

添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

后端代码

@Controllerpublic class TestController {@Autowiredprivate ImageUploadUtil imageUploadUtil;/*** 跳转到文件上传页面* @return*/@RequestMapping("/index")public String index(){return "index";}@RequestMapping("/show")public String show(ModelMap modelMap, String oldPath, String newPath){modelMap.put("oldPath", oldPath);modelMap.put("newPath", newPath);return "index";}@RequestMapping(value = "/watermark", method = RequestMethod.POST)public String watermark(@RequestParam("fileName") MultipartFile file) {String imageURL = imageUploadUtil.uploadImage(file);File imageFile = new File(imageURL);String watermarkAddImageURL = "";if (imageFile.exists()) {watermarkAddImageURL = imageUploadUtil.watermarkAdd(imageFile, file.getOriginalFilename());}return "forward:/show?oldPath=" + imageURL + "&newPath=" + watermarkAddImageURL;}@GetMapping("/down")public void down(String path, HttpServletResponse response,HttpServletRequest request) {try {InputStream input = new FileInputStream(path);// 获取绑定了客户端的流ServletOutputStream output = response.getOutputStream();// 把输入流中的数据写入到输出流中IOUtils.copy(input,output);input.close();output.close();} catch (Exception e) {e.printStackTrace();}}}

图片工具类

@Componentpublic class ImageUploadUtil {@Value("${file.upload.path}")private String uploadPath;@Value("${file.logo.path}")public String logoPath;public String uploadImage(MultipartFile file) {String filePath = uploadPath + file.getOriginalFilename();try {File targetFile=new File(filePath);FileUtils.writeByteArrayToFile(targetFile, file.getBytes());} catch (IOException e) {e.printStackTrace();}return filePath;}public String watermarkAdd( File imgFile, String imageFileName) {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); // ③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) ); // 设置水印透明度g.rotate(Math.toRadians(-10), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); // 设置水印图片的旋转度int x = Const.X;int y = Const.Y;int xInterval = Const.X_INTERVAL;int yInterval = Const.Y_INTERVAL;//添加单个水印double x2 = width * 0.9 - markWidth;double y2 = height * 0.9 - markHeight;g.drawImage(imageLogo, new Double(x2).intValue(), new Double(y2).intValue(), null); // ④// 循环添加多个水印logo/* double count = 1.5;while ( x < width*count ) {y = -height / 2;while( y < height*count ) {g.drawImage(imageLogo, x, y, null); // ④y += markHeight + yInterval;}x += markWidth + xInterval;}*/g.dispose();os = new FileOutputStream(uploadPath + 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;}}

前端页面

<!DOCTYPE html><html xmlns:th=""><head><meta charset="UTF-8"><title>标题</title></head><body><form action="/watermark" method="post" enctype="multipart/form-data"><p>选择文件: <input type="file" name="fileName"/></p><p><input type="submit" value="提交"/></p></form><img th:src="@{/down(path='D:\test\5.jpg')}" width="500" height="300"></br><div th:if="${newPath}"><p>上传的图片</p><img th:src="@{/down(path=${oldPath})}" width="500" height="300"><p>带水印的图片</p><img th:src="@{/down(path=${newPath})}" width="500" height="300"></div></body></html>

2.实现效果:

浏览器访问:​​http://localhost:8001/index​

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。