Camera.getCamera() signature is misleading…

The issue at hand here was to be able to use multiple cameras in a flex/air application and between them. It was quite trivial to get the default camera running by calling the Camera.getCamera() with no input parameter. The signature for the Camera according to the API docs is:

public static function getCamera(name:String = null):Camera

Normally this led me to believe that is possible to get the cameras based on their names using Camera.names property. Having 2 identical cameras (same model, manufacturer etc…) made me curious how could the method distinguish between them (Camera.names array holds 2 identical strings: “Logitech QuickCam S5500” in this situation). I built a quick test setup code to try it out:

[Bindable]
private var _camerasList:ArrayList;
private var _currentCamera:Camera;
…
/** init **/
if (Camera.names.length)
{
	_camerasList = new ArrayList(Camera.names);
}
…
//cameras change handler
protected function cameras_changeHandler(event:IndexChangeEvent):void
{
	…
	var name:String = event.target as DropDownList).selectedItem;
	_currentCamera = Camera.getCamera(name);
	var video:Video = new Video(_currentCamera.width, _currentCamera.height);
	video.attachCamera(_currentCamera);
	uicomp.addChild(video);
	addElement(uicomp);
}

<s:DropDownList id="cameras"
	dataProvider="{_camerasList}"
	change="cameras_changeHandler(event)">
</s:DropDownList>

It turns out that _currentCamera = Camera.getCamera(name); returns null every time it is called. After some Google research and reading the docs more accurately I got to the part where they suggests that the name parameter is actually the string representation of the camera index in the Camera.names array and not the name of the camera:

name:String (default = null)Specifies which camera to get, as determined from the array returned by the names property. For most applications, get the default camera by omitting this parameter. To specify a value for this parameter, use the string representation of the zero-based index position within the Camera.names array. For example, to specify the third camera in the array, use Camera.getCamera(“2”).

The modified code is now as follows:

protected function cameras_changeHandler(event:IndexChangeEvent):void
{
	...
	_currentCamera = Camera.getCamera((event.target as DropDownList).selectedIndex.toString());
	var video:Video = new Video(_currentCamera.width, _currentCamera.height);
	video.attachCamera(_currentCamera);
	uicomp.addChild(video);
	addElement(uicomp);
}

Works like a charm. (The docs advise though against calling it with a parameter, and rather using the default camera instead). On the other hand a signature like

public static function getCamera(index:String = null):Camera

would be more appropriate at least will make wonder “what index”?.

Leave a Reply

Your email address will not be published. Required fields are marked *

*