java swf 缩略图_获取swf的缩略图

/*Darron Schall (darron@darronschall.com)

July 7th, 2003

File: SWFHeaderInfo.cs

Note: Some ideas for this code were used from the flasm

source code. Seehttp://flasm.sourceforge.netfor more information on flasm.

Requires: This file requires "SharpZipLib", found athttp://www.icsharpcode.net/OpenSource/SharpZipLib/for

decompressing compressed .swf files with the ZLib algorithm.

Description:

See the summary for the SWFHeaderInfo class*/

using System;

using System.IO;

namespace FlashToImage

{

//SWFHeaderInfo is a class that exposes as properties the contents of///a .swf file header.  The path to the .swf file is passed into///the constructor.  The Status property is useful for///determining if the input was valid or not.  It is either "OK" if///the swf header was successfully read, or an error message otherwise.//SWFHeaderInfo works for both compressed and uncompressed .swf  files.///    public class SWFHeaderInfo

{

private byte bitPos; //bitPos is our "position" in out bitBuffer "array", valid values are 0-8 (8 means get a new buffer)        private byte bitBuffer;  //this is a byte that we'll be treating like an array        uint nBits; //the number of bit used to store the data for the RECT type storing//the movies xMin, xMax, yMin, and yMax values        private ICSharpCode.SharpZipLib.Zip.Compression.Inflater zipInflator; //used to decompressed swf        Stream swf; //either a FileStream or MemoryStream//private internal data values        private byte mVersion;

private uint mLength;

private uint mxMin;

private uint mxMax;

private uint myMin;

private uint myMax;

private ushort mFrameRate;

private ushort mFrameCount;

private bool mCompressed;

private string mStatus;

//public read-only (because no "set" defined) properties        public byte Version

{

get

{

return mVersion;

}

}

public uint Length

{

get

{

return mLength;

}

}

public uint xMin

{

get

{

return mxMin;

}

}

public uint xMax

{

get

{

return mxMax;

}

}

public uint yMin

{

get

{

return myMin;

}

}

public uint yMax

{

get

{

return myMax;

}

}

public ushort FrameRate

{

get

{

return mFrameRate;

}

}

public ushort FrameCount

{

get

{

return mFrameCount;

}

}

public bool Compressed

{

get

{

return mCompressed;

}

}

public string Status

{

get

{

return mStatus;

}

}

public SWFHeaderInfo(string path)

{

char first; //C if compressed, F otherwise//initialize            mVersion = 0;

mLength = 0;

mxMin = 0;

mxMax = 0;

myMin = 0;

mxMax = 0;

mFrameRate = 0;

mFrameCount = 0;

mCompressed = false;

mStatus = "No input specified";

//attempt to open the swf file            try

{

swf = new FileStream(path, FileMode.Open);

}

catch (Exception e)

{

mStatus = "Error opening swf file";

//We don't need to close the file here because there//was a problem opening it.//swf.Close();                return;

}

bitPos = 8; //set out bitPos to be "out of bounds" so the first call to getBits reads in//a new byte from the file (the first byte)//try to read the first byte of the file            try

{

first = (char)getBits(swf, 8);

}

catch (Exception e)

{

mStatus = "Error reading first byte file";

//close the swf file on error - 08/04/03                swf.Close();

return;

}

if (first == 'C')

{

//compressed swf file                mCompressed = true;

swf = decompressSwf(swf);

if (mStatus == "Error decompressing swf file")

{

//trouble decompressing.. bail out! - 08/04/03                    swf.Close();

return;

}

//reset our "array" index so the first call to getBits reads a new byte                bitPos = 8;

//attempt to get the first byte of the file                try

{

first = (char)getBits(swf, 8);

}

catch (Exception e)

{

mStatus = "Error reading first byte file";

//close the swf file on error - 08/04/03                    swf.Close();

return;

}

}

//wrapped everything in a try/catch block "just in case" for//a little better error handling            try

{

//make sure the first 3 bytes are "FWS"                if (first != 'F' || (char)getBits(swf, 8) != 'W' || (char)getBits(swf, 8) != 'S')

{

//not a swf file!                    mStatus = "File specified does not seem to be a valid .swf file";

//close the swf file on error - 08/04/03                    swf.Close();

return;

}

//start collecting informatin                mVersion = (byte)getBits(swf, 8);

mLength = getDoubleWord(swf);

nBits = getBits(swf, 5);

mxMin = getBits(swf, nBits);

mxMax = getBits(swf, nBits) / 20;

myMin = getBits(swf, nBits);

myMax = getBits(swf, nBits) / 20;

mFrameRate = (ushort)(swf.ReadByte() <

mFrameCount = (ushort)(getWord(swf));

mStatus = "OK";

}

catch (Exception e)

{

mStatus = "Error reading .swf header information";

return;

}

swf.Close();

}

//corrections for flash storing the byte values in little-endian format//ported from the flasm source code        private uint getWord(Stream f)

{

return (uint)(f.ReadByte() | f.ReadByte() <

}

//corrections for flash storing the byte values in little-endian format//ported from the flasm source code        private uint getDoubleWord(Stream f)

{

return getWord(f) | (getWord(f) <

}

//idea for this function borrowed from the flasm source code, but//the function code is my creation        private uint getBits(Stream f, uint n)

{

uint returnbits = 0;

while (true)

{

//do we have more bits to read?                if (n >= 1)

{

//yes, more bits to read....  do we have unread bits in our buffer?                    if (bitPos == 8)

{

//no more bits to read in our buffer, lets get a new//buffer from f and reset the bitPos                        bitPos = 0;

bitBuffer = (byte)f.ReadByte(); //read 8 bits at a time                    }

//if we're here, we have more bits to read and//we have unread bits in the buffer

returnbits <<= 1; //shift the returnbit value left 1 place

byte bitMask = (byte)(0x80 >> bitPos);

//determine if the next bit we add to return bits should//be a 1 or a 0, based on the value of applying//the bitMask to the bitBuffer.  A quick example://bitBuffer = 01011010//bitmask =   00001000//~~~~~~~~~~~~~~~~~~~~  anding the bits together yeilds://00001000//and because the result is equal to the bitmask, we add//a 1 to the returnbits value.

returnbits |= (bitBuffer & bitMask) == bitMask ? (uint)1 : (uint)0;

n -= 1; //one less bit to read                    bitPos += 1; //advance our "array" index                }

else

{

//no more bits to read, return what we read from f                    return returnbits;

}

}

}

private Stream decompressSwf(Stream swf)

{

//seek to after the 4th byte and read in 4 bytes to determine the//size that the buffer needs to be to uncompressed the swf file            swf.Seek(4, SeekOrigin.Begin);

uint bufferSize = getDoubleWord(swf);

//the uncompressedSwf byte array will be used as a MemoryStream            byte[] uncompressedSwf = new byte[bufferSize];

//only after the 8th bit is the ZLib compresseion applied, so just//copy the first 8 bits from the compressed swf to the uncompressed swf            swf.Seek(0, SeekOrigin.Begin);

swf.Read(uncompressedSwf, 0, 8);

//set the first byte to be 'F' instead of 'C'            uncompressedSwf[0] = (byte)0x46;

zipInflator = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();

//because the zipInflator takes a byte array, not a FileStream,//we need to declare a temporary byte array to use as the//input for inflation            byte[] tmpSwf = new byte[bufferSize - 8];

//read the rest of the swf into our temporary byte array            swf.Read(tmpSwf, 0, (int)(bufferSize - 8));

//close the swf on disk because we have enough information in memory now            swf.Close();

zipInflator.SetInput(tmpSwf);

if (zipInflator.Inflate(uncompressedSwf, 8, (int)(bufferSize - 8)) == 0)

{

mStatus = "Error decompressing swf file";

}

//change our swf from a FileStream to a MemoryStream now, using the uncompressed swf data            return new MemoryStream(uncompressedSwf);

}

/*[STAThread]

public static void Main()

{

SWFHeaderInfo s = new SWFHeaderInfo("C:\\Development\\SWFHeaderInfo\\test.swf");

Console.WriteLine(s.Status);

if (s.Status == "OK")

{

Console.WriteLine("Version: " + s.Version);

Console.WriteLine("Length: " + s.Length);

Console.WriteLine("xMin: " + s.xMin);

Console.WriteLine("xMax: " + s.xMax);

Console.WriteLine("yMin: " + s.xMin);

Console.WriteLine("yMax: " + s.yMax);

Console.WriteLine("FrameRate: " + s.FrameRate);

Console.WriteLine("FrameCount: " + s.FrameCount);

Console.WriteLine("Compressed: " + s.Compressed);

}

// keep the input on the screen

Console.Read();

}*/

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
万能文件查看器"可以查看的文件: 1、各种版本的swf(flash5/6/7/8/mx)动画; 2、jpg、bmp、gif、psd图片。 3、mpg、dat、avi、asf、wmv、wav、mp3、rm、rmvb、VOB等各种声音/MPG1/MPG2/流媒体文件等(其中rm、rmvb需用户安装reaplayer10.5版,本站有下。) 3、txt、rtf、doc、xls、pps、ppt等office文档和asp网页源文件等; 4、调用exe、chm及快捷方式等。 以下情况安装"万能文件查看器"更有用: 1、电脑中没装FLASH,打不开SWF动画;或者某些SWF由于版本问题而打不开;甚至还有可能运行了低版本的exe可执行swf动画,高版本的就再也打不开了。 2、没装媒体播放器;或者嫌mediaplayer播放器启动慢,运行慢;或者mediaplay等版本太低,打不开某些媒体文件(如不能播放DVD的vob文件); 3、没装acdsee等看图软件;或者图片的打开方式被改成了photoshop,一打开图片就运行photoshop,太慢;或者嫌acdsee等看图软件太大。 如果你经常遇到以上情况,那么建议你安装"万能文件查看器"。 安装"万能文件查看器"你相当于安装了"动画/图片/各种mpg1、mpg2/网络流媒体/mp3"等多种播放器!软件相当小!仅三兆多!并且,绝对免费! 安装方法: 1、如果以前曾经安装过"万能文件查看器",请删去程序(默认安装目录为:c:\万能文件查看器)目录。 2、请双击安装“real解码器.exe”,安装此编码器可以使本软件支持rm和rmvb格式的影像文件(如果你的计算机上安装了realplay10以上版本可以不安装此插件)。 3、请双击安装“setup.exe”,这是"万能文件查看器"的安装文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值