Displaying webcams video feed in Flex VideoDisplay Component
This example shows you how to get access to the video feed from your computers webcam via the Flex VideoDisplay component. This caused me an hour of headscratching so hopefully this example will help some others out there!
Initially I tried creating a camera object using the following code:
import mx.controls.Alert;
import flash.media.Camera;
import mx.events.ListEvent;
import mx.utils.ObjectUtil;
private function get_camera():void{
var camera:Camera = new Camera();
if(!camera){
Alert.show('No camera detected');
}else{
Alert.show(camera.name);
webcam_display.attachCamera(camera);
}
}
This code did not work as expected, it gathered a list of cameras (three for some reason, even though I only have a built in iSight) so it knew something was available to use, but it would not display any video.
So I had a quick check of the AS3 Language reference and came across the static Camera.getCamera() method. Instead of creating a new camera object as in the code above, you need to make a very slight change and do it this way instead:
import mx.controls.Alert;
import flash.media.Camera;
import mx.events.ListEvent;
import mx.utils.ObjectUtil;
private function get_camera():void{
//Change is made to this line!
var camera:Camera = Camera.getCamera();
if(Camera.names.length == 0){
Alert.show('No camera detected');
}else{
Alert.show(camera.name);
webcam_display.attachCamera(camera);
}
}
As you can see, we are not using an instance of the camera class here, just importing the Class and using the static method provided by the Language reference. As soon as I made this change I saw my mugshot on the screen, and all was good! I need to look into the docs a little more to see why I cannot create a camera object in the normal way and if I do dig anything up I will post it here but hopefully this will save some head scratching and / or swearing for someone out there.







Comments
Post new comment