This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Changed return type of command to a value object, which returns helpf…

…ul references to the ByteArray, BitmapData and original File object. Also changed example image renderer to give visual feedback on the revised file sizes
  • Loading branch information...
psyked committed May 29, 2010
1 parent 91f5c4e commit f01a3f0dafcf4d83327900d49d13f8e5a212f530
Binary file not shown.
@@ -8,7 +8,8 @@
import couk.markstar.starrequests.requests.IRequest;
import couk.markstar.starrequests.utils.RequestQueueUtil;
import couk.psyked.starrequests.requests.GenerateThumbnailBitmapDataRequest;
-
+ import couk.psyked.starrequests.requests.vo.GenerateThumbnailBitmapDataRequestVO;
+
import mx.collections.ArrayCollection;
override protected function childrenCreated():void
@@ -17,6 +18,11 @@
browse_button.addEventListener( MouseEvent.CLICK, onLocationBrowse );
+ if ( dataProvider )
+ {
+ dataProvider.removeAll();
+ dataProvider = null;
+ }
dataProvider = new ArrayCollection();
preview_list.dataProvider = dataProvider;
}
@@ -32,8 +38,12 @@
protected function onLocationSelect( e:Event ):void
{
- trace(( e.currentTarget as File ).url );
- generateQueueForLocation(( e.currentTarget as File ));
+ // clean up the stored reference to the File object used to select the location.
+ fileLocation.removeEventListener( Event.SELECT, onLocationSelect );
+ fileLocation = null;
+ var selectedLocation:File = ( e.currentTarget as File );
+ generateQueueForLocation( selectedLocation );
+ path_label.text = "Location selected: " + selectedLocation.nativePath;
}
protected var dataProvider:ArrayCollection;
@@ -44,20 +54,17 @@
{
for each ( var f:File in fileLocation.getDirectoryListing())
{
- trace( f.nativePath );
var request:IRequest = new GenerateThumbnailBitmapDataRequest( f );
request.completedSignal.add( completedListener );
queue.addRequest( request );
}
}
- protected function completedListener( ba:ByteArray ):void
+ protected function completedListener( vo:GenerateThumbnailBitmapDataRequestVO ):void
{
- trace( "ba.length =", ba.length );
-
- if ( ba.length )
+ if ( vo.thumbnailByteArray.length )
{
- dataProvider.addItem( ba );
+ dataProvider.addItem( vo );
}
}
]]>
@@ -1,10 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
-<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" autoDrawBackground="true">
- <mx:Image width="120" height="120" horizontalCenter="0" verticalCenter="0" id="preview_image"
- maintainAspectRatio="true" verticalAlign="middle" horizontalAlign="center"
- source="{data}">
+<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
+ xmlns:mx="library://ns.adobe.com/flex/mx" autoDrawBackground="true" width="320"
+ height="140">
+ <fx:Script>
+ <![CDATA[
+ import couk.psyked.starrequests.requests.vo.GenerateThumbnailBitmapDataRequestVO;
+ import couk.psyked.utils.FileSizeFormatter;
+
+ override public function set data( value:Object ):void
+ {
+ super.data = value;
+ preview_image.source = typedData.thumbnailByteArray;
+ filename.text = typedData.originalFile.name;
+ original_file_size.text = FileSizeFormatter.GetFileSize( typedData.originalFile.size );
+ resampled_file_size.text = FileSizeFormatter.GetFileSize( typedData.thumbnailByteArray.length );
+ }
+
+ private function get typedData():GenerateThumbnailBitmapDataRequestVO
+ {
+ return data as GenerateThumbnailBitmapDataRequestVO;
+ }
+ ]]>
+ </fx:Script>
+ <mx:Image width="120" height="120" verticalCenter="0" id="preview_image"
+ maintainAspectRatio="true" verticalAlign="middle" horizontalAlign="center" left="10">
<mx:filters>
<mx:DropShadowFilter color="0x000000" angle="90" distance="2" blurX="10" blurY="10" />
</mx:filters>
</mx:Image>
+ <s:Label x="138" y="10" text="Filename:" />
+ <s:Label x="138" y="30" text="Original file size:" />
+ <s:Label x="138" y="50" text="Resampled file size:" />
+ <s:Label x="197" y="10" text="Label" width="114" fontStyle="italic" id="filename" />
+ <s:Label x="237" y="30" text="Label" width="73" fontStyle="italic" id="original_file_size" />
+ <s:Label x="259" y="50" text="Label" width="51" fontStyle="italic" id="resampled_file_size" />
</s:ItemRenderer>
@@ -1,11 +1,12 @@
package couk.psyked.starrequests.requests
{
import cmodule.jpegencoder.CLibInit;
-
+
import couk.markstar.starrequests.requests.AbstractRequest;
import couk.markstar.starrequests.requests.IRequest;
+ import couk.psyked.starrequests.requests.vo.GenerateThumbnailBitmapDataRequestVO;
import couk.psyked.utils.BitmapManager;
-
+
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
@@ -15,10 +16,10 @@ package couk.psyked.starrequests.requests
import flash.filesystem.File;
import flash.net.URLRequest;
import flash.utils.ByteArray;
-
+
import mx.collections.ArrayCollection;
import mx.graphics.ImageSnapshot;
-
+
import org.osflash.signals.ISignal;
import org.osflash.signals.Signal;
@@ -28,12 +29,12 @@ package couk.psyked.starrequests.requests
* pixel image. This request will load a specified file and generate a thumbnail version
* of the image to use. The ByteArray it returns is the JPEG-encoded bitmap data for the
* thumbnail.
- *
+ *
* Ideally you'd store the thumbnail in a cache file somewhere, to save having to generate
* the thumbnail over and over again. This class Request makes use of an Alchemy JPEG encoder
* class to do its encoding faster, but makes use of some pure AS3 classes that leverage the
* guts of the Flash Player to resample the bitmap data to the requested size.
- *
+ *
* If you pass a string of these commands in a syncronous queue it's a real shit for
* memory consumption, but it does clean up after itself when it's done.
*/
@@ -45,13 +46,16 @@ package couk.psyked.starrequests.requests
public function GenerateThumbnailBitmapDataRequest( file:File )
{
+ returnObject = new GenerateThumbnailBitmapDataRequestVO();
+ returnObject.originalFile = file;
+
var alchemyEncoder:CLibInit = new CLibInit();
lib = alchemyEncoder.init();
baout = new ByteArray();
_file = file;
- _completedSignal = new Signal( ByteArray );
+ _completedSignal = new Signal( GenerateThumbnailBitmapDataRequestVO );
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, progressListener );
@@ -64,6 +68,7 @@ package couk.psyked.starrequests.requests
super.send();
var checkArrayCollection:ArrayCollection = new ArrayCollection( ALLOWED_FILE_TYPES );
+
//trace( "_file.extension", _file.extension );
//trace( "checkArrayCollection.contains( _file.extension )", checkArrayCollection.contains( _file.extension ));
@@ -130,14 +135,20 @@ package couk.psyked.starrequests.requests
ba = bmd.getPixels( bmd.rect );
ba.position = 0;
lib.encodeAsync( alchemyEncodingCompleteFunction, ba, baout, bmd.width, bmd.height, 100 );
+
+ returnObject.thumbnailBitmapData = bmd;
}
+ internal var returnObject:GenerateThumbnailBitmapDataRequestVO;
+
private function alchemyEncodingCompleteFunction( ba:ByteArray ):void
{
trace( "alchemyEncodingCompleteFunction", ba );
_progressSignal.dispatch( 1 );
- _completedSignal.dispatch( ba );
+ returnObject.thumbnailByteArray = ba;
+
+ _completedSignal.dispatch( returnObject );
cleanup();
}
@@ -0,0 +1,13 @@
+package couk.psyked.starrequests.requests.vo
+{
+ import flash.display.BitmapData;
+ import flash.filesystem.File;
+ import flash.utils.ByteArray;
+
+ public class GenerateThumbnailBitmapDataRequestVO
+ {
+ public var thumbnailByteArray:ByteArray;
+ public var thumbnailBitmapData:BitmapData;
+ public var originalFile:File;
+ }
+}
@@ -0,0 +1,35 @@
+package couk.psyked.utils
+{
+
+ public class FileSizeFormatter
+ {
+
+ /**
+ * Returns the file size (in bytes) as a logical string.
+ */
+ public static function GetFileSize( num:Number ):String
+ {
+ var bytes:Number = num;
+ var kilobytes:Number = num / 1024;
+ var megabytes:Number = kilobytes / 1024;
+ var gigabytes:Number = megabytes / 1024;
+
+ if ( gigabytes > 1 )
+ {
+ return Math.ceil( gigabytes * 100 ) / 100 + " GB";
+ }
+ else if ( megabytes > 1 )
+ {
+ return Math.ceil( megabytes * 100 ) / 100 + " MB";
+ }
+ else if ( kilobytes > 1 )
+ {
+ return Math.ceil( kilobytes ) + " KB";
+ }
+ else
+ {
+ return Math.ceil( bytes ) + " Bytes";
+ }
+ }
+ }
+}

0 comments on commit f01a3f0

Please sign in to comment.