|
|
@@ -0,0 +1,97 @@
|
|
|
+package com.crm.manager.util;
|
|
|
+
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.awt.image.ConvolveOp;
|
|
|
+import java.awt.image.Kernel;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class ImageSharpTool {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义卷积核锐化
|
|
|
+ */
|
|
|
+ public byte[] sharpImage(MultipartFile file) throws IOException {
|
|
|
+ byte[] fileBytes = file.getBytes();
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes);
|
|
|
+ BufferedImage sourceImg = ImageIO.read(inputStream);
|
|
|
+ if (sourceImg == null) {
|
|
|
+ throw new RuntimeException("不支持的图片格式,请上传jpg/png");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 锐化卷积核 强度可调
|
|
|
+ float[] sharpKernel = {
|
|
|
+ 0, -0.3f, 0,
|
|
|
+ -0.3f, 2.2f, -0.3f,
|
|
|
+ 0, -0.3f, 0
|
|
|
+ };
|
|
|
+ Kernel kernel = new Kernel(3, 3, sharpKernel);
|
|
|
+ ConvolveOp convolveOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
|
|
|
+
|
|
|
+ // 创建画布渲染锐化后图片
|
|
|
+ BufferedImage sharpImg = new BufferedImage(sourceImg.getWidth(), sourceImg.getHeight(), sourceImg.getType());
|
|
|
+ Graphics2D g2d = sharpImg.createGraphics();
|
|
|
+ try {
|
|
|
+ g2d.drawImage(sourceImg, convolveOp, 0, 0);
|
|
|
+ } finally {
|
|
|
+ g2d.dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 输出jpg字节流
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ Thumbnails.of(sharpImg)
|
|
|
+ .scale(1.0)
|
|
|
+ .outputQuality(0.92)
|
|
|
+ .outputFormat("jpg")
|
|
|
+ .toOutputStream(outputStream);
|
|
|
+
|
|
|
+ return outputStream.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 动态调节锐化强度
|
|
|
+ * @param sharpFactor 强度系数 1.0~3.0
|
|
|
+ */
|
|
|
+ public byte[] sharpImage(MultipartFile file, float sharpFactor) throws IOException {
|
|
|
+ byte[] fileBytes = file.getBytes();
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes);
|
|
|
+ BufferedImage sourceImg = ImageIO.read(inputStream);
|
|
|
+ if (sourceImg == null) {
|
|
|
+ throw new RuntimeException("不支持的图片格式,请上传jpg/png");
|
|
|
+ }
|
|
|
+
|
|
|
+ float val = (sharpFactor - 1f) * 0.3f;
|
|
|
+ float[] sharpKernel = {
|
|
|
+ 0, -val, 0,
|
|
|
+ -val, 1 + 2*val, -val,
|
|
|
+ 0, -val, 0
|
|
|
+ };
|
|
|
+ Kernel kernel = new Kernel(3, 3, sharpKernel);
|
|
|
+ ConvolveOp convolveOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
|
|
|
+
|
|
|
+ BufferedImage sharpImg = new BufferedImage(sourceImg.getWidth(), sourceImg.getHeight(), sourceImg.getType());
|
|
|
+ Graphics2D g2d = sharpImg.createGraphics();
|
|
|
+ try {
|
|
|
+ g2d.drawImage(sourceImg, convolveOp, 0, 0);
|
|
|
+ } finally {
|
|
|
+ g2d.dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ Thumbnails.of(sharpImg)
|
|
|
+ .scale(1.0)
|
|
|
+ .outputQuality(0.92)
|
|
|
+ .outputFormat("jpg")
|
|
|
+ .toOutputStream(outputStream);
|
|
|
+
|
|
|
+ return outputStream.toByteArray();
|
|
|
+ }
|
|
|
+}
|