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

来源:互联网 发布:开源 大数据连接器 编辑:程序博客网 时间:2024/04/27 21:25
import ImageParser;//支持的图片格式为jpg/gif/pngvar 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;}}}}


原创粉丝点击