ImageParser(加载完成前获取图片尺寸)

el/2024/6/13 22:01:33
import ImageParser;
//支持的图片格式为jpg/gif/png
var url:String = "1.jpg";
var uq:URLRequest = new URLRequest(url);
var _imgPar:ImageParser=new ImageParser();
_imgPar.parse(uq);
_imgPar.addEventListener(ImageParser.PARSE_COMPLETE,sizeComplete);
_imgPar.addEventListener(ImageParser.PARSE_FAILED,sizeFailed);
function sizeComplete(evt:Event)
{trace("contentDimensions:"+_imgPar.contentWidth+"*"+_imgPar.contentHeight);
}
function sizeFailed(evt:Event)
{trace("contentDimensions has no parsed");
}

ImageParser.as

/*
@Name:ImageParser.as
@Usage:在图片未下载完成前获取图像的图片,支持jpg,gif,png格式。
*/
package{import flash.net.URLStream;import flash.net.URLRequest;import flash.events.Event;import flash.events.ProgressEvent;import flash.utils.Endian;public class ImageParser extends URLStream {/*HexTag 数据标记,每串标记作为标记数组里的一个元素leapLength 跳过的字节数fileType 文件类型parseComplete 解析完成标记contentHeight,contentWidth 目标高度和宽度isAPPnExist 标记JPG图片的APPn数据段是否存在,默认为无*/protected static  const JPGHexTag:Array=[[0xFF,0xC0,0x00,0x11,0x08]];protected static  const PNGHexTag:Array=[[0x49,0x48,0x44,0x52]];protected static  const GIFHexTag:Array=[[0x21,0xF9,0x04],[0x00,0x2C]];;protected var APPnTag:Array;protected var HexTag:Array;protected var address:uint;protected var fileType:String;protected var byte:uint;protected var index:uint=0;protected var leapLength:uint;private var parseComplete:Boolean=false;private var match:Boolean=false;private var isAPPnExist:Boolean=false;public var contentHeight:uint;public var contentWidth:uint;public static  const PARSE_FAILED:String="PARSE_FAILED";public static  const PARSE_COMPLETE:String="PARSE_COMPLETE";public function ImageParser() {}//解析对象public function parse(uq:URLRequest):void {addEventListener(ProgressEvent.PROGRESS,parseHandler);addEventListener(Event.COMPLETE,completeHandler);fileType=uq.url.slice(uq.url.lastIndexOf(".") + 1).toLowerCase();switch (fileType) {case "png" :HexTag=PNGHexTag;break;case "jpg" :HexTag=JPGHexTag;APPnTag=new Array();break;case "gif" ://gif图像数据endian是LITTLE_ENDIANendian=Endian.LITTLE_ENDIAN;HexTag=GIFHexTag;leapLength=4;break;}load(uq);}//解析数据流protected function parseHandler(evt:ProgressEvent):void {if (fileType == "jpg") {JPGAPPnMatch();} else {matchHexTag();}if (parseComplete) {dispatchEvent(new Event(PARSE_COMPLETE));removeEventListener(ProgressEvent.PROGRESS,parseHandler);if (connected) {close();}}}//解析完成protected function completeHandler(evt:Event):void {if (!contentWidth||!contentHeight) {dispatchEvent(new Event(PARSE_FAILED));}}//比较SOF0数据标签,其中包含width和height信息protected function matchHexTag() {var len:uint=HexTag.length;while (bytesAvailable > HexTag[0].length) {match=false;byte=readUnsignedByte();address++;if (byte == HexTag[0][index]) {//trace(byte.toString(16).toUpperCase());match=true;if (index >= HexTag[0].length - 1 && len == 1) {getWidthAndHeight();parseComplete=true;break;} else if (index >= HexTag[0].length - 1 && len > 1) {HexTag.shift();index=0;matchHexTag();break;}}if (match) {index++;} else {index=0;}}}//因为JPG图像比较复杂,有的有缩略图APPn标签里(缩略图同样有SOF0标签),所有先查找APPn标签protected function JPGAPPnMatch() {while (bytesAvailable > leapLength) {match=false;byte=readUnsignedByte();address++;if (byte == 0xFF) {byte=readUnsignedByte();address++;/*如果byte在0xE1与0xEF之间,即找到一个APPn标签APPn标签为(0xFF 0xE1到0xFF 0xEF)*/if (byte >= 225 && byte <= 239) {isAPPnExist=true;//trace(byte.toString(16).toUpperCase());leapLength=readUnsignedShort() - 2;leapBytes(leapLength);JPGAPPnMatch();}}//APPn标签搜索完毕后即可开始比较SOF0标签if (byte != 0xFF && leapLength != 0) {matchHexTag();break;}/*如果超过一定数据还未找到APPn,则认为此JPG无APPn,直接开始开始比较SOF0标签。这里我取巧选了一个100作为判断,故并不能保证100%有效,但如重新解析的话效率并不好。如果谁有更有效的解决办法请告诉我,谢谢。*/if (address > 100 && isAPPnExist == false) {matchHexTag();break;}}}//跳过count个字节数protected function leapBytes(count:uint):void {for (var i:uint=0; i < count; i++) {readByte();}address+= count;}//获取加载对象的width和heightprotected function getWidthAndHeight() {if (fileType == "gif") {leapBytes(leapLength);}switch (fileType) {case "png" :contentWidth=readUnsignedInt();contentHeight=readUnsignedInt();break;case "gif" :contentWidth=readUnsignedShort();contentHeight=readUnsignedShort();break;case "jpg" :contentHeight=readUnsignedShort();contentWidth=readUnsignedShort();break;}}}
}



http://www.ngui.cc/el/5432086.html

相关文章

flash时间轴声音大小控制

A2时间轴声音大小控制: var sound:Sound new Sound(); sound.setVolume(200);AS3时间轴声音大小控制: var trans new SoundTransform(2,0); SoundMixer.soundTransform trans;

as3 addEventListener各参数详解

public function addEventListener(type:String, listener:Function, useCapture:Boolean false, priority:int 0, useWeakReference:Boolean false):void * type:String 动作类型 * listener:Function 调用的方法 * useCapture:Boolean(default false) 确定侦听器是运行于…

网页SWF页面传参

网页里: swf/about.swf?id123 swf/about.swf?id<%Request.QueryString["id"] %> as3里: var id0; if (loaderInfo.parameters["id"]!null) { idloaderInfo.parameters["id"]; }

AS忽略png透明区域的鼠标事件

http://bbs.9ria.com/thread-73075-1-2.html 这个问题被提到过很多次了&#xff0c;这里是一种解决方案&#xff0c;http://l4cd.net/blog/post-ignore-the-events-of-png-transparency.html。可是经过测试Sprite的hitArea效率很低&#xff0c;而且还需要创建一个area对象&…

EnterFrame和TimerEvent区别

1. enterFrame是一个由播放器发布的事件&#xff0c;每过(1/帧频)秒向全部继承自DisplayObject的对象发送。 在这个事件之前正好是屏幕的一次固定刷新。(即两次更新的时间间隔为(1/帧频)秒) 2. 在flex中对enterFrame设置频率 在Application标签中设置frameRate”"&#xf…

XCode 4.2(4.1)真机调试及生成IPA全攻略

众所周知&#xff0c;在Xcode上开发的程序只能在模拟器中运行&#xff0c;如果要放到真机上则要花费99美金购买开发者证书iDP。这严重阻碍了我等草根开发者探索的脚步。写个小程序&#xff0c;同学间分享一下这个小小的愿望都不能满足&#xff0c;自然不能善罢甘休。 在没有iDP…

AS3老虎机简单原型

http://nickro.org/wp-content/uploads/2012/04/demo.swf

Super用法深透理解

学习AS3以来&#xff0c;Super用法一直有点搞不清&#xff0c;帮助文档里说的也是含糊不清。。 我们看下官方帮助先&#xff1a;“调用方法或构造函数的超类或父版本。在类构造函数体内使用 super() 语句时&#xff0c;该语句将调用构造函数的超类版本。对超类构造函数的调用必…

stage.width|stage.height|stage.stageWidth|stage.stageHeight区别

我们先看下帮助文档里的说明&#xff1a; stage.width&#xff1a;指示显示对象的宽度&#xff0c;以像素为单位。宽度是根据显示对象内容的范围来计算的。如果您设置了 width 属性&#xff0c;则 scaleX 属性会相应调整&#xff0c;如以下代码所示&#xff1a; stage.height&a…

加载动态数据,不同环境判断

加载动态文件时&#xff0c;需添加随机数&#xff0c;独立PLAYER则不需要&#xff0c;只需要添加以下判断即可 if (Capabilities.playerType "StandAlone" || Capabilities.playerType "External") {}else {infoXMLString "?" Math.random(…