Search code examples
actionscript-3flashwebcamscalingbitmapdata

Save original bitmapData from scaled video in AS3


I have a flash photo booth (web cam) application that I'm trying to wrap up but seem to be having issues trying to save a 640x480 image from a scaled down video window. The video seems to be scaling down fine but when I draw it to a bitmap it's shrinking the photo even more so I had to create a matrix scale of 2.0 to resize it back to 640x480 and I'm not sure if doing so is hurting the quality of the image. I don't want to use any resize hacks (especially up-scaling).

I'm new to AS3 so please forgive me.

import flash.display.Bitmap;
import flash.display.BitmapData;
import com.adobe.images.JPGEncoder;

var cam:Camera = Camera.getCamera();
cam.setQuality(0, 100);
cam.setMode(640,480,30,true); // setMode(videoWidth, videoHeight, video fps, favor   area)
var video:Video = new Video();
video.attachCamera(cam);
video.x = 382;
video.y = 225;
video.width = 256;
video.height = 192;
addChild(video);

var bitmapData:BitmapData = new BitmapData(640,480);

var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.x = 648;
bitmap.y = 225;
bitmap.width = 256;
bitmap.height = 192;
addChildAt(bitmap, 18);

photoCapture.buttonMode = true;
photoCapture.addEventListener(MouseEvent.CLICK,captureImage);

here is the dirty part...

function captureImage(e:MouseEvent):void {
var scale:Number=2.0;
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);
bitmapData.draw(video, matrix, null, null, null, true);

var jpgEncoder:JPGEncoder=new JPGEncoder(100);
var byteArray:ByteArray=jpgEncoder.encode(bitmapData);

var fileReference:FileReference=new FileReference();
fileReference.save(byteArray, ".jpg");
}

Basically I just want two small box's about 256x192 showing the video stream (640x480) and captured photo (640x480) and when I save it actually saves a 640x480 image.


Solution

    • First you create a video 256x192
    • then you create a bitmapData 640x480
    • then you shrink this bitmapdata (well, the bitmap actually) to 256x192
    • then you try to draw a small video into a large bitmap(data) - no wonder that you have to upscale

    Create a BitmapData, Bitmap and Video in the same size and everything should be OK.

    If you want the final image to be 640x480 you should create video, bitmap, bitmapdata in this size, then add the video to an empty MovieClip/Sprite (container) and scale container in order to fit it on the screen.