⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swftosvg.java

📁 wmftosvg图片转换源代码。使用前需要下载相关lib先。
💻 JAVA
字号:
package SWF2SVG;

// Copyright 1997, 1998,1999 Carmen Delessio (carmen@blackdirt.com)
// Black Dirt Software http://www.blackdirt.com/graphics
// Free for non-commercial use

// revisions:
// May 12,1999 corrected bad URL http://www.w3.org 
// Corrected using <desc> as placeholders by embedding in their own <g>



import java.io.*;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.net.*;
import java.applet.Applet;
import java.lang.Math;
import java.lang.String;
import java.awt.image.*;

class  swftosvg  {


  
      private int i;
    
    
     
      private int windowLong;
      private short windowInt;

      public int inch;
     
      private java.util.Vector MetaRecordVector;
 
      private DataInputStream d;

  
      public WMFHandleTable ht;
     
      
      byte[] byteData;                // Unpacked data: one byte per pixel
      byte[] rawData;                 // the raw unpacked data
      int[] intData;                  // Unpacked data: one int per pixel

      public WMFToolkit cvtTool = new WMFToolkit();


      private String svgGraphic = new String("");
      

      
      public short WMFWidth;
      public short WMFHeight;

      private short htmlWidth = 800;
      private short htmlHeight = 600;


      public short logExtX;
      public short logExtY;

      public short logOrgX = 0;
      public short logOrgY = 0;


      public short devExtX;
      public short devExtY;


      public int AppletWidth;
      public int AppletHeight;

      public  float d_x;
      public  float d_y;
      public  Graphics wmfGraphics;              
      public  Image wmfImageBuffer;                  

      public int fontStyle;
      public int fontWeight;
      public boolean fontItalic = false;

      public      int penRed =0;
      public      int penGreen =0;
      public      int penBlue =0;

      public      int textRed =0;
      public      int textGreen =0;
      public      int textBlue =0;
      String textColor = "000000";
      String penColor = "000000";
      List ls=new ArrayList();
      public boolean styleSet = false;
      DataInputStream f;
  
  public  swftosvg(InputStream is, PrintStream os,List ls) throws IOException
  {


       // mRecord = new MetaRecord( windowLong, windowInt, null);
         d = new DataInputStream(is);
         this.ls=ls;
         parseit();
     if (styleSet){
         svgGraphic += "</g>";
      }
        svgGraphic += "</svg>\n";
    // System.out.println(svgGraphic);
       os.println(svgGraphic);
       System.out.println("ok is over");
  }

 // gets all wmf records into a hash table
  public synchronized void  parseit()throws IOException {

       byte[] f_long =new byte[4];
       byte[] f_int = new byte[2];
       byte[] parmBuffer;
       short x;
       short y;
       short x2;
       short y2;
       short count = 0;
       MetaRecord locMetaRecord;

//begin metafile header
//注意.这里两个Byte表示一个字.看来图片还是16位处理器下产生的.
        windowLong = readLong(d); //key  4 bytes
       
        if (windowLong == -1698247209){
          //检测的是头文件中标准部分.第一个是双字,必须为这个
       //如果不是则说明头不存在标准部分.
           windowInt = readInt(d); // unused
           x2 = readInt(d);
           y2 = readInt(d);
           x = readInt(d);
           y = readInt(d);//这四个字读入是bbox,放映了图片的大小
           WMFWidth = (short) Math.abs(x2-x);
           WMFHeight = (short) Math.abs(y2-y);
           //定义中inch是word型.应该是四个字节.
            windowInt = readInt(d); // inch
           
         //   System.out.println(  windowInt );
            inch = windowInt;
         //   System.out.println( cvtTool.getScreenResolution());
 
           x = (short)(inch/cvtTool.getScreenResolution());  //pixels per inch
           if (x < 1){
             x =1;
           }
        //   System.out.println("x="+x);

           WMFWidth = (short)(WMFWidth /x);
           WMFHeight = (short)(WMFHeight/x);

           // based on bounded bo x- nnox consideration of map modes

           svgGraphic = "<?xml version = \"1.0\" standalone = \"yes\"?>\n";
           svgGraphic = svgGraphic + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG April 1999//EN\"\n \"http://www.w3.org/Graphics/SVG/svg-19990412.dtd\">\n";
           svgGraphic = svgGraphic + "<svg width = \"" + WMFWidth +"px\" height=\"" + WMFHeight+"px\">\n";


           htmlWidth = WMFWidth;
           htmlHeight = WMFHeight;

           devExtX = htmlWidth;
           devExtY = htmlHeight;

           x = cvtTool.twip2pixel(x);  // add inch stuff here
           y = cvtTool.twip2pixel(y);

           windowLong = readLong(d); // reserved,没有使用,必须为0.
          
           windowInt = readInt(d);// checksum 头中前十个字总和.
         
           //metaheader

           for ( i = 0; i < 3; i++){
            windowInt = readInt(d);
          //版本信息,头文件大小及类型全部省略.
           }
           windowLong = readLong(d);
        
           windowInt = readInt(d);
         
           windowLong = readLong(d);
     
           windowInt = readInt(d);//头文件全部读取完毕.
    
        }
        else {
        // not placeable- no metafile header
        // already read long to check for key

        windowInt = readInt(d);
        windowLong = readLong(d);
        windowInt = readInt(d);
        windowLong = readLong(d);
        windowInt = readInt(d);
        
        //如果没有前面的标准头部,则全部读完metaheader.

        }


        MetaRecordVector = new java.util.Vector();
        Vector   dclist =new Vector();
        int index=0;
        
        Stack dcstack=new Stack();
        dc olddc=new dc(ls);
        while(true){
           count++;
           System.out.println("*******");
           try{
                 d.readFully(f_long);//读入记录大小,以双字表示
              }
           catch(EOFException e){ System.out.println("**** eof");break;}
            //这里是wmf文件特点?
            //     if (bytes_read == -1)
            //	     break;
    try{      windowLong = flipLong(f_long);
            //反转读取的记录大小到整形.这里是双字大小.
              windowInt = readInt(d);
           //读取一个字表示记录类型META_XXX
            //记录类型如果为0则为空记录,跳出.
    }
    catch(Exception e){
    	 System.out.println("eof");break;
    }
          if (windowLong >= 3){
             windowLong = (windowLong*2) -6;
            }//通过记录大小来判断下面将要读取的字节个数.
        //  System.out.println(windowLong);
           parmBuffer = null;
           parmBuffer = new byte[windowLong];
           d.readFully(parmBuffer);
           locMetaRecord=new MetaRecord( windowLong, windowInt, parmBuffer);
          // System.out.println("ok");
           MetaRecordVector.addElement(locMetaRecord);
          if(windowInt== 0x1E){
    //   System.out.println("sav"+parmBuffer);
         	  olddc.addlist(MetaRecordVector);
         	  olddc.setsvgGraphic();
         	 // dclist.add(index, olddc);
         	  svgGraphic+=olddc.change();
              dcstack.push(olddc);
         	  olddc= new dc(ls);
         	  MetaRecordVector=new java.util.Vector();
         	
            }
          if(windowInt== 0x127){
    //   System.out.println("res"+parmBuffer[1]);
                olddc=(dc) dcstack.pop();
         	    olddc.addlist(MetaRecordVector);
         	    olddc.setsvgGraphic();
         	//   System.out.println(MetaRecordVector.size());
         	    svgGraphic+=olddc.change();
        	  
        	    olddc=(dc) dclist.elementAt(index);
        	    
        	    MetaRecordVector=new java.util.Vector();
     
        	 // olddc=(dc)dclist.get(index-1);
       /*   if(MetaRecordVector.capacity()!=0)
        	  {  
        	//	  ht=(WMFHandleTable)dclist.get(index).getht();
        		  olddc.setsvgGraphic();
        	//	  int  indexrecord=dclist.get(index).getindex();
        		  olddc.setht(ht,indexrecord);
        		  olddc.addlist(MetaRecordVector);
        	      svgGraphic+=olddc.change();
        	   }*/
        	  
           }
       if (windowInt == 0){
    	       System.out.println(index);
    	       olddc.setsvgGraphic();
        	   olddc.addlist(MetaRecordVector);
        	   svgGraphic+=olddc.change();
        	   styleSet =olddc.getstyset();
               break;
               }
         }
}


  // interprets each record
  public int flipLong(byte[] byteFlip){
      DataInputStream dl;
      ByteArrayInputStream  b_in;
      byte[] bytebuffer;
      bytebuffer = new byte[4];
      bytebuffer[0] = byteFlip[3];
      bytebuffer[1] = byteFlip[2];  
      bytebuffer[2] = byteFlip[1];  
      bytebuffer[3] = byteFlip[0];  

      b_in = new ByteArrayInputStream(bytebuffer);
      dl = new DataInputStream(b_in);
      try{
        return dl.readInt();
      }
       catch(IOException e){System.err.println(e);}
         return 0;

}


public int readLong( DataInputStream d){
    byte[] longBuf =new byte[4];

     try{
         d.read(longBuf);
//         d.readFully(longBuf);
         return flipLong(longBuf);
      }  catch(IOException e){
         System.err.println(e);
         return 99;
      }

}

public short readInt( DataInputStream d){
     byte[] intBuf =new byte[2];

     try{
         d.read(intBuf);
//         d.readFully(intBuf);
         return flipInt(intBuf);
      }  catch(IOException e){
         System.err.println(e);
         return 99;
      }

}



public short flipInt( byte[] byteFlip){
      DataInputStream d;
      ByteArrayOutputStream b_out;
      ByteArrayInputStream  b_in;
      byte[] bytebuffer;

      bytebuffer = new byte[2];
      bytebuffer[0] = byteFlip[1];
      bytebuffer[1] = byteFlip[0];  

      b_in = new ByteArrayInputStream(bytebuffer);
      d = new DataInputStream(b_in);
      try{
        return d.readShort();
      }
      catch(IOException e){ System.err.println(e);}
         return 0;
     }

public void setls(List ls){
	this.ls=ls;
}
  public static void main(String args[]) throws IOException{ 

  swftosvg wmf = null;
  File source_file;
  File dest_file;
  FileInputStream source = null;
  FileOutputStream destination = null;
  PrintStream dataOut = null;
  DataInputStream dataIn;

  /* if ((args.length == 0) || (args.length >2)){
             System.out.println ("Useage: java wmf2svg <source file><destination file>");       
             System.exit(0);
   }*/

//   source_file = new File(args[0]);
      source_file = new File("e:/dd.wmf");
      dest_file = new File("e:/dd.svg");

       source = new FileInputStream(source_file);
    //   source = new FileInputStream("e:/lion.wmf");
       destination = new FileOutputStream(dest_file);
    // destination = new FileOutputStream("e:/lion.svg");
       dataOut = new PrintStream(destination);
       dataIn = new DataInputStream(source);
      ;
       poptd po=null;
       try{
       po=   new poptd(dataIn);}
       catch(Exception e){
    	   System.out.println("is rong1");
       }
      
   // get it to input strream and output stream here
   // make the

   //?? make is stage WMF2SVG.read
   // WMF2SVawrite
       List ls=null;
     try{
        ls=po.getls();
   
        dataIn.close();
        source.close();
        }
     catch(Exception e){
    	 System.out.println("is rong2");
     }
     DataInputStream dataIn2 = null ;
     source = new FileInputStream(source_file);
     try{
     dataIn2 = new DataInputStream(source);
     }
     catch(Exception e){
    	 System.out.println("is rong");
     }
     try{
     wmf = new  swftosvg(dataIn2,dataOut,ls);}
     catch(Exception e){
    	 System.out.println("is rong3");
     }

   
  }
  
 






} //end parsewmf

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -