Hi-Res webcam part #248
October 3rd, 2008Yesterday, Oli woke my sleeping awareness of my weblog by commenting on this one and a half year old post about getting a high resultion picture out of a webcam in Flash. His comment (in short): “it doesn’t work!”.
Unfortunately, he’s correct. I’ve run into this myself, once CS3 came out. It did work in the Flash 9 Bèta, but it is broken in the release version. The problem lies in these lines:
my_video._width = my_cam.width; my_video._height = my_cam.height;
Where this also adjusted the what is now Video.videoWith and Video.videoHeight properties, it is now impossible to change those after the creation of your instance of the Video object. No need to worry, though, because if you’d just supply the size you want when creating the instance, everything works fine. Here’s the whole working thing:
var my_cam:Camera = Camera.getCamera();
my_cam.setMode(640,480,10);
var my_video:Video = new Video(my_cam.width, my_cam.height);
my_video.attachCamera(my_cam);
addChild(my_video);
var bmd_video:BitmapData = new BitmapData(my_cam.width,my_cam.height);
var bm:Bitmap = new Bitmap(bmd_video);
bm.x = my_cam.width;
bm.y = 0;
addChild(bm);
var updateInterval:uint = setInterval(update,100);
function update() {
bmd_video.draw(my_video);
}
Of course, putting everything in nice class files would be better, but I find timeline code easier for these simple demonstrations. This doesn’t require you to create and save all the files, it’s just paste and run. But if you prefer that, you can use this:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.Timer;
[SWF(width="1280", height="480", frameRate="10", backgroundColor="#FFFFFF")]
public class WebcamHiRes extends MovieClip
{
private var _camera:Camera;
private var _video:Video;
private var _bmd:BitmapData;
private var _bm:Bitmap;
private var _updateTimer:Timer;
public function WebcamHiRes():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_camera = Camera.getCamera();
_camera.setMode(640, 480, 10);
_video = new Video(_camera.width, _camera.height);
_video.attachCamera(_camera);
addChild(_video);
_bmd = new BitmapData(_camera.width, _camera.height);
_bm = new Bitmap(_bmd);
_bm.x = _camera.width;
addChild(_bm);
_updateTimer = new Timer(100);
_updateTimer.addEventListener(TimerEvent.TIMER, update);
_updateTimer.start();
}
private function update(e:Event = null):void
{
_bmd.draw(_video);
}
}
}