zoukankan      html  css  js  c++  java
  • Flex 加载 wmf,svg

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

    转svg。 代码如下

     1 /**
     2      * 将wmf转换为svg
     3      * 
     4      * @param src
     5      * @param dest
     6      */
     7     public  String wmfToSvg(InputStream insrc) {
     8         boolean compatible = false;
     9         //String resultString="";
    10         try {
    11             //InputStream in = new FileInputStream(src);
    12             WmfParser parser = new WmfParser();
    13             final SvgGdi gdi = new SvgGdi(compatible);
    14             parser.parse(insrc, gdi);    
    15             org.w3c.dom.Document doc = gdi.getDocument();
    16             StringWriter strwite=new StringWriter();
    17         
    18             output(doc, strwite);
    19             return strwite.getBuffer().toString();
    20             // return resultString;
    21              //getBuffer().toString();
    22         } catch (Exception e) {
    23             return e.getMessage();
    24         }
    25     }
    26     
    27     private  void output(org.w3c.dom.Document doc, StringWriter out) throws Exception {
    28         TransformerFactory factory = TransformerFactory.newInstance();
    29         Transformer transformer = factory.newTransformer();
    30         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    31         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    32         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    33         transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
    34                 "-//W3C//DTD SVG 1.0//EN");
    35         transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
    36                 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
    37         transformer.transform(new javax.xml.transform.dom.DOMSource(doc), new StreamResult(out));
    38         out.flush();
    39         out.close();
    40     }

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

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

     这是改为png的流输出

     1 BASE64Encoder encode=new BASE64Encoder();
     2 byte[] WMF= houserow.getBlob("WMF");                
     3                     if(WMF!=null&&WMF.length>0)
     4                     {
     5                         String contentString =bytetoString(WMF);
     6                         InputStream wmfStream=new StringBufferInputStream(contentString);
     7                         String svgstr=wmfToSvg(wmfStream);
     8                         ByteArrayOutputStream strwite=new ByteArrayOutputStream();     
     9                         convertToPng(svgstr,strwite);
    10                         byte[]lens=strwite.toByteArray();
    11                         String WMF_pic="";
    12                         if(svgstr != null&&!svgstr.equals(""))
    13                         {
    14                             WMF_pic=encode.encode(lens);
    15                         }    
    16                         createKeyPairXML(ke12,"WMF","户型图",WMF_pic,"");    
    17                         
    18                     }
    19 
    20     
    21     
    22     /**
    23      * 将svgCode转换成png文件,直接输出到流中
    24      * 
    25      * @param svgCode svg代码
    26      * @param outputStream 输出流
    27      * @throws TranscoderException 异常
    28      * @throws IOException io异常
    29      */
    30     public  void convertToPng(String svgCode, OutputStream outputStream)
    31             throws TranscoderException, IOException {
    32         try {
    33             byte[] bytes = svgCode.getBytes("ISO-8859-1");
    34             PNGTranscoder t = new PNGTranscoder();     
    35            
    36             TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
    37             TranscoderOutput output = new TranscoderOutput(outputStream);
    38             t.transcode(input, output);
    39             outputStream.flush();
    40         } finally {
    41             if (outputStream != null) {
    42                 try {
    43                     outputStream.close();
    44                 } catch (IOException e) {
    45                     e.printStackTrace();
    46                 }
    47             }
    48         }
    49     }
    50     
    51 
    52     public static String bytetoString(byte[] in)throws Exception{
    53         InputStream is =byteTOInputStream(in);
    54         return InputStreamTOString(is);
    55     }
    56     public static InputStream byteTOInputStream(byte[] in)throws Exception{
    57     
    58         ByteArrayInputStream is =new ByteArrayInputStream(in);
    59         return is;
    60         
    61     }
    62     public static String InputStreamTOString(InputStream in)throws Exception{
    63         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
    64         byte[] data = new byte[in.available()];
    65         int count=in.read(data,0,data.length);
    66         if((count !=-1))
    67         {
    68             outstream.write(data, 0, count);
    69         }
    70         data = null;
    71         String test =new String(outstream.toByteArray(),"ISO-8859-1");
    72         System.out.println(test);
    73         return test;    
    74     }
    75     public static byte[] Stringtobyte(String in)throws Exception{
    76         InputStream is =StringToInputStream(in);
    77         return InputStreamToByte(is);
    78     }
    79     public static InputStream StringToInputStream(String in)throws Exception{
    80         ByteArrayInputStream is =new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
    81         return is;
    82     }
    83     public static byte[] InputStreamToByte(InputStream in)throws Exception{
    84         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
    85         byte[] data=new byte[size];
    86         int count =-1;
    87         while((count = in.read(data,0,size))!=-1)
    88         {
    89             outstream.write(data, 0, count);
    90         }
    91         data = null;
    92         byte[] tt=outstream.toByteArray();
    93         File file=new File("");
    94         FileImageOutputStream pic_out= new FileImageOutputStream(file);
    95         BufferedImage img=new BufferedImage(105,80,BufferedImage.TYPE_3BYTE_BGR); 
    96         ImageIO.write(img, "jpg", outstream);
    97         return outstream.toByteArray();
    98         
    99     }

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

  • 相关阅读:
    LeetCode题解之Flipping an Image
    LeetCode 之Find Minimum in Rotated Sorted Array
    LeetCode题解Transpose Matrix
    LeetCode 题解之Minimum Index Sum of Two Lists
    LeetCode题解之Intersection of Two Linked Lists
    LeetCode 题解之Add Two Numbers II
    LeetCode题解之Add two numbers
    href="#"与href="javascript:void(0)"的区别
    有关ie9 以下不支持placeholder属性以及获得焦点placeholder的移除
    ie7下属性书写不规范造成的easyui 弹窗布局紊乱
  • 原文地址:https://www.cnblogs.com/haibalai/p/5029607.html
Copyright © 2011-2022 走看看