比较简单方便的通过纯 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, 0, 8 );
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
}
}
}
{
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, 0, 8 );
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
}
}
}