The Camera Application and the CameraUI Class

0
118

You can access the native camera within AIR. Your application needs to have the permission for it. In Flash Professional, select File→AIR Android settings→Permissions→Camera. In Flash Builder, select CAMERA under Mobile Settings→
Permissions.

The flash.media.CameraUI class is an addition to the ActionScript language to support the device’s native camera application. It is a subclass of the EventDispatcher class. It is only supported on AIR for mobile applications.

This object allows you to launch the native camera application to take an image or shoot a video while your AIR application moves to the background. The image is stored in the Gallery along with the other images.

To use this class, first you must verify that your device supports access to the camera by checking the CameraUI.isSupported property:

import flash.media.CameraUI;
if (CameraUI.isSupported == false) {
trace(“You cannot use the native camera.”);
return;
}

If camera access is supported, create an instance of the CameraUI class and call its launch function. This function expects one parameter of type MediaType to specify picture or video mode. Choosing a level of compression is not an option at the time of this writing:

import flash.media.MediaType;
var cameraUI:CameraUI = new CameraUI();
cameraUI.launch(MediaType.IMAGE);

The camera application is now active and in the foreground. The AIR application moves to the background.

To receive camera events, set listeners before launching the camera. A Media Event.COMPLETE is dispatched after a picture is taken, an Event.CANCEL if no media is selected, and an ErrorEvent if there is an error in the process:

cameraUI.addEventListener(MediaEvent.COMPLETE, onComplete);
cameraUI.addEventListener(Event.CANCEL, onCancel);
cameraUI.addEventListener(ErrorEvent.ERROR, onError);

Once the event is received, the camera application automatically closes and the AIR application moves back to the foreground.

function onComplete(event:MediaEvent):void {
var promise:MediaPromise = event.data as MediaPromise;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
loader.loadFilePromise(promise);
}
function onImageLoaded(event:Event):void {
var bitmapData:BitmapData = Bitmap(event.target.content).bitmapData;
var bitmap:Bitmap = new Bitmap(bitmapData);
var isPortrait:Boolean = (bitmapData.height/bitmapData.width) > 1.0;
var forRatio:int = Math.min(stage.stageHeight, stage.stageWidth);
var ratio:Number;
if (isPortrait) {
ratio = forRatio/bitmapData.width;
} else {
ratio = forRatio/bitmapData.height;
}
bitmap.width = bitmapData.width * ratio;
bitmap.height = bitmapData.height * ratio;
if (!isPortrait) {
bitmap.y = stage.stageHeight;
bitmap.rotation = -90;
}
addChild(bitmap);
}

Uploading to a Remote Server

Images can be uploaded to a remote server if you have access to one. You need the Internet permission to add this functionality:

<uses-permission android:name=”android.permission.INTERNET” />

This process is identical to what you would do in a desktop application:

import flash.net.URLRequestMethod;
import flash.filesystem.File;
var request:URLRequest = new URLRequest(“server url”);
request.method = URLRequestMethod.POST;
var uploadFile:File = new File(promise.file.url);
uploadFile.upload(request);