纯 AS3 将图片转换为 SWF(转自同行“古树悬叶”的博客)

比较简单方便的通过纯 AS3 代码将图片转换为 SWF 格式,可以免去 SWFTools 需要通过命令行将 *.jpg / *.png 之类的文件转换成 *.swf。

package
{
     import flash.display.Bitmap;
     import flash.display.BitmapData;
     import flash.display.JPEGEncoderOptions;
     import flash.display.Loader;
     import flash.display.LoaderInfo;
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.net.FileReference;
     import flash.net.URLRequest;
     import flash.utils.ByteArray;
     import flash.utils.Endian;

     public  class Image2Swf  extends Sprite
     {
         private  var isCompress :Boolean  = true; //是否为压缩格式的SWF

         private  var loader :Loader;
         private  var bitmapData :BitmapData;

         public  function Image2Swf ( )
         {
            loader  =  new Loader ( );
            loader.contentLoaderInfo.addEventListener (Event.INIT, initHandler );
            loader.load ( new URLRequest ( "test.jpg" ) );
         }

         private  function initHandler (e :Event ) : void
         {
            e.target.removeEventListener (Event.INIT, initHandler );

             var bitmap :Bitmap  = e.target.content as Bitmap;

             if (bitmap )
             {
                bitmapData  = bitmap.bitmapData;

                 var byteArray :ByteArray  =  new ByteArray ( );

                bitmapData.encode (bitmapData.rect,  new JPEGEncoderOptions ( ), byteArray )

                loader  = null;
                loader  =  new Loader ( );
                loader.contentLoaderInfo.addEventListener ( Event.COMPLETE, completeHandler  );
                loader.loadBytes (byteArray );        
             }
         }

         private  function completeHandler (e :Event ) : void
         {
            e.target.removeEventListener ( Event.COMPLETE, completeHandler  );

             if (isCompress )
             {
                 //保存为压缩格式的SWF
                saveContentToSWF_compress ( e  );
             }
             else
             {
                 //保存为非压缩格式的SWF
                saveContentToSWF ( e  );
             }
            loader.unload ( );
            loader  = null;
            bitmapData.dispose ( );
            bitmapData  = null;
         }

         //压缩格式
         private  function saveContentToSWF_compress ( e :Event  ) : void
         {
             var swf_head :ByteArray  =  new ByteArray ( );
                swf_head.endian  = Endian.LITTLE_ENDIAN;
                swf_head.writeBytes ( (e.target as LoaderInfo ).bytes,  08  );
                swf_head [ 0 ]  = 0x43;  // 'C'; 
                swf_head.position  =  0;

             var swf_body :ByteArray  =  new ByteArray ( );
                swf_body.endian  = Endian.LITTLE_ENDIAN;
                swf_body.writeBytes ( (e.target as LoaderInfo ).bytes,  8  );
                swf_body.position  =  0;
                swf_body.compress ( );
                swf_body.position  =  0;

             var swf_ByteArray :ByteArray  =  new ByteArray ( ); //包含 head 与 body 的二进制

                swf_ByteArray.writeBytes (swf_head );
                swf_ByteArray.writeBytes (swf_body );

             var file :FileReference  =  new FileReference ( );
                file.save (swf_ByteArray,  "压格式的SWF.swf" ); //SWF head CWS
         }

         //非压缩格式
         private  function saveContentToSWF ( e :Event  ) : void
         {
             var file :FileReference  =  new FileReference ( );
                file.save ( (e.target as LoaderInfo ).bytes,  "非压格式的SWF.swf" ); //SWF head 为FWS
         }
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值