From e67870fff62635cd14beb0d5988f08aeef4b22fa Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 07 四月 2025 21:27:06 +0800
Subject: [PATCH] 添加远程关阀式终止灌溉计划,未测试

---
 pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/VideoUtils.java |  172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 172 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/VideoUtils.java b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/VideoUtils.java
new file mode 100644
index 0000000..1258861
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/VideoUtils.java
@@ -0,0 +1,172 @@
+package com.dy.pipIrrWebFile.util;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.jcodec.api.FrameGrab;
+import org.jcodec.common.model.Picture;
+import org.jcodec.scale.AWTUtil;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.*;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2025/1/13 11:22
+ * @Description
+ */
+
+/**
+ * 瑙嗛鎿嶄綔宸ュ叿绫�
+ */
+@Slf4j
+public class VideoUtils {
+
+    /*** 鍥剧墖鏍煎紡*/
+    private static final String FILE_EXT = "jpg";
+
+    /*** 甯ф暟*/
+    private static final int THUMB_FRAME = 5;
+
+    /*** 鍥剧墖鏍煎紡*/
+    private String fileExt ;
+
+    /*** 甯ф暟*/
+    private Integer thumbFrame ;
+
+    public VideoUtils(){
+        this.fileExt = FILE_EXT;
+        this.thumbFrame = THUMB_FRAME;
+    }
+
+
+    public VideoUtils(String fileExt, int thumbFrame){
+        this.fileExt = fileExt;
+        this.thumbFrame = thumbFrame;
+        if(this.fileExt == null || this.fileExt.trim().equals("")){
+            this.fileExt = FILE_EXT;
+        }
+        if(this.thumbFrame == null || this.thumbFrame.intValue() <= 0){
+            this.thumbFrame = THUMB_FRAME;
+        }
+    }
+
+    /**
+     * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+     * @param videoFilePath 婧愯棰戞枃浠惰矾寰�
+     * @param frameFilePath 鎴彇甯х殑鍥剧墖瀛樻斁璺緞
+     */
+    public void fetchFrame(String videoFilePath, String frameFilePath) throws Exception {
+        File videoFile = new File(videoFilePath);
+        File frameFile = new File(frameFilePath);
+        getThumbnail(videoFile, frameFile);
+    }
+
+
+    /**
+     * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+     * @param videoFilePath 婧愯棰戞枃浠惰矾寰�
+     */
+    public BufferedImage fetchFrame(String videoFilePath) throws Exception {
+        File videoFile = new File(videoFilePath);
+        return getThumbnail(videoFile);
+    }
+
+    /**
+     * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+     *
+     * @param videoFile  婧愯棰戞枃浠�
+     * @param targetFile 鎴彇甯х殑鍥剧墖
+     */
+    public void fetchFrame(MultipartFile videoFile, File targetFile) throws Exception {
+        File file = new File(videoFile.getName());
+        FileUtils.copyInputStreamToFile(videoFile.getInputStream(), file);
+        getThumbnail(file, targetFile);
+    }
+
+    /**
+     * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+     *
+     * @param videoFile  婧愯棰戞枃浠�
+     */
+    public BufferedImage fetchFrame(MultipartFile videoFile) throws Exception {
+        File file = new File(videoFile.getName());
+        FileUtils.copyInputStreamToFile(videoFile.getInputStream(), file);
+        return getThumbnail(file);
+    }
+
+    /**
+     * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+     *
+     * @param videoFile 婧愯棰戞枃浠�
+
+    public File fetchFrame2(MultipartFile videoFile) {
+        String originalFilename = videoFile.getOriginalFilename();
+        File file = new File(originalFilename);
+        File targetFile = null;
+        try {
+            FileUtils.copyInputStreamToFile(videoFile.getInputStream(), file);
+
+            int i = originalFilename.lastIndexOf(".");
+            String imageName;
+
+            if (i > 0) {
+                imageName = originalFilename.substring(0, i);
+            } else {
+                imageName = UUID.randomUUID().toString().replace("-", "");
+            }
+            imageName = imageName + ".jpg";
+            targetFile = new File(imageName);
+            getThumbnail(file, targetFile);
+        } catch (Exception e) {
+            log.error("鑾峰彇瑙嗛鎸囧畾甯у紓甯革細", e);
+        } finally {
+            if (file.exists()) {
+                file.delete();
+            }
+        }
+        log.debug("瑙嗛鏂囦欢 - 甯ф埅鍙� - 澶勭悊缁撴潫");
+        return targetFile;
+    }
+    */
+
+    /**
+     * 鑾峰彇绗竴甯х缉鐣ュ浘
+     *
+     * @param videoFile  瑙嗛璺緞
+     * @param targetFile 缂╃暐鍥剧洰鏍囪矾寰�
+     */
+    public void getThumbnail(File videoFile, File targetFile) throws Exception {
+        // 鏍规嵁鎵╁睍鍚嶅垱寤轰竴涓柊鏂囦欢璺緞
+        Picture picture = FrameGrab.getFrameFromFile(videoFile, thumbFrame);
+        BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture);
+        ImageIO.write(bufferedImage, fileExt, targetFile);
+    }
+
+
+    /**
+     * 鑾峰彇绗竴甯х缉鐣ュ浘
+     *
+     * @param videoFile  瑙嗛璺緞
+     */
+    public BufferedImage getThumbnail(File videoFile) throws Exception {
+        // 鏍规嵁鎵╁睍鍚嶅垱寤轰竴涓柊鏂囦欢璺緞
+        Picture picture = FrameGrab.getFrameFromFile(videoFile, thumbFrame);
+        BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture);
+        return bufferedImage;
+    }
+
+
+    public static void main(String[] args) {
+        VideoUtils util = new VideoUtils();
+        try {
+            long startTime = System.currentTimeMillis();
+            util.getThumbnail(new File("D:\\mp4\\test.mp4"), new File("D:\\mp4\\test.jpg"));
+            System.out.println("鎴彇鍥剧墖鑰楁椂锛�" + (System.currentTimeMillis() - startTime));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+}

--
Gitblit v1.8.0