Przeglądaj źródła

火焰图片算法实现

ty130316261 3 lat temu
rodzic
commit
a8ca6b7bf5

+ 11 - 7
zd-modules/zd-forward/src/main/java/com/zd/forward/serivce/FireImageService.java

@@ -1,20 +1,17 @@
 package com.zd.forward.serivce;
 
-import com.zd.common.core.constant.Constants;
-import com.zd.common.core.domain.R;
 import com.zd.common.core.exception.ServiceException;
 import com.zd.common.core.utils.IdGen;
-import com.zd.common.core.utils.StringUtils;
 import com.zd.forward.config.AlgorithmYml;
 import com.zd.forward.domain.DataPostAnalysisRespDto;
 import com.zd.forward.domain.ImgPostResponse;
 import com.zd.forward.properties.FireProperties;
 import com.zd.forward.util.HttpUtils;
-import com.zd.forward.util.VideoUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.bytedeco.javacv.FFmpegFrameGrabber;
 import org.bytedeco.javacv.Frame;
 import org.bytedeco.javacv.FrameGrabber;
+import org.bytedeco.javacv.Java2DFrameConverter;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpEntity;
 import org.springframework.stereotype.Service;
@@ -23,16 +20,14 @@ import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
 import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.Collections;
-import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 
 import static com.zd.forward.util.HttpUtils.getHttpEntityMap;
-import static com.zd.forward.util.HttpUtils.getStringObjectMultiValueMap;
-import static com.zd.forward.util.VideoUtil.FrameToBufferedImage;
 
 /**
  * 火焰图片抓拍处理
@@ -116,4 +111,13 @@ public class FireImageService {
             sendSginAccessLogService.saveAlarm(fireProperties);
         }
     }
+
+    /**
+     * frame 转图片流
+     */
+    public static BufferedImage FrameToBufferedImage(Frame frame) {
+        //创建BufferedImage对象
+        Java2DFrameConverter converter = new Java2DFrameConverter();
+        return converter.getBufferedImage(frame);
+    }
 }

+ 0 - 157
zd-modules/zd-forward/src/main/java/com/zd/forward/util/VideoUtil.java

@@ -1,157 +0,0 @@
-package com.zd.forward.util;
-
-import com.zd.common.core.utils.IdGen;
-import com.zd.common.redis.service.RedisService;
-import com.zd.forward.properties.FireProperties;
-import org.bytedeco.ffmpeg.global.avcodec;
-import org.bytedeco.javacv.*;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.web.multipart.MultipartFile;
-
-import javax.annotation.Resource;
-import javax.imageio.ImageIO;
-import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicBoolean;
-
-public class VideoUtil {
-
-    //调用原子线程判断
-    private static final AtomicBoolean running = new AtomicBoolean(true);
-
-    public String streamURL;// 流地址
-
-    public String filePath;// 视频文件路径
-
-    /**
-     * 上传文件存储在本地的根路径
-     */
-    @Value("${file.path:/home/AIPIC}")
-    public String imagePath;// 图片路径,存放截取视频某一帧的图片
-
-    @Resource
-    RedisService redisService;
-    @Resource
-    FireProperties fireProperties;
-
-    public void setStreamURL(String streamURL) {
-        this.streamURL = streamURL;
-    }
-
-    public void setFilePath(String filePath) {
-        this.filePath = filePath;
-    }
-
-    public void setImagePath(String imagePath) {
-        this.imagePath = imagePath;
-    }
-
-    /**
-     * frame 转图片流
-     */
-    public static BufferedImage FrameToBufferedImage(Frame frame) {
-        //创建BufferedImage对象
-        Java2DFrameConverter converter = new Java2DFrameConverter();
-        return converter.getBufferedImage(frame);
-    }
-
-
-    /**
-     * 执行视频流抓取
-     */
-    public void run(Long uuid, boolean screenshot) {
-        Map<Long, Object> resultMap = new HashMap<>(3);
-        Map<String, Object> map = new LinkedHashMap<>(5);
-        map.put("screenshot", screenshot);
-        System.out.println(streamURL);
-        // 获取视频源
-        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(streamURL);
-        try {
-            grabber.start();
-            //获取第一帧
-            Frame frame = grabber.grabFrame();
-            if (frame != null) {
-                int flag = 0;
-                while (frame != null && VideoUtil.running.get()) {
-                    //视频快照
-                    frame = grabber.grabImage();
-                    //文件储存对象
-                    String fileName = imagePath + uuid + "_" + flag;
-                    File tempFile = File.createTempFile(fileName, ".jpg");
-                    ImageIO.write(FrameToBufferedImage(frame), "jpg", tempFile);
-                }
-                //停止
-                grabber.stop();
-                //保存图片
-                resultMap.put(uuid, map);
-                //转成JSON存储到redis
-                redisService.redisTemplate.opsForValue().set(uuid,resultMap,60*60);
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-        } finally {
-            try {
-                grabber.stop();
-            } catch (FrameGrabber.Exception e) {
-                e.printStackTrace();
-            }
-        }
-        VideoUtil.running.set(true);
-    }
-
-
-    /**
-     * 测试:
-     * 1、在D盘中新建一个test文件夹,test中再分成video和img,在video下存入一个视频,并命名为test
-     * D:/test/video     D:/test/img
-     */
-    public static void main(String[] args) {
-        //getVideoStream();
-        String result = String.valueOf(getStreamScreenshot("D://files/video/F28688868_20210317093106.flv", "D://files/img/", "rtmp://58.200.131.2:1935/livetv/hunantv"));
-        System.out.println(result);
-    }
-
-
-    /**
-     * 获取视频流
-     */
-    public static Long getVideoStream(String filePath, String imagePath, String streamURL) {
-        return getStream(filePath, imagePath, streamURL, false);
-    }
-
-    /**
-     * 获取视频流快照
-     */
-    public static Long getStreamScreenshot(String filePath, String imagePath, String streamURL) {
-        return getStream(filePath, imagePath, streamURL, true);
-    }
-
-    /**
-     * 开始执行下拉流截视频流
-     */
-    public static Long getStream(String filePath, String imagePath, String streamURL, boolean screenshot) {
-        VideoUtil videoUtil = new VideoUtil();
-        videoUtil.setFilePath(filePath);
-        videoUtil.setImagePath(imagePath);
-        videoUtil.setStreamURL(streamURL);
-        Long uuid = IdGen.snowflakeId();
-        try {
-            Thread t0 = new Thread(() -> {
-                System.out.println("start...");
-                videoUtil.run(uuid, screenshot);
-            });
-            t0.start();
-            Thread.sleep(55000);
-            System.out.println("stop...");
-            running.set(false);
-        } catch (Throwable t) {
-            System.out.println("Caught in main: " + t);
-            t.printStackTrace();
-        }
-        return uuid;
-    }
-}