From dcadab885677e68b69831b588932afafdb6e92a6 Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期一, 13 一月 2025 14:52:17 +0800 Subject: [PATCH] 增加视频文件生成缩略图功能 --- pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 54 insertions(+), 3 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java index f8e6734..62f19d8 100644 --- a/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java @@ -9,8 +9,9 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import java.io.File; -import java.io.InputStream; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.*; /** * web鏂囦欢涓婁紶, 鍐呴儴璋冪敤锛屽嵆鐢卞叾浠栧瓙妯″潡璋冪敤锛� @@ -24,6 +25,9 @@ @Value("${dy.photoZipWidth}") private String photoZipWidthStr ; + + private static final String VideoZipPicFileType = "jpg"; + private static final Integer VideoZipPicFromFrame = 5 ; /** * web鍒嗗竷寮忔枃浠剁郴缁熶繚瀛樼収鐗囨枃浠� @@ -65,7 +69,7 @@ String zipFilePath1 = filePath.substring(0, index) ; String zipFilePath2 = filePath.substring(index) ; String zipFilePath = zipFilePath1 + "_" + zipFilePath2 ; - InputStream zipFileInput = null ; + InputStream zipFileInput ; if(zipFilePath2.equalsIgnoreCase(".png")){ zipFileInput = ZipImg.zipToPng(fPic, photoZipWidth, photoZipWidth) ; }else{ @@ -152,6 +156,26 @@ String filePath = absolutePath + fileRelativePath ; if(!fUtil.saveFile(filePath, input)){ fileRelativePath = null ; + }else { + //瀛樺偍鎴愬姛, 鐢熸垚缂╃暐鍥� + BufferedImage bufImg = new VideoUtils(VideoZipPicFileType, VideoZipPicFromFrame).fetchFrame(file) ; + int index = filePath.lastIndexOf(".") ; + String basePath = filePath.substring(0, index) ; + Integer photoZipWidth = 400 ; + if(photoZipWidthStr != null && NumUtil.isPlusIntNumber(photoZipWidthStr)){ + photoZipWidth = Integer.parseInt(photoZipWidthStr) ; + } + String zipFilePath = basePath + "_." + VideoZipPicFileType ; + InputStream zipFileInput = ZipImg.zipToJpg(bufImg, photoZipWidth, photoZipWidth) ; + if(zipFileInput.available() > 0){ + new FileUtil().saveFile(zipFilePath, zipFileInput) ; + }else{ + //濡傛灉鍘嬬缉鏂囦欢涓嶅瓨鍦ㄦ垨鐢熸垚澶辫触锛屽垯澶嶅埗婧愭枃浠� + ByteArrayOutputStream os = new ByteArrayOutputStream(); + ImageIO.write(bufImg, VideoZipPicFileType, os); + InputStream in = new ByteArrayInputStream(os.toByteArray()); + new FileUtil().saveFile(zipFilePath, in) ; + } } } } @@ -199,4 +223,31 @@ return fileRelativePath ; } + + public static void main(String[] args) throws Exception{ + String photoZipWidthStr = "400" ; + String VideoZipPicFileType = "jpg"; + //瀛樺偍鎴愬姛, 鐢熸垚缂╃暐鍥� + String filePath = "D:\\mp4\\test.mp4" ; + BufferedImage bufImg = new VideoUtils("jpg", 0).fetchFrame(filePath) ; + int index = filePath.lastIndexOf(".") ; + String basePath = filePath.substring(0, index) ; + Integer photoZipWidth = 400 ; + if(photoZipWidthStr != null && NumUtil.isPlusIntNumber(photoZipWidthStr)){ + photoZipWidth = Integer.parseInt(photoZipWidthStr) ; + } + String zipFilePath = basePath + "_." + VideoZipPicFileType ; + InputStream zipFileInput = null ; + zipFileInput = ZipImg.zipToJpg(bufImg, photoZipWidth, photoZipWidth) ; + if(zipFileInput.available() > 0){ + new FileUtil().saveFile(zipFilePath, zipFileInput) ; + }else{ + //濡傛灉鍘嬬缉鏂囦欢涓嶅瓨鍦ㄦ垨鐢熸垚澶辫触锛屽垯澶嶅埗婧愭枃浠� + ByteArrayOutputStream os = new ByteArrayOutputStream(); + ImageIO.write(bufImg, VideoZipPicFileType, os); + InputStream in = new ByteArrayInputStream(os.toByteArray()); + new FileUtil().saveFile(zipFilePath, in) ; + } + } + } -- Gitblit v1.8.0