代码改变世界

ORM映射框架总结--Flash 处理

2010-01-02 14:34  贺臣  阅读(931)  评论(0编辑  收藏  举报
代码
  1 /**
  2  * 
  3  * 2009-4-17
  4  * 
  5  * 
  6  * 根据flash读取其宽度和高度
  7  * */
  8 using System;
  9 using System.Collections.Generic;
 10 using System.Linq;
 11 using System.Text;
 12 using System.IO;
 13 using System.Collections;
 14 
 15 namespace CommonData.Flash
 16 {
 17     [Serializable]
 18     public partial class FlashInfo
 19     {
 20         private int width, height, version, frameCount, fileLength;
 21         private float frameRate;
 22         private bool isCompressed;
 23 
 24         public FlashInfo(string filename)
 25         {
 26             if (!File.Exists(filename))
 27             {
 28                 throw new FileNotFoundException(filename);
 29             }
 30             FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
 31             BinaryReader reader = new BinaryReader(stream);
 32             try
 33             {
 34                 if (stream.Length < 8)
 35                 {
 36                     throw new InvalidDataException("文件不是 Flash 文件格式");
 37                 }
 38                 string flashMark = new string(reader.ReadChars(3));
 39                 if (flashMark != "FWS" && flashMark != "CWS")
 40                 {
 41                     throw new InvalidDataException("文件不是 Flash 文件格式");
 42                 }
 43                 isCompressed = flashMark == "CWS";
 44                 version = Convert.ToInt32(reader.ReadByte());
 45                 fileLength = reader.ReadInt32();
 46                 byte[] dataPart = new byte[stream.Length - 8];
 47                 reader.Read(dataPart, 0, dataPart.Length);
 48                 MemoryStream dataStream = new MemoryStream(dataPart);
 49                 try
 50                 {
 51                     if (isCompressed)
 52                     {
 53                         MemoryStream outStream = new MemoryStream();
 54                         zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outStream);
 55                         CopyStream(dataStream, outZStream);
 56                         outStream.Position = 0;
 57                         ProcessCompressedPart(outStream);
 58                         outZStream.Close();
 59                         outStream.Close();
 60                     }
 61                     else
 62                         ProcessCompressedPart(dataStream);
 63                 }
 64                 finally
 65                 {
 66                     dataStream.Close();
 67                 }
 68             }
 69             finally
 70             {
 71                 reader.Close();
 72                 stream.Close();
 73             }
 74         }
 75 
 76         private void ProcessCompressedPart(MemoryStream stream)
 77         {
 78             BinaryReader reader = new BinaryReader(stream);
 79             try
 80             {
 81                 byte[] rect;
 82                 int nbits, totalBits, totalBytes;
 83                 nbits = reader.ReadByte() >> 3;
 84                 totalBits = nbits * 4 + 5;
 85                 totalBytes = totalBits / 8;
 86                 if (totalBits % 8 != 0)
 87                 {
 88                     totalBytes++;
 89                 }
 90                 reader.BaseStream.Seek(-1, SeekOrigin.Current);
 91                 rect = reader.ReadBytes(totalBytes);
 92                 frameRate = float.Parse(string.Format("{1}.{0}", reader.ReadByte(), reader.ReadByte()));
 93                 frameCount = Convert.ToInt32(reader.ReadInt16());
 94                 BitArray bits = new BitArray(rect);
 95                 bool[] reversedBits = new bool[bits.Length];
 96                 for (int i = 0; i < totalBytes; i++)
 97                 {
 98                     int count = 7;
 99                     for (int j = 8 * i; j < 8 * (i + 1); j++)
100                     {
101                         reversedBits[j + count] = bits[j];
102                         count -= 2;
103                     }
104                 }
105                 bits = new BitArray(reversedBits);
106                 StringBuilder sbField = new StringBuilder(bits.Length);
107                 for (int i = 0; i < bits.Length; i++)
108                     sbField.Append(bits[i] ? "1" : "0");
109                 string result = sbField.ToString();
110                 string widthBinary = result.Substring(nbits + 5, nbits);
111                 string heightBinary = result.Substring(3 * nbits + 5, nbits);
112                 width = Convert.ToInt32(FlashInfo.BinaryToInt64(widthBinary) / 20);
113                 height = Convert.ToInt32(FlashInfo.BinaryToInt64(heightBinary) / 20);
114             }
115             finally
116             {
117                 reader.Close();
118             }
119         }
120 
121         private static long BinaryToInt64(string binaryString)
122         {
123             if (string.IsNullOrEmpty(binaryString))
124                 throw new ArgumentNullException();
125             long result = 0;
126             for (int i = 0; i < binaryString.Length; i++)
127             {
128                 result = result * 2;
129                 if (binaryString[i] == '1')
130                     result++;
131             }
132             return result;
133         }
134 
135         public int Width
136         {
137             get
138             {
139                 return this.width;
140             }
141         }
142 
143         public int Height
144         {
145             get
146             {
147                 return this.height;
148             }
149         }
150 
151         public int FileLength
152         {
153             get
154             {
155                 return this.fileLength;
156             }
157         }
158 
159         public int Version
160         {
161             get
162             {
163                 return this.version;
164             }
165         }
166 
167         public float FrameRate
168         {
169             get
170             {
171                 return this.frameRate;
172             }
173         }
174 
175         public int FrameCount
176         {
177             get
178             {
179                 return this.frameCount;
180             }
181         }
182 
183         public bool IsCompressed
184         {
185             get
186             {
187                 return this.isCompressed;
188             }
189         }
190 
191         public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
192         {
193             byte[] buffer = new byte[2000];
194             int len;
195             while ((len = input.Read(buffer, 02000)) > 0)
196             {
197                 output.Write(buffer, 0, len);
198             }
199             output.Flush();
200         }
201     }
202 }
203 

 

  这个类用于处理Flash文件的,可以获得falsh 的一些相关信息。这里需要一个插件的处理 zlib.net.dll。 可以到网上去下载

  


作者:情缘
出处:http://www.cnblogs.com/qingyuan/
关于作者:从事仓库,生产软件方面的开发,在项目管理以及企业经营方面寻求发展之路
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
联系方式: 个人QQ  821865130 ; 仓储技术QQ群 88718955,142050808 ;
吉特仓储管理系统 开源地址: https://github.com/hechenqingyuan/gitwms