xiongzhu 4 years ago
parent
commit
509014a986

+ 23 - 16
src/main/java/com/izouma/nineth/utils/ImageUtils.java

@@ -12,33 +12,40 @@ import java.io.InputStream;
 
 public class ImageUtils {
 
-    public static BufferedImage resizeJpg(InputStream image, int width, int height) throws IOException {
+    public static BufferedImage resizeJpg(InputStream image, int width, int height, boolean compress) throws IOException {
         BufferedImage bufferedImage = ImageIO.read(image);
+        return resizeJpg(bufferedImage, width, height, compress);
+    }
+
+    public static BufferedImage resizeJpg(BufferedImage bufferedImage, int width, int height, boolean compress) throws IOException {
         return Thumbnails.of(bufferedImage)
                 .size(Math.min(width, bufferedImage.getWidth()), Math.min(height, bufferedImage.getHeight()))
                 .outputFormat("jpg")
+                .outputQuality(compress ? 0.7 : 1.0)
                 .asBufferedImage();
     }
 
     public static BufferedImage resizePng(InputStream image, int width, int height, boolean compress) throws IOException {
         BufferedImage bufferedImage = ImageIO.read(image);
-        BufferedImage img = Thumbnails.of(bufferedImage)
-                .size(Math.min(width, bufferedImage.getWidth()), Math.min(height, bufferedImage.getHeight()))
-                .outputFormat("png")
-                .asBufferedImage();
-        bufferedImage.flush();
-        return img;
+        return resizePng(bufferedImage, width, height, compress);
     }
 
-    public static BufferedImage resizePng1(InputStream image, int width, int height, boolean compress) throws IOException {
-        BufferedImage bufferedImage = ImageIO.read(image);
-        BufferedImage img = Thumbnails.of(bufferedImage)
-                .size(Math.min(width, bufferedImage.getWidth()), Math.min(height, bufferedImage.getHeight()))
-                .outputFormat("png")
-                .asBufferedImage();
-        bufferedImage.flush();
-        PngQuant pngQuant = new PngQuant();
-        return pngQuant.getRemapped(img);
+    public static BufferedImage resizePng(BufferedImage bufferedImage, int width, int height, boolean compress) throws IOException {
+        if (compress) {
+            BufferedImage img = Thumbnails.of(bufferedImage)
+                    .size(Math.min(width, bufferedImage.getWidth()), Math.min(height, bufferedImage.getHeight()))
+                    .outputFormat("png")
+                    .outputQuality(1.0)
+                    .asBufferedImage();
+            PngQuant pngQuant = new PngQuant();
+            return pngQuant.getRemapped(img);
+        } else {
+            return Thumbnails.of(bufferedImage)
+                    .size(Math.min(width, bufferedImage.getWidth()), Math.min(height, bufferedImage.getHeight()))
+                    .outputFormat("png")
+                    .outputQuality(1.0)
+                    .asBufferedImage();
+        }
     }
 
     public static void main(String[] args) throws IOException {

+ 24 - 112
src/main/java/com/izouma/nineth/web/FileUploadController.java

@@ -57,7 +57,7 @@ public class FileUploadController {
                     + "." + ext;
         }
 
-        if (Pattern.matches("(jpg|png)", ext) && compress) {
+        if (Pattern.matches("(jpg|png)", ext) && (compress || width != null || height != null)) {
             if (width == null && height == null) {
                 width = Integer.MAX_VALUE;
                 height = Integer.MAX_VALUE;
@@ -68,88 +68,16 @@ public class FileUploadController {
             }
             BufferedImage img = null;
             if ("jpg".equals(ext)) {
-                img = ImageUtils.resizeJpg(file.getInputStream(), width, height);
+                img = ImageUtils.resizeJpg(file.getInputStream(), width, height, compress);
             } else if ("png".equals(ext)) {
-                img = ImageUtils.resizePng(file.getInputStream(), width, height, true);
-            }
-            ByteArrayOutputStream os = new ByteArrayOutputStream();
-            ImageIO.write(img, ext, os);
-            InputStream is = new ByteArrayInputStream(os.toByteArray());
-            return storageService.uploadFromInputStream(is, path);
-        } else if (width != null || height != null) {
-            if (height == null) {
-                height = Integer.MAX_VALUE;
-            } else if (width == null) {
-                width = Integer.MAX_VALUE;
-            }
-            BufferedImage img = null;
-            if ("jpg".equals(ext)) {
-                img = ImageUtils.resizeJpg(file.getInputStream(), width, height);
-            } else if ("png".equals(ext)) {
-                img = ImageUtils.resizePng(file.getInputStream(), width, height, false);
+                img = ImageUtils.resizePng(file.getInputStream(), width, height, compress);
             }
+            Objects.requireNonNull(img, "图片压缩失败");
             ByteArrayOutputStream os = new ByteArrayOutputStream();
             ImageIO.write(img, ext, os);
             InputStream is = new ByteArrayInputStream(os.toByteArray());
             return storageService.uploadFromInputStream(is, path);
         }
-
-        return storageService.uploadFromInputStream(file.getInputStream(), path);
-    }
-
-    @PostMapping("/file1")
-    public String uploadFile1(@RequestParam("file") MultipartFile file,
-                             @RequestParam(value = "path", required = false) String path,
-                             @RequestParam(value = "compress", defaultValue = "false") boolean compress,
-                             @RequestParam(value = "width", required = false) Integer width,
-                             @RequestParam(value = "height", required = false) Integer height) throws IOException {
-
-        String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("")
-                .toLowerCase().replace("jpeg", "jpg");
-        if (path == null) {
-            String basePath = Optional.ofNullable(file.getContentType()).orElse("application").split("/")[0];
-            path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
-                    + RandomStringUtils.randomAlphabetic(8)
-                    + "." + ext;
-        }
-
-        if (Pattern.matches("(jpg|png)", ext) && compress) {
-            if (width == null && height == null) {
-                width = Integer.MAX_VALUE;
-                height = Integer.MAX_VALUE;
-            } else if (height == null) {
-                height = Integer.MAX_VALUE;
-            } else if (width == null) {
-                width = Integer.MAX_VALUE;
-            }
-            BufferedImage img = null;
-            if ("jpg".equals(ext)) {
-                img = ImageUtils.resizeJpg(file.getInputStream(), width, height);
-            } else if ("png".equals(ext)) {
-                img = ImageUtils.resizePng1(file.getInputStream(), width, height, true);
-            }
-            ByteArrayOutputStream os = new ByteArrayOutputStream();
-            ImageIO.write(img, ext, os);
-            InputStream is = new ByteArrayInputStream(os.toByteArray());
-            return storageService.uploadFromInputStream(is, path);
-        } else if (width != null || height != null) {
-            if (height == null) {
-                height = Integer.MAX_VALUE;
-            } else if (width == null) {
-                width = Integer.MAX_VALUE;
-            }
-            BufferedImage img = null;
-            if ("jpg".equals(ext)) {
-                img = ImageUtils.resizeJpg(file.getInputStream(), width, height);
-            } else if ("png".equals(ext)) {
-                img = ImageUtils.resizePng1(file.getInputStream(), width, height, false);
-            }
-            ByteArrayOutputStream os = new ByteArrayOutputStream();
-            ImageIO.write(img, ext, os);
-            InputStream is = new ByteArrayInputStream(os.toByteArray());
-            return storageService.uploadFromInputStream(is, path);
-        }
-
         return storageService.uploadFromInputStream(file.getInputStream(), path);
     }
 
@@ -198,7 +126,7 @@ public class FileUploadController {
         String url;
 
 
-        if (Pattern.matches("(jpg|png)", ext) && compress) {
+        if (Pattern.matches("(jpg|png)", ext) && (compress || width != null || height != null)) {
             if (width == null && height == null) {
                 width = Integer.MAX_VALUE;
                 height = Integer.MAX_VALUE;
@@ -207,27 +135,11 @@ public class FileUploadController {
             } else if (width == null) {
                 width = Integer.MAX_VALUE;
             }
-            BufferedImage img = null;
-            if ("jpg".equals(ext)) {
-                img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height);
-            } else {
-                img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, true);
-            }
-            ByteArrayOutputStream os = new ByteArrayOutputStream();
-            ImageIO.write(img, ext, os);
-            InputStream is = new ByteArrayInputStream(os.toByteArray());
-            url = storageService.uploadFromInputStream(is, path);
-        } else if (Pattern.matches("(jpg|png)", ext) && (width != null || height != null)) {
-            if (height == null) {
-                height = Integer.MAX_VALUE;
-            } else if (width == null) {
-                width = Integer.MAX_VALUE;
-            }
-            BufferedImage img = null;
+            BufferedImage img;
             if ("jpg".equals(ext)) {
-                img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height);
+                img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height, compress);
             } else {
-                img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, false);
+                img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, compress);
             }
             ByteArrayOutputStream os = new ByteArrayOutputStream();
             ImageIO.write(img, ext, os);
@@ -241,26 +153,26 @@ public class FileUploadController {
 
 
         if ("mp4".equalsIgnoreCase(ext)) {
-            FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(tmpFile);
-            frameGrabber.start();
-            Java2DFrameConverter aa = new Java2DFrameConverter();
+            String thumbPath = "thumb_image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
+                    + RandomStringUtils.randomAlphabetic(8) + ".jpg";
 
+            FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(tmpFile);
+            Java2DFrameConverter frameConverter = new Java2DFrameConverter();
             try {
-                BufferedImage bi;
-                Frame f = frameGrabber.grabKeyFrame();
+                frameGrabber.start();
 
-                bi = aa.convert(f);
-                File thumbFile = null;
-                while (bi != null) {
-                    thumbFile = File.createTempFile("video_thumb_", ".jpg");
-                    ImageIO.write(bi, "jpg", thumbFile);
-                    f = frameGrabber.grabKeyFrame();
-                    bi = aa.convert(f);
+                Frame frame = null;
+                while (frame == null) {
+                    frame = frameGrabber.grabKeyFrame();
                 }
-                Objects.requireNonNull(thumbFile);
-                String thumbPath = "thumb_image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
-                        + RandomStringUtils.randomAlphabetic(8) + ".jpg";
-                thumbUrl = storageService.uploadFromInputStream(new FileInputStream(thumbFile), thumbPath);
+                Objects.requireNonNull(frame, "获取视频缩略图失败");
+                BufferedImage thumbBi = frameConverter.convert(frame);
+
+                BufferedImage thumbResized = ImageUtils.resizeJpg(thumbBi, 1000, 1000, false);
+                ByteArrayOutputStream os = new ByteArrayOutputStream();
+                ImageIO.write(thumbResized, "jpg", os);
+                InputStream is = new ByteArrayInputStream(os.toByteArray());
+                thumbUrl = storageService.uploadFromInputStream(is, thumbPath);
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {

+ 11 - 7
src/main/vue/src/views/BlindBoxEdit.vue

@@ -20,7 +20,13 @@
                         <el-input v-model="formData.name" :disabled="!canEdit"></el-input>
                     </el-form-item>
                     <el-form-item prop="pics" label="图片">
-                        <object-upload v-model="formData.pics[0]" :disabled="!canEdit" compress width="3000" height="3000"></object-upload>
+                        <object-upload
+                            v-model="formData.pics[0]"
+                            :disabled="!canEdit"
+                            compress
+                            width="3000"
+                            height="3000"
+                        ></object-upload>
                     </el-form-item>
                     <el-form-item prop="minterId" label="创建者">
                         <minter-select
@@ -158,12 +164,10 @@
             >
                 <el-form-item prop="collectionId" label="作品">
                     <el-select v-model="addItemForm.collectionId">
-                        <el-option
-                            v-for="item in collections"
-                            :label="item.name"
-                            :value="item.id"
-                            :key="item.id"
-                        ></el-option>
+                        <el-option v-for="item in collections" :label="item.name" :value="item.id" :key="item.id">
+                            <span style="float: left">{{ item.name }}</span>
+                            <span style="float: right; color: #8492a6; font-size: 13px">#{{ item.id }}</span>
+                        </el-option>
                     </el-select>
                 </el-form-item>
                 <el-form-item prop="total" label="数量">

+ 34 - 0
src/test/java/com/izouma/nineth/CommonTest.java

@@ -26,6 +26,9 @@ import org.apache.http.client.utils.URLEncodedUtils;
 import org.apache.poi.util.TempFile;
 import org.bouncycastle.util.encoders.Base64;
 import org.bouncycastle.util.encoders.UrlBase64Encoder;
+import org.bytedeco.javacv.FFmpegFrameGrabber;
+import org.bytedeco.javacv.Frame;
+import org.bytedeco.javacv.Java2DFrameConverter;
 import org.junit.Assert;
 import org.junit.Test;
 import org.libjpegturbo.turbojpeg.processor.api.ImageProcessException;
@@ -47,6 +50,7 @@ import javax.imageio.ImageIO;
 import java.awt.*;
 import java.awt.font.FontRenderContext;
 import java.awt.geom.AffineTransform;
+import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Method;
@@ -312,4 +316,34 @@ public class CommonTest {
         }
     }
 
+    @Test
+    public void testGrabFrame() throws IOException {
+        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("/Users/drew/Downloads/video1627914878375.mp4");
+        frameGrabber.start();
+        Java2DFrameConverter aa = new Java2DFrameConverter();
+        int length = frameGrabber.getLengthInVideoFrames();
+        Frame frame = null;
+        int i = 0;
+        while (frame == null && i < length) {
+            frame = frameGrabber.grabKeyFrame();
+            i++;
+        }
+        Objects.requireNonNull(frame);
+        BufferedImage thumbBi = aa.convert(frame);
+        ImageIO.write(thumbBi, "jpg", new File("/Users/drew/Desktop/1.jpg"));
+    }
+
+    @Test
+    public void testQuality() throws IOException {
+        Thumbnails.of("/Users/drew/Desktop/2021-11-04-11-28-36VAOoasUd.jpeg")
+                .size(1000, 1000)
+                .outputFormat("jpg")
+                .outputQuality(1)
+                .toFile("/Users/drew/Desktop/1.jpg");
+        Thumbnails.of("/Users/drew/Desktop/2021-11-04-11-28-36VAOoasUd.jpeg")
+                .size(1000, 1000)
+                .outputFormat("jpg")
+                .outputQuality(0.6)
+                .toFile("/Users/drew/Desktop/2.jpg");
+    }
 }