javaFX:实现会议多媒体切换系统(二)

本文详细介绍了使用JavaFX和Maven如何将PPTX文件的页面转换为图片,以应用于多媒体切换系统。核心类PPTUtil包含关键功能,实现了PPT到图片的转换操作。
摘要由CSDN通过智能技术生成

关键类(PPTUtil):

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package ren.kun.meetingshow;

import com.spire.presentation.Presentation;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.hslf.usermodel.HSLFTextRun;
import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
import org.springframework.util.CollectionUtils;

/**
 *
 * @author 65340
 */
public class PPTUtil {

    public static void PPTtoImage(String filePath, String fileName) throws Exception {
        File f = new File(filePath + fileName);
        if (f.exists()) {
            if (f.getName().endsWith(".pptx") || f.getName().endsWith(".PPTX")) {
                ppt2Img(filePath, fileName);
            } else {
                System.out.println("无法解析ppt文件,请使用pptx文件");
            }
        } else {
            System.out.println("文件不存在");
        }
    }
    
    //pptx里的页面转图片的关键函数
    public static void ppt2Img(String filePath, String fileName) throws Exception {
        String fileUrl = filePath + fileName;
        String outputFile = filePath + "output";
        Presentation ppt = new Presentation();
        ppt.loadFromFile(fileUrl);
        for (int i = 0; i < ppt.getSlides().getCount(); i++) {

            BufferedImage image = ppt.getSlides().get(i).saveAsImage();
            String fn = outputFile + "//" + String.format("ToImage-%1$s.jpg", i);
            new File(fn).createNewFile();
            ImageIO.write(image, "jpg", new File(fn));
        }
    }

    //下面的函数无法成功执行(是pptx转Image的一个方法,但行不通)
    public static void pptx2Image(String filePath, String fileName) throws Exception {

        // 获取系统可用字体
        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = e.getAvailableFontFamilyNames();
        List availFonts = CollectionUtils.arrayToList(fontNames);

        FileInputStream is = new FileInputStream(new File(filePath + fileName));

        XMLSlideShow ppt = new XMLSlideShow(is);

        Dimension pgsize = ppt.getPageSize();
        int pageSize = ppt.getSlides().size();
        
        for (int i = 0; i < pageSize; i++) {

            //防止中文乱码
            for (XSLFShape shape : ppt.getSlides().get(i).getShapes()) {
                if (shape instanceof XSLFTextShape) {
                    XSLFTextShape tsh = (XSLFTextShape) shape;
                    for (XSLFTextParagraph p : tsh) {
                        for (XSLFTextRun r : p) {
                            String fontFamily = r.getFontFamily();
                            //log.info(">>>>>原始ppt字体:{}", fontFamily);
                            fontFamily = "宋体";
                            //log.info(">>>>>ppt字体修改为:{}", fontFamily);
                            r.setFontFamily(fontFamily);
//                            if (!availFonts.contains(fontFamily)) {
//                                fontFamily = "宋体";
//                                log.info(">>>>>ppt字体修改为:{}", fontFamily);
//                                r.setFontFamily(fontFamily);
//                            }
                        }
                    }
                }
            }
            BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            // clear the drawing area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

            // render
            ppt.getSlides().get(i).draw(graphics);
            // save the output
            String pptname = fileName.substring(0, fileName.lastIndexOf("."));
            String newimgPath = filePath + "image/" + pptname + "/";
            File imgPath = new File(newimgPath);
            if (!imgPath.exists()) {//图片目录不存在则创建
                imgPath.mkdirs();
            }
            String file = newimgPath + (i + 1) + ".png";
            FileOutputStream out = new FileOutputStream(file);
            javax.imageio.ImageIO.write(img, "png", out);
            IOUtils.closeQuietly(out);
        }

    }

    //下面的函数无法成功执行(是pptx转Image的一个方法,但行不通)
    public static void ppt2Image(String filePath, String fileName) throws IOException {
        // 获取系统可用字体
        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = e.getAvailableFontFamilyNames();
        List availFonts = CollectionUtils.arrayToList(fontNames);

        HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(filePath + fileName));

        Dimension pgsize = ppt.getPageSize();
        for (int i = 0; i < ppt.getSlides().size(); i++) {
            //防止中文乱码
            for (HSLFShape shape : ppt.getSlides().get(i).getShapes()) {
                if (shape instanceof HSLFTextShape) {
                    HSLFTextShape tsh = (HSLFTextShape) shape;
                    for (HSLFTextParagraph p : tsh) {
                        for (HSLFTextRun r : p) {
                            String fontFamily = r.getFontFamily();
                            //log.info(">>>>>原始ppt字体:{}", fontFamily);
                            fontFamily = "宋体";
                            //log.info(">>>>>ppt字体修改为:{}", fontFamily);
                            r.setFontFamily(fontFamily);
//                            if (!availFonts.contains(fontFamily)) {
//                                fontFamily = "宋体";
//                                log.info(">>>>>ppt字体修改为:{}", fontFamily);
//                                r.setFontFamily(fontFamily);
//                            }

                        }
                    }
                }
            }

            BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();

            // clear the drawing area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
            // render

            HSLFSlide s = ppt.getSlides().get(i);
            s.draw(graphics);

            // save the output
            String pptname = fileName.substring(0, fileName.lastIndexOf("."));
            String newimgPath = filePath + "image/" + pptname + "/";
            File imgPath = new File(newimgPath);
            if (!imgPath.exists()) {//图片目录不存在则创建
                imgPath.mkdirs();
            }
            String file = newimgPath + (i + 1) + ".png";
            FileOutputStream out = new FileOutputStream(file);
            javax.imageio.ImageIO.write(img, "png", out);
            IOUtils.closeQuietly(out);
            //resizeImage(filename, filename, width, height);
        }

    }

    public static int countFileNum(String filePath, String endStr){
        int counts = 0;
        File dirFile = new File(filePath);
        if (dirFile.isDirectory()) {
            File[] files1 = dirFile.listFiles();
            for (int i = 0; i < files1.length; i++) {
                if (files1[i].getName().endsWith(endStr)) {
                    counts++;
                }
            }
        }
        
        return counts;
    }
}

对该类(PPTUtil)的总结:

该类里面函数的最重要功能,就是把pptx里面的页面转换成图片

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值