Accessing the camera phone with Adobe AIR for Android

Adobe AIR for Android can access some hardware features of smartphones. Among some of them we can mention the access to internet, read your phone’s status, access network, access the wifi, GPS, sensors, etc.. Adobe AIR can also access the camera device, and this can be done in two ways..

Adobe AIR is available for devices running Android from version 2.2 (Froyo)

we have two classes to access the camera with Adobe AIR for Android:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Camera.html
The first way to access the camera phone is using the Camera class, classical and already well known for our work in web and desktop environment. Before you can use the method Camera.isSupported, and using cameras to catch the Camera.getCamera().

Usually this class is also used to send the stream from your camera to the Flash Media Server(together with the NetStream class) and transmit to web, desktop or mobile. Here’s an example usage:
[ACTIONSCRIPT3]
var camera:Camera = Camera.getCamera();
if (camera != null)
{
video = new Video(camera.width * 2, camera.height * 2);
video.attachCamera(camera);
addChild(video);
}
else
{
trace(“You need a camera.”);
}
[/ACTIONSCRIPT3]
In getCamera, if not passed any parameters, access the device’s main camera, you can access a camera with the index specifies it as a string:
[ACTIONSCRIPT3]
var arrCamera:Array = Camera.names;// todas as câmeras como array
var camera:Camera = Câmera.getCamera(“1”);//acessando a câmera de indece 1
[/ACTIONSCRIPT3]

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/CameraUI.html
CameraUI is a new class unique to Adobe AIR for Mobile Devices, allowing you to capture an image or video application standard camera phone. The image or video is available on the object MediaEvent. Here’s an example usage:
[ACTIONSCRIPT3]
var deviceCameraApp:CameraUI = new CameraUI();
var imageLoader:Loader;

deviceCameraApp.addEventListener( MediaEvent.COMPLETE, imageCaptured );
deviceCameraApp.addEventListener( Event.CANCEL, captureCanceled );
deviceCameraApp.addEventListener( ErrorEvent.ERROR, cameraError );
deviceCameraApp.launch( MediaType.IMAGE );

function imageCaptured( event:MediaEvent ):void
{
trace( “Media captured…” );

var imagePromise:MediaPromise = event.data;

if( imagePromise.isAsync )
{
trace( “Asynchronous media promise.” );
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, asyncImageLoaded );
imageLoader.addEventListener( IOErrorEvent.IO_ERROR, cameraError );

imageLoader.loadFilePromise( imagePromise );
}
else
{
trace( “Synchronous media promise.” );
imageLoader.loadFilePromise( imagePromise );
showMedia( imageLoader );
}
}

function captureCanceled( event:Event ):void
{
trace( “Media capture canceled.” );
NativeApplication.nativeApplication.exit();
}

function asyncImageLoaded( event:Event ):void
{
trace( “Media loaded in memory.” );
showMedia( imageLoader );
}

function showMedia( loader:Loader ):void
{
this.addChild( loader );
}

function cameraError( error:ErrorEvent ):void
{
trace( “Error:” + error.text );
NativeApplication.nativeApplication.exit();
}
[/ACTIONSCRIPT3]

One last detail to work on the smartphone, it is necessary to set the permission for the application can access the camera on Android. This is done in the xml configuration of Adobe AIR:
[XML]

]]>

[/XML]

Was this article helpful? feel free to make a donation and help keep the blog in the air
Adobe AIR, Android, Flash , ,

6 comments


  1. Pingback: Accessing the camera phone with Adobe AIR for Android | Leonardo … | camera

  2. Pingback: Accessing the camera phone with Adobe AIR for Android | Leonardo … | Camera of life

  3. I never know that adobe air is all ready in my Android phone until read your post about Adobe Air for Android. I been a fanatic of that beast software Adobe specially playing Flash Games. Anyway thank you for the Link of Flash Platform Reference .

  4. Adobe Air is totally the best, very useful at android. Adobe has been very useful and its features is great and endless. Adobe, a powerful apps nowadays.

  5. Cathy

    Hi Leonardo,

    Iv been trying to complete this task of making a simple android application which takes a picture for a while now and your tutorial is the closest iv gotten to becoming successful. Iv manipulated the coding a bit at the beginning as with the code from your tutorial it came up with many errors. the application opens on my android device now but only for a second and then it quickly shuts. is there any advice you can give on what i’m doing wrong?

    Here is my code:
    import flash.media.Camera;

    var c:Camera = Camera.getCamera();
    c.setMode(camera.width, camera.height, 30);
    camera.attachCamera(c); //access devices camera

    var deviceCameraApp:CameraUI = new CameraUI();
    var imageLoader:Loader;

    deviceCameraApp.addEventListener( MediaEvent.COMPLETE, imageCaptured );
    deviceCameraApp.addEventListener( Event.CANCEL, captureCanceled );
    deviceCameraApp.addEventListener( ErrorEvent.ERROR, cameraError );
    deviceCameraApp.launch( MediaType.IMAGE );

    function imageCaptured( event:MediaEvent ):void
    {
    trace( “Media captured…” );

    var imagePromise:MediaPromise = event.data;

    if( imagePromise.isAsync )
    {
    trace( “Asynchronous media promise.” );
    imageLoader = new Loader();
    imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, asyncImageLoaded );
    imageLoader.addEventListener( IOErrorEvent.IO_ERROR, cameraError );

    imageLoader.loadFilePromise( imagePromise );
    }
    else
    {
    trace( “Synchronous media promise.” );
    imageLoader.loadFilePromise( imagePromise );
    showMedia( imageLoader );
    }
    }

    function captureCanceled( event:Event ):void
    {
    trace( “Media capture canceled.” );
    NativeApplication.nativeApplication.exit();
    }

    function asyncImageLoaded( event:Event ):void
    {
    trace( “Media loaded in memory.” );
    showMedia( imageLoader );
    }

    function showMedia( loader:Loader ):void
    {
    this.addChild( loader );
    }

    function cameraError( error:ErrorEvent ):void
    {
    trace( “Error:” + error.text );
    NativeApplication.nativeApplication.exit();
    }

    Thank you, Cathy

  6. we have to see the smartphone’s hardware configuration, I had this problem when I got a device that did not have much processing power.

Leave a Reply