Main flow part code.
Resizing part code.
package utils.tool { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import utils.ImageUtils; public class ImageResizer { private var maxW:int, maxH:int; private var smoothing:Boolean; private var saveFunction:Function; public function ImageResizer(maxW:int, maxH:int, smoothing:Boolean = false, type:String="jpg") { this.maxW = maxW; this.maxH = maxH; this.smoothing = smoothing; if (type === "png") { saveFunction = ImageUtils.saveBitmapDataAsPNGAsync; } else if(type === "jpg"){ saveFunction = ImageUtils.saveBitmapDataAsJPEGAsync; } else { throw new Error("Not Supported"); } } public function resizeAndSave(srcURL:String, destURL:String):Bitmap { return resizeAndCreate(srcURL, saveFunction, destURL); } public function resize(srcURL:String):Bitmap { return resizeAndCreate(srcURL, doNothing); } protected function resizeAndCreate(srcURL:String, process:Function, destURL:String=null):Bitmap { var retBMP:Bitmap = new Bitmap(); resizeMain(srcURL, process, retBMP, destURL); return retBMP; } protected function resizeMain(srcURL:String, process:Function, retBMP:Bitmap, destURL:String=null):void { var loader:Loader = new Loader(); loader.load(new URLRequest(srcURL)); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); function completeHandler(evt:Event):void { loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler); var bmp:Bitmap = loader.content as Bitmap if (bmp != null) { retBMP.bitmapData = ImageUtils.resizeLimitSize(bmp, maxW, maxH); process(destURL, retBMP.bitmapData); } loader.unload(); } } public function resizeAndPopulate(srcURL:String, bmp:Bitmap):void { resizeMain(srcURL, doNothing, bmp); } private static function doNothing(...args):void {} } }
Resizing part code.
public static function resize(s:BitmapData, dw:int, dh:int, smoothing:Boolean=false):BitmapData { var d:BitmapData = new BitmapData(dw, dh); d.draw(s, new Matrix(dw/s.width, 0, 0, dh/s.height), null, null, null, smoothing); return d; } public static function resizeLimitSize(bmp:Bitmap, maxW:int, maxH:int, smoothing:Boolean=false):BitmapData { var bitmapData:BitmapData = bmp.bitmapData; var ratioW:Number = maxW / bitmapData.width; var ratioH:Number = maxH / bitmapData.height; if (ratioW < ratioH) { var expectW:int = maxW; var expectH:int = bitmapData.height * ratioW; } else { expectW = bitmapData.width * ratioH; expectH = maxH; } var resizedBMPData:BitmapData = ImageUtils.resize(bitmapData, expectW, expectH, smoothing); bitmapData.dispose(); return resizedBMPData; }Here is the code for ImageUtils.saveBitmapDataAsJPEGAsync, saveBitmapDataAsPNGAsync etc.
public static function saveBitmapDataAsJPEGAsync(path:String, bitmapData:BitmapData, quality:Number=50.0):void { saveByteDataAsync(new JPEGEncoder(quality).encode(bitmapData), path); } public static function saveBitmapDataAsPNGAsync(path:String, bitmapData:BitmapData):void { saveByteDataAsync(new PNGEncoder().encode(bitmapData), path); } public static function saveByteDataAsync(data:ByteArray, path:String):void { try { var file:File = new File(path); var fs:FileStream = new FileStream(); fs.openAsync(file, FileMode.WRITE); fs.writeBytes(data); fs.close(); } catch (err:IOError) { trace(err); } }
コメント