Search

 

September 2010
S M T W T F S
« Jul   Oct »
 1234
567891011
12131415161718
19202122232425
2627282930  

Tags

Archives


« | Main | »

AIR on Android: CameraUI

By Rich Tretola | September 20, 2010
29,569 views

The flash.media.CameraUI class allows you to access the camera of your Android devise. This example shows how to access the camera of the devise and load an image into your application. Lets take a look at the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package
{
    import flash.display.Sprite;
    import flash.events.MediaEvent;
    import flash.events.MouseEvent;
    import flash.media.CameraUI;   
    import flash.media.MediaPromise;
    import flash.media.MediaType;
   
    public class Cam2 extends Sprite
    {
       
        private var camera:CameraUI;
       
        public function Cam2() {
            if (CameraUI.isSupported){                 
                camera = new CameraUI();
                camera.addEventListener(MediaEvent.COMPLETE, onComplete);              
            } else {
                mediaPath.text = "CameraUI not supported";
            }
            //takePictureBtn is a Button object which is on my Flash CS5 stage
            takePictureBtn.addEventListener(MouseEvent.CLICK,captureImage);
        }
       
        private function captureImage(event:MouseEvent):void {
            if (CameraUI.isSupported){
                camera.launch(MediaType.IMAGE);
            }
        }
         
        private function onComplete(event:MediaEvent):void{
            var mp:MediaPromise = event.data;
            //mediaPath is a text object which is on my Flash CS5 stage
            mediaPath.text = mp.file.name + "\n" + mp.file.url;
            //loader is a flash.containers.UILoader which is on my Flash CS5 stage
            loader.source = mp.file.url;
        }
       
    }
}

Don’t forget to add the additional android permissions to allow access to the Camera within the application config file.

1
2
3
4
5
6
7
8
9
10
<android>
    <manifestAdditions>
      <manifest>
        <data><![CDATA[
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-feature android:name="android.hardware.camera.autofocus"/>
        ]]></data>
      </manifest>
    </manifestAdditions>
  </android>

The process is simple. Here is the order of events:

Screen Shot 1 shows the app is the default state:
camui1

Screen Shot 2 shows the Nexus One’s camera:

camui2

Screen Shot 3 shows the app with the new camera image name, path, and preview:
camui3

Topics: ActionScript 3, Adobe AIR, android, mobile | 5 Comments »

5 Responses to “AIR on Android: CameraUI”

  1. Swf.hu 2.0 – flash és webfejlesztés » Heti linkajánló 2010/39 Says:
    September 27th, 2010 at 8:31 am

    [...] AIR on Android: CameraUI Képek készítése Androidos mobilok kamerájával AIR segítségével. [...]

  2. ijoer316 Says:
    December 29th, 2010 at 3:14 am

    hey there, I was checking your example code here and when i try to compile it in flash i keep getting an error. It says that packages can not be nested.

    What am i doing wrong here?

    I put the script at the top of the page here in FRAME 1 of my FLA file.

    Reply to this comment

    Tim Reply:

    @ijoer316 -

    You cant paste this code directly into the timeline AS. This is a package and needs to be referenced as a class from an external .as file.

    If you want to paste directly into the timeline… do this.
    1. Set your stage to = the camera/video size. In this case 800w x 480h

    2. Make sure your flash/android publish setting are set for full screen and landscape

    3. Paste this code in frame 1

    var camera:Camera = Camera.getCamera();
    if (camera == null)
    {
    trace (“No camera”);
    }
    else
    {
    trace(“Camera is installed.”);
    camera.setMode( 800,400, 25, false );
    }
    connectCamera();
    function connectCamera():void
    {

    var video:Video = new Video(camera.width, camera.height);
    video.attachCamera(camera);
    addChild(video);

    }

    Reply to this comment

  3. Fidel Says:
    May 8th, 2011 at 3:38 am

    I tried making my own little app from your example – it works like a charm on my HTC Desire. With one minor problem: The photos are saved twice in the library. Do you have any idea what could be causing that?

    Reply to this comment

    carrybagman Reply:

    I also have Fidel’s problem of CameraUI storing 2 identical photos on the phone, which for me is a Samsung Galaxy S2. Looking at the file creation times I think the first one is created when you take the picture and then the second one (which is the one returned on the MediaEvent.COMPLETE event) when you press the button to accept it. Don’t really want the SD card filling up twice as fast as it should. Anyone know how to stop this happening please?

    Reply to this comment

Comments