当前位置:   article > 正文

如何将文档、视频某页或某帧转换成图片?_converter.getbufferedimage

converter.getbufferedimage

目录

一、需求背景

二、引入依赖

三、根据自身的业务编写合适的代码


一、需求背景

        博主的大概需求是,获取第一章第一节的课件(有pdf、各种文档、视频等形式),生成课程的封面图片。

二、引入依赖
 

  1. <dependency>
  2. <groupId>org.bytedeco</groupId>
  3. <artifactId>javacv</artifactId>
  4. <version>1.4.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.bytedeco</groupId>
  8. <artifactId>javacpp</artifactId>
  9. <version>1.4.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.bytedeco.javacpp-presets</groupId>
  13. <artifactId>ffmpeg-platform</artifactId>
  14. <version>3.4.2-1.4.1</version>
  15. </dependency>

三、根据自身的业务编写合适的代码

  1. /**
  2. * 保存课程信息自动设置课程封面图片
  3. * @param courseInformation
  4. * @param coursewareId
  5. */
  6. private void setDefaultCourseCoverPhoto(CourseInformation courseInformation, Long coursewareId) {
  7. String key = "";
  8. InputStream pdfStream = null;
  9. PDDocument pdfDocument = null;
  10. try {
  11. CoursewareLibrary library = coursewareLibraryMapper.selectById(coursewareId);
  12. //获取网络流
  13. String urlByPreviewPdf = documentManagementApiServer.getUrlByPreviewPdf(library.getSourceFileKey());
  14. pdfStream = ImageUtil.getImageStream(urlByPreviewPdf);
  15. if (BusinessConstant.COURSEWARE_LIBRARY_VIDEO.equals(library.getCoursewareType()) ||
  16. BusinessConstant.COURSEWARE_LIBRARY_AUDIO.equals(library.getCoursewareType())){
  17. pdfStream = returnBitMap(urlByPreviewPdf);
  18. //1.生成本地源文件、目的文件
  19. String source = tempPath + "/source/" + library.getSourceFileKey();
  20. MultipartFile multipartFile = FileUtil.getMultipartFile(pdfStream, library.getSourceFileKey());
  21. File file = FileUtil.toFile(multipartFile);
  22. BufferedImage buffImage = VideoCaptureUtils.generatePhoto(source,file.getPath());
  23. MultipartFile uploadMultipartFile = fileCase(buffImage);
  24. key = uploadPhoto(uploadMultipartFile);
  25. FileUtil.del(file);
  26. FileUtil.del(source);
  27. }else if (BusinessConstant.COURSEWARE_LIBRARY_FILE.equals(library.getCoursewareType()) ||
  28. BusinessConstant.COURSEWARE_LIBRARY_PDF.equals(library.getCoursewareType())){
  29. pdfDocument = PDDocument.load(pdfStream);
  30. //PDFRenderer的类将PDF文档呈现为AWT BufferedImage
  31. PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);
  32. // 提取的页码
  33. int pageNumber = 0;
  34. // 以300 dpi 读取存入 BufferedImage 对象
  35. int dpi = 500;
  36. //Renderer类的renderImage()方法在特定页面中渲染图像
  37. BufferedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
  38. // 文件类型转换
  39. MultipartFile multipartFile = fileCase(buffImage);
  40. key = uploadPhoto(multipartFile);
  41. // 关闭文档
  42. pdfDocument.close();
  43. //删除临时文件
  44. // log.info("临时文件的目录:"+s);
  45. }
  46. courseInformation.setCoverPhotoKey(key);
  47. }catch (Exception e){
  48. log.error("{}",e);
  49. e.printStackTrace();
  50. }finally {
  51. IOUtils.closeQuietly(pdfStream);
  52. IOUtils.closeQuietly(pdfDocument);
  53. }
  54. }
  1. public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
  2. FileItem fileItem = createFileItem(inputStream, fileName);
  3. return new CommonsMultipartFile(fileItem);
  4. }
  5. public static File toFile(MultipartFile multipartFile) {
  6. String fileName = multipartFile.getOriginalFilename();
  7. String prefix = "." + getExtensionName(fileName);
  8. File file = null;
  9. try {
  10. file = File.createTempFile(IdUtil.simpleUUID(), prefix);
  11. multipartFile.transferTo(file);
  12. } catch (IOException var5) {
  13. var5.printStackTrace();
  14. }
  15. return file;
  16. }
  17. /**
  18. * 将buffImage转换为MultipartFile
  19. * @param buffImage
  20. * @return MultipartFile
  21. */
  22. private MultipartFile fileCase(BufferedImage buffImage) {
  23. //得到BufferedImage对象
  24. // BufferedImage bufferedImage = JoinTwoImage.testEncode(200, 200, url);
  25. MultipartFile multipartFile= null;
  26. try {
  27. //创建一个ByteArrayOutputStream
  28. ByteArrayOutputStream os = new ByteArrayOutputStream();
  29. //把BufferedImage写入ByteArrayOutputStream
  30. ImageIO.write(buffImage, "jpg", os);
  31. //ByteArrayOutputStream转成InputStream
  32. InputStream input = new ByteArrayInputStream(os.toByteArray());
  33. //InputStream转成MultipartFile
  34. multipartFile =new MockMultipartFile("file", "file.jpg", "text/plain", input);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. return multipartFile;
  39. }
  1. import org.bytedeco.javacv.FFmpegFrameGrabber;
  2. import org.bytedeco.javacv.Frame;
  3. import org.bytedeco.javacv.Java2DFrameConverter;
  4. import javax.imageio.ImageIO;
  5. import java.awt.*;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class VideoCaptureUtils {
  11. /**
  12. *
  13. * @Title: getTempPath
  14. * @Description: 生成视频的首帧图片方法
  15. * @author: Zing
  16. * @param: @param tempPath 生成首帧图片的文件地址
  17. * @param: @param filePath 传进来的线上文件
  18. * @param: @return
  19. * @param: @throws Exception
  20. * @return: boolean
  21. * @throws
  22. */
  23. public static BufferedImage generatePhoto(String tempPath, String filePath) throws Exception {
  24. File targetFile = new File(tempPath);
  25. if (!targetFile.getParentFile().exists()) {
  26. targetFile.getParentFile().mkdirs();
  27. }
  28. File file2 = new File(filePath);
  29. BufferedImage bufferedImage = null;
  30. //判断文件是否为视频
  31. if(isVideo(filePath)) {
  32. if (file2.getParentFile().exists()) {
  33. FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file2);
  34. ff.start();
  35. int ftp = ff.getLengthInFrames();
  36. int flag=0;
  37. Frame frame = null;
  38. while (flag <= ftp) {
  39. //获取帧
  40. frame = ff.grabImage();
  41. //过滤前3帧,避免出现全黑图片
  42. if ((flag>3)&&(frame != null)) {
  43. break;
  44. }
  45. flag++;
  46. }
  47. // if(ImageIO.write(FrameToBufferedImage(frame), "jpg", targetFile)) {
  48. // ff.close();
  49. // ff.stop();
  50. // return true;
  51. // }else {
  52. // ff.close();
  53. // ff.stop();
  54. // return false;
  55. // }
  56. bufferedImage = FrameToBufferedImage(frame);
  57. ImageIO.write(bufferedImage, "jpg", targetFile);
  58. ff.close();
  59. ff.stop();
  60. }
  61. }
  62. return bufferedImage;
  63. }
  64. /***
  65. *
  66. * @Title: FrameToBufferedImage
  67. * @Description: 创建格式化BufferedImage对象
  68. * @author: Zing
  69. * @param: @param frame
  70. * @param: @return
  71. * @return: RenderedImage
  72. * @throws
  73. */
  74. private static BufferedImage FrameToBufferedImage(Frame frame) {
  75. //创建BufferedImage对象
  76. Java2DFrameConverter converter = new Java2DFrameConverter();
  77. BufferedImage bufferedImage = converter.getBufferedImage(frame);
  78. return bufferedImage;
  79. }
  80. /**
  81. *
  82. * @Title: isVideo
  83. * @Description:判断是不是视频
  84. * @author: Zing
  85. * @param: @param path 文件路径
  86. * @param: @return
  87. * @return: boolean true是视频 false非视频
  88. * @throws
  89. */
  90. public static boolean isVideo(String path) {
  91. List<String> typeList = new ArrayList<String>();
  92. typeList.add("mp4");
  93. typeList.add("flv");
  94. typeList.add("avi");
  95. typeList.add("rmvb");
  96. typeList.add("rm");
  97. typeList.add("wmv");
  98. //获取文件名和后缀
  99. String suffix = path.substring(path.lastIndexOf(".") + 1);
  100. for(String type : typeList) {
  101. if(type.equalsIgnoreCase(suffix)) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public static BufferedImage resize(BufferedImage img, int newW, int newH) {
  108. int w = img.getWidth();
  109. int h = img.getHeight();
  110. BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
  111. Graphics2D g = dimg.createGraphics();
  112. g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  113. RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  114. g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
  115. g.dispose();
  116. return dimg;
  117. }
  118. }

各位可以关注下面这个公众号,这个公众号会分享一些相关的技术型的文章或者开发遇到的常见问题。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/650485
推荐阅读
相关标签
  

闽ICP备14008679号