Rotate bitmap data in AS3 180deg for export

actionscript-3, bitmap, bitmapdata, flash, matrix

Solution

I think you need to translate your object after rotation otherwise it gets clipped so you don't see it, I did an example with a bitmap in a MovieClip just on the stage using CS5.

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;

var scaleFactor:int = 1;

var tempBitmap:BitmapData = new BitmapData(img.width, img.height);

var rotationMatrix:Matrix = new Matrix();
rotationMatrix.rotate(Math.PI);
rotationMatrix.translate(img.width, img.height);

tempBitmap.draw(box_mc, rotationMatrix);
var output:Bitmap = new Bitmap(tempBitmap);

addChild(output);

I didn't mess with the scaling, you can do that yourself I guess. The one I put on the stage is on the right, the bitmapData drawn one is top left of stage, and correctly inverted.

UPDATE

Also have a look at fl.motion.MatrixTransformer as per 32bitkid comment below!

Problem

I have a bitmap in AS3 flash and I am sending it over to javascript using a base64 encoded jpg string. All that works great. I need to send the img over scaled down, and rotated 180deg. The scaling works, but not the rotation. The rotation never takes. I know Im am obviously doing it wrong, but don't know the right way. So basically I just need to rotate the image 180deg so when it is sent to javascript it is upside down. Here is my transform code ``` var scaleFactor:int = 5; var tempBitmap:BitmapData = new BitmapData(img.width/scaleFactor, img.height/scaleFactor); var drawMatrix:Matrix = new Matrix(1/scaleFactor, 0, 0, 1 /scaleFactor ); drawMatrix.rotate(Math.PI); //180deg tempBitmap.draw(img, drawMatrix); ```

Original source