PPT轉圖片(更新)
update:2008-05-05,POI已經可以處理這個需求:
package net.rubyeye.test;
import java.io.FileOutputStream;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.SlideShow;
public class PPTToImageConverter {
public static void main(String[] args) throws Exception {
SlideShow ppt = new SlideShow(new HSLFSlideShow("D:/test.ppt"));
// extract all pictures contained in the presentation
PictureData[] pdata = ppt.getPictureData();
for (int i = 0; i < pdata.length; i++) {
PictureData pict = pdata[i];
// picture data
byte[] data = pict.getData();
int type = pict.getType();
String ext;
switch (type) {
case Picture.JPEG:
ext = ".jpg";
break;
case Picture.PNG:
ext = ".png";
break;
case Picture.WMF:
ext = ".wmf";
break;
case Picture.EMF:
ext = ".emf";
break;
case Picture.PICT:
ext = ".pict";
break;
default:
continue;
}
FileOutputStream out = new FileOutputStream("D:/test/pict_" + i + ext);
out.write(data);
out.close();
}
}
}
import java.io.FileOutputStream;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.SlideShow;
public class PPTToImageConverter {
public static void main(String[] args) throws Exception {
SlideShow ppt = new SlideShow(new HSLFSlideShow("D:/test.ppt"));
// extract all pictures contained in the presentation
PictureData[] pdata = ppt.getPictureData();
for (int i = 0; i < pdata.length; i++) {
PictureData pict = pdata[i];
// picture data
byte[] data = pict.getData();
int type = pict.getType();
String ext;
switch (type) {
case Picture.JPEG:
ext = ".jpg";
break;
case Picture.PNG:
ext = ".png";
break;
case Picture.WMF:
ext = ".wmf";
break;
case Picture.EMF:
ext = ".emf";
break;
case Picture.PICT:
ext = ".pict";
break;
default:
continue;
}
FileOutputStream out = new FileOutputStream("D:/test/pict_" + i + ext);
out.write(data);
out.close();
}
}
}
原文:
小結下最近做的東西吧。因為是做一個素材管理的東西,因此需要處理各種各樣的素材,音頻、視頻、圖片、pdf、ppt等等。遇到一個需求就是將PPT轉成圖片組,google一下,在java裏是可以jcom之類的開源庫實現,本質上都是通過jni調用office的COM接口來實現。我們就需要這麼一個小功能,拖這麼大個開源庫進來實在沒有必要。最後決定自己寫個動態鏈接庫,通過jni來調用。
先寫工具類,
public class PPTUtils {
public PPTUtils() {
}
public static native void convertPPT2IMG(String pptFileName, String tmpDir);
public static void loadLibrary() {//加載動態庫
String dllFileName = "pptDll";
try {
String OsName = System.getProperty("os.name");
if (OsName.contains("Windows")) {
dllFileName += ".dll";
} else {
dllFileName += ".so";
}
//加載動態鏈接庫
System.load(dllFileName);
}
catch (Exception e) {
// LOG.error("can not load " + dllFileName + ", " + e.getMessage());
e.printStackTrace();
}
}
}
public PPTUtils() {
}
public static native void convertPPT2IMG(String pptFileName, String tmpDir);
public static void loadLibrary() {//加載動態庫
String dllFileName = "pptDll";
try {
String OsName = System.getProperty("os.name");
if (OsName.contains("Windows")) {
dllFileName += ".dll";
} else {
dllFileName += ".so";
}
//加載動態鏈接庫
System.load(dllFileName);
}
catch (Exception e) {
// LOG.error("can not load " + dllFileName + ", " + e.getMessage());
e.printStackTrace();
}
}
}
編譯一下,執行javah PPTUtils生成頭文件PPTUtils.h。接下來用vc寫個動態鏈接庫,記的將MSPPT.OLB(在office安裝目錄下)加入工程,新建一個ppt2img.cpp:
#include "stdafx.h"
#include "PPTUtils.h"
#include "msppt.h"
JNIEXPORT void JNICALL Java_com_starnet_dmb_util_PPTUtils_convertPPT2IMG(JNIEnv *env,
jclass clazz, jstring pptFileName, jstring tmpDir){
//初始化com
if (CoInitialize( NULL ) == E_INVALIDARG)
{
AfxMessageBox(_T("初始化Com失敗!"));
return;
}
_Application app;
Presentations prsts;
_Presentation prst;
//jstring轉成char *
const char *ppt;
ppt = env->GetStringUTFChars(pptFileName,0);
const char *tmp;
tmp=env->GetStringUTFChars(tmpDir,0);
if(!app.CreateDispatch(_T("PowerPoint.Application"))){
AfxMessageBox(_T("初始化PowerPoint失敗!"));
return;
}
prsts = app.GetPresentations();
prst = prsts.Open(_T(ppt),false,false,false);
prst.SaveAs(_T(tmp),17,false);
app.ReleaseDispatch();
app.Quit();
env->ReleaseStringUTFChars(pptFileName,ppt);
env->ReleaseStringUTFChars(tmpDir,tmp);
CoUninitialize();
}
#include "PPTUtils.h"
#include "msppt.h"
JNIEXPORT void JNICALL Java_com_starnet_dmb_util_PPTUtils_convertPPT2IMG(JNIEnv *env,
jclass clazz, jstring pptFileName, jstring tmpDir){
//初始化com
if (CoInitialize( NULL ) == E_INVALIDARG)
{
AfxMessageBox(_T("初始化Com失敗!"));
return;
}
_Application app;
Presentations prsts;
_Presentation prst;
//jstring轉成char *
const char *ppt;
ppt = env->GetStringUTFChars(pptFileName,0);
const char *tmp;
tmp=env->GetStringUTFChars(tmpDir,0);
if(!app.CreateDispatch(_T("PowerPoint.Application"))){
AfxMessageBox(_T("初始化PowerPoint失敗!"));
return;
}
prsts = app.GetPresentations();
prst = prsts.Open(_T(ppt),false,false,false);
prst.SaveAs(_T(tmp),17,false);
app.ReleaseDispatch();
app.Quit();
env->ReleaseStringUTFChars(pptFileName,ppt);
env->ReleaseStringUTFChars(tmpDir,tmp);
CoUninitialize();
}
文章轉自莊周夢蝶 ,原文發布時間2008-01-04
最後更新:2017-05-17 17:01:59
上一篇:
介紹一個輕量級java的swf處理庫
下一篇:
視頻站點的搭建
Windows的定時任務(Schedule Task)設置
阿裏雲計算能力實現多項突破 BigBench規模全球首次被拓展至100TB
在 Ubuntu 16.04 上安裝和使用服務器監控報警係統 Shinken
3月29日雲棲精選夜讀:阿裏“NASA”首個重磅武器亮相——機器學習平台PAI2.0
oracle和sql server取第一條記錄的區別以及rownum詳解
備份校驗兩不誤,MySQL自動備份還原校驗設計詳解
Design Pattern: Prototype 模式
PostgreSQL 10.0 preview 功能增強 - 串行隔離級別 預加鎖閾值可控
MySQL鎖係列(一)之鎖的種類和概念
串口大師提示找不到串口的解決辦法,增加注冊表項目。