Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [48]

By Root 2505 0
this feature:

import flash.media.CameraRoll;

if (CameraRoll.supportsAddBitmapData == false) {

trace("You cannot add images to the Gallery.");

return;

}

If this feature is supported, create an instance of CameraRoll and set an Event.COMPLETE listener. Call the addBitmapData function to save the image to the Gallery. In this example, a stage grab is saved.

This feature could be used for a drawing application in which the user can draw over time. The following code allows the user to save his drawing, reload it, and draw over it again:

var cameraRoll:CameraRoll;

cameraRoll = new CameraRoll();

cameraRoll.addEventListener(ErrorEvent.ERROR, onError);

cameraRoll.addEventListener(Event.COMPLETE, onComplete);

var bitmapData:BitmapData =

new BitmapData(stage.stageWidth, stage.stageHeight);

bitmapData.draw(stage);

cameraRoll.addBitmapData(bitmapData);

function onComplete(event:Event):void {

// image saved in gallery

}

Remember that the image that is saved is the same dimension as the stage, and therefore it has a much smaller resolution than the native camera. At the time of this writing, there is no option to specify a compression, to name the image, or to save it in a custom directory. AIR follows Android naming conventions, using the date and time of capture.

The Camera Application and the CameraUI Class

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.

WARNING

Android’s support for the front camera became available with the Gingerbread version. At the time of this writing, AIR does not support the front camera, but it will in a future release.

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 MediaEvent.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.

On select, a MediaEvent object is returned. From this point on, the process is identical to receiving an image from the Gallery. Please refer to Selecting an Image for details:

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;

Return Main Page Previous Page Next Page

®Online Book Reader