|  |  |  | 
|---|
|  |  |  | import com.dy.common.util.NumUtil; | 
|---|
|  |  |  | import com.dy.pipIrrWebFile.util.*; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.jcodec.scale.AWTUtil; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.PostMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|---|
|  |  |  | 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文件上传, 内部调用,即由其他子模块调用, | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${dy.photoZipWidth}") | 
|---|
|  |  |  | private String photoZipWidthStr ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final String VideoZipPicFileType = "jpg"; | 
|---|
|  |  |  | private static final Integer VideoZipPicFromFrame = 5 ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * web分布式文件系统保存照片文件 | 
|---|
|  |  |  | 
|---|
|  |  |  | 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{ | 
|---|
|  |  |  | 
|---|
|  |  |  | String filePath = absolutePath + fileRelativePath ; | 
|---|
|  |  |  | if(!fUtil.saveFile(filePath, input)){ | 
|---|
|  |  |  | fileRelativePath = null ; | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | //存储成功, 生成缩略图 | 
|---|
|  |  |  | BufferedImage bufImg = new VideoUtils(VideoZipPicFileType, VideoZipPicFromFrame).fetchFrame(file) ; | 
|---|
|  |  |  | // 向右旋转90度 | 
|---|
|  |  |  | // bufImg = AWTUtil.rotate90ToRight(bufImg) ; | 
|---|
|  |  |  | String zipFilePath = filePath.substring(0, filePath.lastIndexOf(".")) + "_." + VideoZipPicFileType ; | 
|---|
|  |  |  | Integer photoZipWidth = 400 ; | 
|---|
|  |  |  | if(photoZipWidthStr != null && NumUtil.isPlusIntNumber(photoZipWidthStr)){ | 
|---|
|  |  |  | photoZipWidth = Integer.parseInt(photoZipWidthStr) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | 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) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|