Flex Save canvas as JPEG
Wednesday, June 8, 2011 Category : Flex Builder 0
var dpi:Number = 250;
var imageSnapShot:ImageSnapshot = ImageSnapshot.captureImage(TestCanvas, dpi, new JPEGEncoder(), false);
var byteInfo:ByteArray = imageSnapShot.data;
byteInfo.compress();
var imageFile:File = new File(File.applicationDirectory.nativePath + "/Test.jpg");
var fileStream:FileStream = new FileStream();
fileStream.open(imageFile, FileMode.WRITE);
while(fileStream.writeBytes(byteInfo, 0, byteInfo.length))
{
};
fileStream.close();
======================================================================
var targetObject:DisplayObject = TestCanvas;
var canvasWidth:int = targetObject.width;
var canvasHeight:int = targetObject.height;
var bitmapDataCP:BitmapData = new BitmapData(canvasWidth, canvasHeight, true, 0xFFFFFFFF);
bitmapDataCP.draw(targetObject as IBitmapDrawable);
targetBitmap = new Bitmap(bitmapDataCP);
dobookEncoder = new DoBookJPGEncoder();
dobookEncoder.PixelsPerIteration = 1000;
dobookEncoder.addEventListener(DoBookCompleteEvent.DOBOOK_JPGENCODE_COMPLETE, testRndComplete, false, 0, true);
dobookEncoder.encode(targetBitmap.bitmapData);
======================================================================
var scale:Number = 4;
var myBMPD:BitmapData = new BitmapData( TestCanvas.width, TestCanvas.height);
var myMatrix:Matrix = new Matrix( scale, 0, 0, scale );
myBMPD.draw( TestCanvas, myMatrix );
var bitmap : Bitmap = new Bitmap(myBMPD);
var jpg:JPEGEncoder = new JPEGEncoder();
var byteInfo:ByteArray = jpg.encode(myBMPD);
var imageFile:File = new File(File.applicationDirectory.nativePath + "/Test.jpg");
var fileStream:FileStream = new FileStream();
fileStream.open(imageFile, FileMode.WRITE);
while(fileStream.writeBytes(byteInfo, 0, byteInfo.length))
{
};
fileStream.close();