`
diding
  • 浏览: 80300 次
  • 性别: Icon_minigender_1
  • 来自: 四川
社区版块
存档分类
最新评论

Flex存bmp图像

    博客分类:
  • Flex
阅读更多
前言:

这阵子没事在研究buzzword,并试图还原前台,费了将近三周了,还原了:
EditorFonts.swf
FrameCSS.swf
以及res下面mxml文件成as文件
vu下面的class大家都看得到,只需要适当修改就可以了

还原的过程中,感触很多,总觉的gumbo 的 Text Layout Framework 需要改进的还有太多太多,也不知正式版出来后会怎么样?

正文:
昨晚没事,看到一个老外的bitmap的研究,很可惜针对movieclip,于是作了一个适当的修改,把它一个像JPEGEncoder那样可以适合flex components的class

1.运用实例:
?xml version="1.0" encoding="utf-8"?>
<FxApplication name="FileReference_save_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        xmlns:mx="http://ns.adobe.com/mxml/2009"
        xmlns:net="flash.net.*"
        creationComplete="init();">
    <Script>
        <![CDATA[
            import mx.graphics.ImageSnapshot;
            import mx.graphics.codec.*;
            import net.diding.graphics.codec.BMPEncoder;

            private const jpegEnc:JPEGEncoder = new JPEGEncoder();
            private const bmpEnc:BMPEncoder = new BMPEncoder();
            
            private const xmlObj:XML = describeType(FileReference);

            private function init():void {
                textArea.text = xmlObj.toXMLString();
            }

            private function btn_click(evt:MouseEvent):void {
                //存为jpg
                //var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(panel, 0, jpegEnc);
                //fileReference.save(imageSnap.data, "describeType.jpg");
                 
                 //存为bmp
                 var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(panel, 0, bmpEnc);
                 fileReference.save(imageSnap.data, "describeType.bmp");
            }
        ]]>
    </Script>

    <Declarations>
        <net:FileReference id="fileReference" />
    </Declarations>

    <mx:Panel id="panel"
            width="500"
            height="300"
            verticalCenter="0"
            horizontalCenter="0">
        <mx:TextArea id="textArea"
                editable="true"
                width="100%"
                height="100%" />
        <mx:ControlBar horizontalAlign="right">
            <Button id="btn"
                    label="Save"
                    click="btn_click(event);" />
        </mx:ControlBar>
    </mx:Panel>

</FxApplication>



2.BMPEncoder.as
package net.diding.graphics.codec
{
	import flash.display.BitmapData;
	import mx.graphics.codec.IImageEncoder;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	public class BMPEncoder implements IImageEncoder
	{
		public function BMPEncoder()
		{
		}
		public function encode(bitmapData:BitmapData):ByteArray
		{
			// 图像属性
			var bmpWidth:int = bitmapData.width;
			var bmpHeight:int = bitmapData.height;
			var imageBytes:ByteArray = bitmapData.getPixels(bitmapData.rect);
			var imageSize:int = imageBytes.length;
			var imageDataOffset:int = 0x36;
			var fileSize:int = imageSize + imageDataOffset;
			
			// 图像数据
			var bmpBytes:ByteArray = new ByteArray();
			bmpBytes.endian = Endian.LITTLE_ENDIAN;
			
			// header信息
			bmpBytes.length = fileSize;
			bmpBytes.writeByte(0x42); 
			bmpBytes.writeByte(0x4D); 
			bmpBytes.writeInt(fileSize); 
			bmpBytes.position = 0x0A; 
			bmpBytes.writeInt(imageDataOffset);
			bmpBytes.writeInt(0x28); 
			bmpBytes.position = 0x12; 
			bmpBytes.writeInt(bmpWidth);
			bmpBytes.writeInt(bmpHeight);
			bmpBytes.writeShort(1); 
			bmpBytes.writeShort(32); 
			bmpBytes.writeInt(0); 
			bmpBytes.writeInt(imageSize); 
			bmpBytes.writeUnsignedInt(0x2e30); 
			bmpBytes.writeUnsignedInt(0x2e30);
			
			bmpBytes.position = imageDataOffset; 
			// 存为bmp格式
			var col:int = bmpWidth;
			var row:int = bmpHeight;
			var rowLength:int = col * 4; 
			try {
				imageBytes.position = 0;
				while (row--) {
					
					bmpBytes.position = imageDataOffset + row*rowLength;
					col = bmpWidth;
					while (col--) {
						bmpBytes.writeInt(imageBytes.readInt());
					}
				}	
			}catch(error:Error){	
			}
			// 返回BMP数据文件
			return bmpBytes;
		}

		public function get contentType():String
		{
			return null;
		}

		public function encodeByteArray(byteArray:ByteArray, width:int, height:int, transparent:Boolean=true):ByteArray
		{
			return null;
		}
		
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics