|
|
@@ -1,5 +1,8 @@
|
|
|
package com.zhixinghe1.ots.service.manager.impl;
|
|
|
|
|
|
+import com.zhixinghe1.ots.atomic.service.IAttachmentService;
|
|
|
+import com.zhixinghe1.ots.common.exception.BusinessException;
|
|
|
+import com.zhixinghe1.ots.domain.dto.AttachmentDto;
|
|
|
import com.zhixinghe1.ots.domain.dto.WaterMarkLayout;
|
|
|
import com.zhixinghe1.ots.domain.dto.WaterMarkRequest;
|
|
|
import com.zhixinghe1.ots.domain.dto.WaterMarkStyle;
|
|
|
@@ -7,15 +10,17 @@ import com.zhixinghe1.ots.service.constant.PositionEnum;
|
|
|
import com.zhixinghe1.ots.service.core.TileImageFilter;
|
|
|
import com.zhixinghe1.ots.service.manager.IWaterMarkManager;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import java.awt.Color;
|
|
|
+import java.awt.Font;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import net.coobird.thumbnailator.Thumbnails;
|
|
|
import net.coobird.thumbnailator.filters.Caption;
|
|
|
import net.coobird.thumbnailator.filters.ImageFilter;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
-import java.awt.*;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
@@ -26,34 +31,58 @@ import java.util.Optional;
|
|
|
@Service
|
|
|
public class WaterMarkManagerImpl implements IWaterMarkManager {
|
|
|
|
|
|
- @Override
|
|
|
- public void make(WaterMarkRequest request) throws UnsupportedEncodingException {
|
|
|
- WaterMarkStyle style = request.getWaterMarkStyle();
|
|
|
- WaterMarkLayout layout = request.getWaterMarkLayout();
|
|
|
- Font font = new Font(style.getFont().getName(), style.getFontStyle().getCode(), style.getSize());
|
|
|
-
|
|
|
- ImageFilter imageFilter = null;
|
|
|
- if (PositionEnum.TILE.equals(layout.getPosition())) {
|
|
|
- imageFilter = new TileImageFilter(request.getContent(), font, Color.WHITE, style.getAlpha(), layout.getDegree(),
|
|
|
- layout.getInterval(), layout.getInterval(), layout.getCriscross());
|
|
|
- } else {
|
|
|
- imageFilter = new Caption(request.getContent(), font, Color.BLUE, style.getAlpha(), layout.getPosition().getPosition(), 5);
|
|
|
- }
|
|
|
-
|
|
|
- HttpServletResponse response = Optional.ofNullable(
|
|
|
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse())
|
|
|
- .orElseThrow();
|
|
|
- response.setContentType("application/force-download");
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("测试123.jpg", "UTF-8"));
|
|
|
-
|
|
|
- try (OutputStream outputStream = response.getOutputStream()) {
|
|
|
- Thumbnails.of("C:\\Users\\tyuio\\Desktop\\捕获(1).jpg")
|
|
|
- .scale(1, 1)
|
|
|
- .addFilter(imageFilter)
|
|
|
- .toOutputStream(outputStream);
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("导出水印时发生异常", e);
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private IAttachmentService attachmentService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void make(WaterMarkRequest request) throws UnsupportedEncodingException {
|
|
|
+ AttachmentDto attachmentDto = attachmentService.getById(request.getAttachmentId());
|
|
|
+ if (attachmentDto == null) {
|
|
|
+ throw BusinessException.fail(String.format("无效的附件:%d", request.getAttachmentId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpServletResponse response = Optional.ofNullable(
|
|
|
+ ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse())
|
|
|
+ .orElseThrow();
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setHeader("Content-Disposition",
|
|
|
+ "attachment;fileName=" + URLEncoder.encode(attachmentDto.getName(), "UTF-8"));
|
|
|
+
|
|
|
+ try (OutputStream outputStream = response.getOutputStream()) {
|
|
|
+ makeWaterMark(outputStream, request, attachmentDto);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("导出水印时发生异常", e);
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加水印
|
|
|
+ * @param outputStream
|
|
|
+ * @param request
|
|
|
+ * @param attachmentDto
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private void makeWaterMark(OutputStream outputStream, WaterMarkRequest request, AttachmentDto attachmentDto)
|
|
|
+ throws IOException {
|
|
|
+ WaterMarkStyle style = request.getWaterMarkStyle();
|
|
|
+ WaterMarkLayout layout = request.getWaterMarkLayout();
|
|
|
+ Font font = new Font(style.getFont().getName(), style.getFontStyle().getCode(),
|
|
|
+ style.getSize());
|
|
|
+
|
|
|
+ ImageFilter imageFilter = null;
|
|
|
+ if (PositionEnum.TILE.equals(layout.getPosition())) {
|
|
|
+ imageFilter = new TileImageFilter(request.getContent(), font, Color.WHITE, style.getAlpha(),
|
|
|
+ layout.getDegree(),
|
|
|
+ layout.getInterval(), layout.getInterval(), layout.getCriscross());
|
|
|
+ } else {
|
|
|
+ imageFilter = new Caption(request.getContent(), font, Color.BLUE, style.getAlpha(),
|
|
|
+ layout.getPosition().getPosition(), 5);
|
|
|
+ }
|
|
|
+
|
|
|
+ Thumbnails.of(attachmentDto.getPath())
|
|
|
+ .scale(2, 2)
|
|
|
+ .addFilter(imageFilter)
|
|
|
+ .toOutputStream(outputStream);
|
|
|
+ }
|
|
|
}
|