1、新增控制器
@NotFilter
@ApiOperation("根據課件轉化PPT再返回圖片數組")
@PostMapping("/reviewPPt/{fileId}")
public Map<String, Object> reviewPPt(@PathVariable("fileId") Integer fileId) {
//根據文件uri 得到文件
//轉換的文件圖片的保存地址
//返回文件的url和圖片文件名
return courseFileService.reviewPPt(fileId);
}
2、通過文件id,查詢文件記錄,拿到文件url,uri。
@Override
public Map<String, Object> reviewPPt(Integer fileId) {
//根據文件uri 得到文件
//轉換的文件圖片的保存地址
//返回文件的url和圖片文件名
Map<String, Object> resultMap = new HashMap<>();
CourseFile file = courseFileRepository.findById(fileId).orElse(null);
if(file==null){
throw new ContsException(ResultEnum.FILE_ID_NOT);
}
File ppt = new File(file.getUri());
if(!ppt.exists()){
throw new ContsException(ResultEnum.FILE_NOT);
}
String parent = ppt.getParent()+File.separator;
String imagePrefix = "images"+File.separator+file.getFileName().substring(0,file.getFileName().lastIndexOf("."));
String imagePath = parent + imagePrefix;
File imageFile = new File(imagePath);
if (!imageFile.exists()){
imageFile.mkdirs();
}
List list = FileUtils.pptToImage(ppt, imageFile);
resultMap.put("imagePathList", list);
resultMap.put("reviewUrlPrefix", file.getUrl().substring(0,file.getUrl()
.lastIndexOf(File.separator))+File.separator+imagePrefix);
return resultMap;
}
3、新增工具類,進行ppt轉圖片寫入
public static List pptToImage(File pptFile, File imgFile) {
List<String> list = new ArrayList<>();
FileInputStream is = null ;
int imgCount = 0;
try {
is = new FileInputStream(pptFile);
XMLSlideShow xmlSlideShow = new XMLSlideShow(is);
is.close();
// 獲取大小
Dimension pgsize = xmlSlideShow.getPageSize();
// 獲取幻燈片
List<XSLFSlide> slides = xmlSlideShow.getSlides();
imgCount = slides.size();
for (int i = 0 ; i < slides.size() ; i++) {
// 解決亂碼問題
List<XSLFShape> shapes = slides.get(i).getShapes();
for (XSLFShape shape : shapes) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape sh = (XSLFTextShape) shape;
List<XSLFTextParagraph> textParagraphs = sh.getTextParagraphs();
for (XSLFTextParagraph xslfTextParagraph : textParagraphs) {
List<XSLFTextRun> textRuns = xslfTextParagraph.getTextRuns();
for (XSLFTextRun xslfTextRun : textRuns) {
xslfTextRun.setFontFamily("宋體");
}
}
}
}
//根據幻燈片大小生成圖片
BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,pgsize.height));
// 最核心的代碼
slides.get(i).draw(graphics);
//圖片將要存放的路徑
String absolutePath = imgFile.getAbsolutePath()+"/"+ (i + 1) + ".jpg";
File jpegFile = new File(absolutePath);
// 圖片路徑存放
list.add((i + 1) + ".jpg");
//如果圖片存在,則不再生成
if (jpegFile.exists()) {
continue;
}
// 這裏設置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑
FileOutputStream out = new FileOutputStream(jpegFile);
// 寫入到圖片中去
ImageIO.write(img, "jpeg", out);
out.close();
}
System.out.print("PPT轉換成圖片 成功!");
//log.error("PPT轉換成圖片 成功!");
return list;
}
catch (Exception e) {
e.printStackTrace();
System.out.print("PPT轉換成圖片 發生異常!");
// log.error("PPT轉換成圖片 發生異常!", e);
}
return list;
}
4、測試結果: