最近做gis系统发现要在flex加载wmf图片。我记得flash的loader只能是png,gis,jpg。wmf格式居然是window出的,flash居然不支持我也是醉了,没办法,只能后台转格式,首先wmf是矢量的格式,我先想到的是svg。刚好java的batick包可以支持wmf

转svg。 代码如下

 /**
* 将wmf转换为svg
*
* @param src
* @param dest
*/
public String wmfToSvg(InputStream insrc) {
boolean compatible = false;
//String resultString="";
try {
//InputStream in = new FileInputStream(src);
WmfParser parser = new WmfParser();
final SvgGdi gdi = new SvgGdi(compatible);
parser.parse(insrc, gdi);
org.w3c.dom.Document doc = gdi.getDocument();
StringWriter strwite=new StringWriter(); output(doc, strwite);
return strwite.getBuffer().toString();
// return resultString;
//getBuffer().toString();
} catch (Exception e) {
return e.getMessage();
}
} private void output(org.w3c.dom.Document doc, StringWriter out) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
"-//W3C//DTD SVG 1.0//EN");
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
transformer.transform(new javax.xml.transform.dom.DOMSource(doc), new StreamResult(out));
out.flush();
out.close();
}

Flex的svg包其实很多地方都有,as3svgrendererlib-master用的比较多,github上可以搜的到。我测试了网上的wmf都可以没啥问题,但是系统wmf是楼房基底图,导出来图片中某些字会叠加到一个角落里面,我去这怎么回事,搞了大半天也没发现什么问题

只能是先用png,wmf格式难道种类好多?之前flex 加载tiff的时候就发现tiff格式太多 作罢。

这是改为png的流输出

 BASE64Encoder encode=new BASE64Encoder();
byte[] WMF= houserow.getBlob("WMF");
if(WMF!=null&&WMF.length>0)
{
String contentString =bytetoString(WMF);
InputStream wmfStream=new StringBufferInputStream(contentString);
String svgstr=wmfToSvg(wmfStream);
ByteArrayOutputStream strwite=new ByteArrayOutputStream();
convertToPng(svgstr,strwite);
byte[]lens=strwite.toByteArray();
String WMF_pic="";
if(svgstr != null&&!svgstr.equals(""))
{
WMF_pic=encode.encode(lens);
}
createKeyPairXML(ke12,"WMF","户型图",WMF_pic,""); } /**
* 将svgCode转换成png文件,直接输出到流中
*
* @param svgCode svg代码
* @param outputStream 输出流
* @throws TranscoderException 异常
* @throws IOException io异常
*/
public void convertToPng(String svgCode, OutputStream outputStream)
throws TranscoderException, IOException {
try {
byte[] bytes = svgCode.getBytes("ISO-8859-1");
PNGTranscoder t = new PNGTranscoder(); TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
TranscoderOutput output = new TranscoderOutput(outputStream);
t.transcode(input, output);
outputStream.flush();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static String bytetoString(byte[] in)throws Exception{
InputStream is =byteTOInputStream(in);
return InputStreamTOString(is);
}
public static InputStream byteTOInputStream(byte[] in)throws Exception{ ByteArrayInputStream is =new ByteArrayInputStream(in);
return is; }
public static String InputStreamTOString(InputStream in)throws Exception{
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
byte[] data = new byte[in.available()];
int count=in.read(data,0,data.length);
if((count !=-1))
{
outstream.write(data, 0, count);
}
data = null;
String test =new String(outstream.toByteArray(),"ISO-8859-1");
System.out.println(test);
return test;
}
public static byte[] Stringtobyte(String in)throws Exception{
InputStream is =StringToInputStream(in);
return InputStreamToByte(is);
}
public static InputStream StringToInputStream(String in)throws Exception{
ByteArrayInputStream is =new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
return is;
}
public static byte[] InputStreamToByte(InputStream in)throws Exception{
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
byte[] data=new byte[size];
int count =-1;
while((count = in.read(data,0,size))!=-1)
{
outstream.write(data, 0, count);
}
data = null;
byte[] tt=outstream.toByteArray();
File file=new File("");
FileImageOutputStream pic_out= new FileImageOutputStream(file);
BufferedImage img=new BufferedImage(105,80,BufferedImage.TYPE_3BYTE_BGR);
ImageIO.write(img, "jpg", outstream);
return outstream.toByteArray(); }

之后会补一篇加载tiff和dwg的文章,这些格式都是gis系统经常用到的,虽然拿FME来处理也不错,那玩意正版要几十万,还是算了。

最新文章

  1. 安卓虚拟机adb shell sqlite3数据库
  2. 基础2.Jquery过滤选择器
  3. 转载(sublime text 2 调试python时结果空白)
  4. Win+R命令大全
  5. DBLINK 创建的注意事项
  6. C++ 中的形参与返回值
  7. 编写高质量JS代码的68个有效方法(七)
  8. openstack context
  9. cocos2d-x Lua与OC互相调用
  10. poj 4982 踩方格
  11. Vmware Tools 下载及安装方法
  12. Vue.js简单的应用
  13. bootstrap 导航栏鼠标悬停显示下拉菜单
  14. 【转】Python 爬虫的工具列表【预】
  15. Java开发笔记(三十二)字符型与整型相互转化
  16. MySQL 列出存储过程
  17. Python:Day27 socketserver、线程
  18. su命令详解
  19. 基本数据类型转String,String转基本数据类型
  20. 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★

热门文章

  1. UNIX 和 LINUX
  2. 记录一些容易忘记的属性 -- UINavigationController
  3. Hadoop学习资料
  4. 几款值得推荐的android(安卓)开源框架简介
  5. 10-20日 && 抽签问题
  6. MySQL数据库1 - 基本概念及安装
  7. 黑马程序员——【Java高新技术】——JDK1.5新特性:静态导入、可变参数、增强型for循环、自动装箱拆箱、枚举
  8. 对编写html代码的几点儿小建议
  9. Startup key combinations for Intel-based Macs
  10. java中Timer的使用