package utils
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.errors.IllegalOperationError;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.registerClassAlias;
import spark.primitives.BitmapImage;
public class ImageSlicer
{
/**
* Creates a new ImageSlicer
*
* @constructor
* @throws IllegalOperationError
*/
public function ImageSlicer() {
throw new IllegalOperationError('ImageSlicer cannot be instantiated.');
}
public static function sliceImageWithFixedSliceSize(image:BitmapData,sliceWidth:uint,sliceHeight:uint):Array
{
var iWidth:Number = image. width;
var iHeight:Number = image.height;
var testMod:Boolean;
testMod=((iWidth%sliceWidth)>0)?true:false;
var numCols:uint=iWidth/sliceWidth+testMod;
testMod=((iHeight%sliceHeight)>0)?true:false;
var numRows:uint=iHeight/sliceHeight+testMod;
var iArray:Array = new Array();
var rectangle:Rectangle=new Rectangle();
var bitmap:Bitmap;
var bData:BitmapData;
for( var rowIdx:int = 0; rowIdx < numRows; rowIdx++ ) {
iArray[rowIdx] = new Array();
for( var colIdx:int = 0; colIdx < numCols; colIdx++ ) {
bData = new BitmapData( sliceWidth, sliceHeight, true, 0x00000000 );
rectangle = new Rectangle( colIdx * sliceWidth, rowIdx * sliceHeight, sliceWidth, sliceHeight );
bData.copyPixels( image, rectangle, new Point( 0, 0 ) );
bitmap = new Bitmap( bData );
iArray[rowIdx][colIdx] = bitmap;
}
}
return iArray;
}
}
}
沒有留言:
張貼留言