AS3获取Jpg图像长宽

一个老外的比较有用的类,自已整合到自已的utls包中:
1.用法:

import flash.events.Event;
import net.diding.utils.JPGSizeExtractor;

private var je:JPGSizeExtractor;

private function DDinit():void
{
je=new JPGSizeExtractor();
je.addEventListener(JPGSizeExtractor.PARSE_COMPLETE, sizeHandler);
je.extractSize("http://192.168.1.102/buzzword/bin-debug/hpde.jpg");
}

private function sizeHandler(e:Event):void
{
trace("Dimensions: " + je.width + " x " + je.height);
}



2.类源文件:JPGSizeExtractor.as

package net.diding.utils
{
import flash.net.URLStream;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.Endian;

public class JPGSizeExtractor extends URLStream
{
protected static const SOF0:Array=[0xFF, 0xC0, 0x00, 0x11, 0x08];
public static const PARSE_COMPLETE:String="parseComplete";
public static const PARSE_FAILED:String="parseFailed";
protected var dataLoaded:uint;
protected var jpgWidth:uint;
protected var jpgHeight:uint;
protected var jumpLength:uint;
protected var stopWhenParseComplete:Boolean;
protected var traceDebugInfo:Boolean;

public function JPGSizeExtractor()
{
endian=Endian.BIG_ENDIAN;
}

protected function jumpBytes(count:uint):void
{
for (var i:uint=0; i < count; i++)
{
readByte();
}
}

protected function progressHandler(e:ProgressEvent):void
{
dataLoaded=bytesAvailable;
var APPSections:Array=new Array();
for (var i:int=1; i < 16; i++)
{
APPSections[i]=[0xFF, 0xE0 + i];
}
var index:uint=0;
var byte:int=0;
var address:int=0;
while (bytesAvailable >= SOF0.length + 4)
{
var match:Boolean=false;
// Only look for new APP table if no jump is in queue
if (jumpLength == 0)
{
byte=readUnsignedByte();
address++;
// Check for APP table
for each (var APP:Array in APPSections)
{
if (byte == APP[index])
{
match=true;
if (index + 1 >= APP.length)
{
if (traceDebugInfo)
trace("APP" + Number(byte - 0xE0).toString(16).toUpperCase() + " found at 0x" + address.toString(16).toUpperCase());
// APP table found, skip it as it may contain thumbnails in JPG (we don't want their SOF's)
jumpLength=readUnsignedShort() - 2; // -2 for the short we just read
}
}
}
}
// Jump here, so that data has always loaded
if (jumpLength > 0)
{
if (traceDebugInfo)
trace("Trying to jump " + jumpLength + " bytes (available " + Math.round(Math.min(bytesAvailable / jumpLength, 1) * 100) + "%)");
if (bytesAvailable >= jumpLength)
{
if (traceDebugInfo)
trace("Jumping " + jumpLength + " bytes to 0x" + Number(address + jumpLength).toString(16).toUpperCase());
jumpBytes(jumpLength);
match=false;
jumpLength=0;
}
else
break; // Load more data and continue
}
else
{
// Check for SOF
if (byte == SOF0[index])
{
match=true;
if (index + 1 >= SOF0.length)
{
// Matched SOF0
if (traceDebugInfo)
trace("SOF0 found at 0x" + address.toString(16).toUpperCase());
jpgHeight=readUnsignedShort();
jpgWidth=readUnsignedShort();
if (traceDebugInfo)
trace("Dimensions: " + jpgWidth + " x " + jpgHeight);
removeEventListener(ProgressEvent.PROGRESS, progressHandler); // No need to look for dimensions anymore
if (stopWhenParseComplete && connected)
close();
dispatchEvent(new Event(PARSE_COMPLETE));
break;
}
}
if (match)
{
index++;
}
else
{
index=0;
}
}
}
}

protected function fileCompleteHandler(e:Event):void
{
if (!jpgWidth || jpgHeight)
dispatchEvent(new Event(PARSE_FAILED));
}

public function extractSize(fileURL:String, stopWhenParsed:Boolean=true):void
{
addEventListener(ProgressEvent.PROGRESS, progressHandler);
addEventListener(Event.COMPLETE, fileCompleteHandler);
dataLoaded=0;
jumpLength=0;
if (traceDebugInfo)
trace("Started loading '" + fileURL + "'");
stopWhenParseComplete=stopWhenParsed;
super.load(new URLRequest(fileURL));
}

public function get loaded():uint
{
return dataLoaded;
}

public function get width():uint
{
return jpgWidth;
}

public function get height():uint
{
return jpgHeight;
}

public function set debug(newDebug:Boolean):void
{
traceDebugInfo=newDebug;
}

public function get debug():Boolean
{
return traceDebugInfo;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值