java pdf 转 swf

Java Process.exitValue & Process.waitFor()

Process.exitValue() 采用非阻塞的方式返回,如果没有立即拿到返回值,则抛出异常

Process.waitFor() 当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。但是如果我们在调用此方法时,如果不注意的话,很容易出现主线程阻塞,Process也挂起的情况。在调用waitFor() 的时候,Process需要向主线程汇报运行状况,所以要注意清空缓存区,即InputStream和ErrorStream,在网上,很多只提到处理InputStream,忽略了ErrorStream。以下一段代码,贴出来,仅做参考。

复制代码
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/*
* PDF转SWF工具
* @author tangs
*
*/
public class Converter {
public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOException {
////目标路径不存在则建立目标路径
File dest = new File(destPath);
if (!dest.exists()) dest.mkdirs();

//源文件不存在则返回
File source = new File(sourcePath);
if (!source.exists()) return 0;

//调用pdf2swf命令进行转换
// String command = "D:\\swftools\\pdf2swf.exe" + " -o \"" + destPath + fileName +"\" <SPAN style='COLOR: #ff0000'>-s languagedir=D:\\xpdf\\xpdf-chinese-simplified</SPAN> -s flashversion=9 \"" + sourcePath + "\"";
// String command = "D:\\swftools\\pdf2swf.exe" + " -o \"" + destPath + fileName +"\" -s flashversion=9 \"" + sourcePath + "\"";
String command= "D:/SWFTools/pdf2swf.exe -t \""+destPath+"\\Java.pdf\" -o \""+destPath+"\\test.swf\" -s flashversion=9 -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ";
System.out.println("cmd:"+command);
// Process pro = Runtime.getRuntime().exec(command);
Process process = Runtime.getRuntime().exec(command); // 调用外部程序
final InputStream is1 = process.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(is1));
try {
while(br.readLine()!= null) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
InputStream is2 = process.getErrorStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
StringBuilder buf = new StringBuilder(); // 保存输出结果流
String line = null;
while((line = br2.readLine()) != null) buf.append(line); // 循环等待ffmpeg进程结束
System.out.println("输出结果为:" + buf);

// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream()));
while (br2.readLine() != null);

try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return process.exitValue();


}


public static void main(String []args) throws IOException {
String sourcePath = "D:\\Java.pdf";
String destPath = "D:\\";
String fileName = "Javssa.swf";
try{
Converter.convertPDF2SWF(sourcePath, destPath, fileName);

}catch(Exception ex)
{
System.out.println("error");
}
System.out.println("success");



}
}
复制代码


工具准备
swftools.exe 下载
http://www.swftools.org/download.html
安装至D盘
SWFTools提供了一系列将各种文件转成swf的工具:
font2swf.exe
gif2swf.exe
jpeg2swf.exe
pdf2swf.exe
png2swf.exe
wav2swf.exe
这里我们只使用pdf2swf.exe

flexpaper下载
http://code.google.com/p/flexpaper/
这里我们使用已经编译好的FlexPaper的flash版本

posted on   肖秋峰  阅读(3925)  评论(3编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!

导航

< 2011年11月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示