Skip to content

Instantly share code, notes, and snippets.

@azrafe7
Created January 20, 2013 18:14
Show Gist options
  • Save azrafe7/4580413 to your computer and use it in GitHub Desktop.
Save azrafe7/4580413 to your computer and use it in GitHub Desktop.
Utility class to save BitmapData as PNG.
package
{
import com.adobe.images.PNGEncoder;
import flash.display.BitmapData;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileReference;
import flash.utils.ByteArray;
/**
* Utility class to save BitmapData as PNG.
*
* @author azrafe7
*/
public class ScreenShot
{
public static var prefix:String = "SCREENSHOT "; // prefix to use for file names
public static var ZERO:Point = new Point; // Point(0, 0)
public static var onKeyDown:Function = null; // callback function for key down
public static var onClick:Function = null; // callback function for mouse click
public static var onFileSaved:Function = null; // callback function for file saved
public static var queue:Vector.<*> = new Vector.<*>; // queue of {data:ByteArray, fileName:String} objects to save
/**
* Initializes the class (must call this before using other methods of the class).
*
* @param stage a reference to the stage.
*/
public static function init(stage:Stage):void
{
if (!stage) throw new Error("Passed stage cannot be null!");
stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
stage.addEventListener(MouseEvent.CLICK, _onClick);
}
/**
* Adds bitmapData to the queue of files to save.
*
* @param bitmapData BitmapData you want to save.
* @param fileName Suggested fileName.
* @param clipRect Area of bitmapData that must be saved.
*/
public static function enqueueBMD(bitmapData:BitmapData, fileName:String = null, clipRect:Rectangle = null):void
{
var bmd:BitmapData;
if (!clipRect) {
bmd = bitmapData.clone();
} else {
bmd = new BitmapData(clipRect.width, clipRect.height);
bmd.copyPixels(bitmapData, clipRect, ZERO);
}
if (!fileName) fileName = createFileName() + ".png";
var byteArray:ByteArray = PNGEncoder.encode(bmd);
queue.push( { data:byteArray, fileName:fileName } );
trace("File added (now " + queue.length + " in queue)");
bmd.dispose();
bmd = null;
}
/**
* Saves all objects in the queue (must be called from an eventListener).
*/
public static function saveAll():void {
trace("Saving " + queue.length + " files...");
if (queue.length > 0) {
var fileReference:FileReference = new FileReference();
fileReference.addEventListener(Event.COMPLETE, _onFileSaved);
var item:* = queue.shift();
fileReference.save(item.data, item.fileName);
}
}
/**
* Creates a file name using prefix and appending date and time.
*/
public static function createFileName():String
{
var now:Date = new Date;
return prefix + getShortDate() + " " + getShortTime(null, true);
}
/**
* Returns a date string (YYYY-MM-DD) from a Date object.
*/
public static function getShortDate(date:Date=null, separator:String="-"):String
{
if (!date) date = new Date;
return date.getFullYear() + separator + pad(date.getMonth()+1) + separator + pad(date.getDate());
}
/**
* Returns a time string (HH-MM-SS-MLS) from a Date object.
*/
public static function getShortTime(date:Date=null, milliseconds:Boolean=false, separator:String="-"):String
{
if (!date) date = new Date;
return date.getHours() + separator + pad(date.getMinutes()) + separator + pad(date.getSeconds()) + (milliseconds ? separator + pad(date.getMilliseconds(), 3) : "");
}
/**
* Adds padChar to value string until the result is of specified length.
*/
public static function pad(value:*, length:int = 2, padChar:String = '0'):String
{
var res:String = String(value);
while (res.length < length) res = padChar + res;
return res;
}
// EVENT LISTENERS
protected static function _onKeyDown(event:KeyboardEvent):void
{
if (onKeyDown != null && onKeyDown is Function) onKeyDown(event);
}
protected static function _onClick(event:MouseEvent):void
{
if (onClick != null && onClick is Function) onClick(event);
}
protected static function _onFileSaved(event:Event):void
{
var fileReference:FileReference = FileReference(event.currentTarget);
trace(" File saved (" + queue.length + " more in queue): " + fileReference.name);
fileReference.removeEventListener(Event.COMPLETE, _onFileSaved);
if (onFileSaved != null && onFileSaved is Function) onFileSaved(event);
if (queue.length > 0) saveAll();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment