Archive for the ‘Source Code’ Category

Send pictures from Android device to Windows with AIR applications

In this example I will show how you can take and send pictures from Android phone to Windows application using Adobe AIR in Android and Windows.

Create AIR for Android application (to take and send picture)

In Adobe AIR 2.5 there is Camera UI class that allows you to capture a still image or video using the default camera application on a device. First check that Camera is supported and create a new Camera UI object and register MediaEvent.COMPLETE to call onComplete function when picture is taken.

After that establish a new NetConnection for P2P in the local network (LAN). This can be done by specifying connection string as "rtmpf:" in NetConnection.

private function init():void {
  if (CameraUI.isSupported){
    myCam = new CameraUI();
    myCam.addEventListener(MediaEvent.COMPLETE, onComplete);
  }
  nc = new NetConnection();
  nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
  nc.connect("rtmfp:");
}

When connection is made netStatus function will be called. If NetConnection is ok it is time to setup own group for multicasting. You can set up your own NetGroup with GroupSpecifier. Enable local connections between peers and set your IP address and port for multicasting. You can find more details about multitasking for example here: www.flashrealtime.com (Tom Krcha - thanks about your excellent demos and work with Flash based Technologies).

private function netStatus(event:NetStatusEvent):void {
  switch(event.info.code) {
    case "NetConnection.Connect.Success":
       var groupspec:GroupSpecifier = new GroupSpecifier("fi.ptm.imageApp/myChannel");
       groupspec.postingEnabled = true;
       groupspec.ipMulticastMemberUpdatesEnabled = true;
       groupspec.addIPMulticastAddress("225.225.0.1:30000");
       group = new NetGroup(nc,groupspec.groupspecWithAuthorizations());
       group.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
       break;
  }
}

A new picture is taken when user presses Take Picture Button in device's screen. Picture is taken with Android Native Camera Application (using with Camera UI class) and onComplete will be called when picture is taken.

In this example image is shown with <mx:Image id="image"> and image URL is used when image is sent to Windows AIR application.

private function takePicture():void {
  image.filters = [];
  if (CameraUI.isSupported) myCam.launch(MediaType.IMAGE);
}

private function onComplete(evt:MediaEvent):void{
  image.source = evt.data.file.url;
  imageURL = evt.data.file.url;
}

Image is converted to ByteArray and all of those bytes is sent to Windows AIR application when user clicks send button in the devices screen.

private function sendPicture():void {
  var fs:FileStream = new FileStream();
  fs.open(new File(imageURL), FileMode.READ);
  var bytes:ByteArray = new ByteArray();
  fs.readBytes(bytes);
  fs.close();
  if (bytes == null) {
    trace("Image bytes is null!");
  } else {
    // create a new object with image data and image count (sent)
    var message:Object = new Object();
    message.bytes = bytes;
    message.images = ++imagesSent;
    group.post(message);
    trace("Pictures sent:"+imagesSent);
  }
}

Create AIR local client in Windows (receive images)

Windows AIR application is connected to same group and it is waiting for images from AIR for Android application. If we get a new netStatus with NetGroup.Posting.Notify message then a new image is received from Android. First new Sprite is created and all of those image bytes is loaded with Loader.

public function receivePicture(message:Object):void {
  imageContainer = new Sprite();
  var loaderContext:LoaderContext = new LoaderContext();
  loaderContext.allowCodeImport = false;
  var loader:Loader = new Loader();
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPicLoaded,false,0,true);
  loader.loadBytes(message.bytes, loaderContext);
  // add a new sprite to hold image
  imageSprite = new Sprite();
  imageSprite.addChild(loader);
  imageContainer.addChild(imageSprite);
  wrapper.addChild(imageContainer);
}

ImageSprite is a wrapped for a new image sprite to get Parallel image animation into it (scaling, positioning and sizing) for 4 seconds.

private function onPicLoaded(e:Event):void {
  imageSprite.x = -imageContainer.width / 2;
  imageSprite.y = -imageContainer.height / 2;
  imageContainer.x += imageContainer.width / 2;
  imageContainer.y += imageContainer.height / 2;
  startX = imageContainer.width / 2;
  startY = imageContainer.height / 2;
  targetX = Math.floor(Math.random()*frame.width);
  var borderSize:uint = 100;
  if (targetX < borderSize) targetX = borderSize;
  if (targetX > (frame.width-borderSize)) targetX = frame.width - borderSize;
  targetY = Math.floor(Math.random()*frame.height);
  if (targetY < borderSize) targetY = borderSize;
  if (targetY > (frame.height-borderSize)) targetY = frame.height - borderSize;
  startRotation = Math.floor(Math.random()*360);
  if (Math.random() > .5) targetRotation = startRotation + 180;
  else targetRotation = startRotation - 180;
  imageAnimation.play();
}

Here is a basic parallel animation used to make a cool sliding effect with a new images. All previous taken images will be visible there too.

<mx:Parallel id="imageAnimation" target="{imageContainer}" >
  <mx:AnimateProperty property="scaleX" fromValue="1" toValue="0.2" duration="4000" />
  <mx:AnimateProperty property="scaleY" fromValue="1" toValue="0.2" duration="4000" />
  <mx:AnimateProperty property="x" fromValue="{startX}" toValue="{targetX}" duration="4000" />
  <mx:AnimateProperty property="y" fromValue="{startY}" toValue="{targetY}" duration="4000" />
  <mx:AnimateProperty property="rotation" fromValue="{startRotation}" toValue="{targetRotation}" duration="4000" />
</mx:Parallel>

Testing and source codes

You have to install Flex 4.1 (or newer) and AIR 2.5 SDK to try these examples (just copy/overwrite AIR SDK 2.5 to Flash Builder sdks/4.1.0 folder, or make copy from 4.1.0 to for example FlexAIR2.5 folder and overwrite there).

AIRSendingImageFromAndroid.zip
AIRReceiveImageFromAndroid.zip

Forum Nokia Wiki: Contributor of the Month – March/2010

I wrote Flash Lite related articles and code examples to Forum Nokia Wiki and the Wiki Administrators selected me  to Contributor of the Month.  It is big honor to me - thanks! My articles covers features of APIBridge and S60 Platform Services API for Flash Lite.

"Forum Nokia honors one member every month for their outstanding contribution to our community’s Wiki. The contributor of the month is awarded a new S60 device. The honor is given to people that have contributed outstanding work for the month in number of articles and quality of the articles."

Articles and Flash Lite source codes:
* Capturing and uploading image, video or audio files to server with Flash Lite and APIBridge
* Creating Image Gallery with Flash Lite and APIBridge
* Display contact information and locations in Google Maps with Flash Lite 3.1

Display contact informations and locations in Google Maps with Flash Lite 3.1

I improved my eariler example which loads contact informations from users device - displays informations to scrollable list and shows contacts location in Google Maps (static image). In this new version a new Device Object is used to keep Flash Lite application in portrait mode all the time. Another great feature is to detect users device location with APIBridge. In this version of Contacts Demo both user location and contact's address location is shown at static Google Maps Image (user can adjust zoom level).

This Flash Lite example code article is published in Forum Nokia Wiki. You can found more informations and source codes there:
- Display contact informations and locations in google Maps with Flash Lite 3.1

Edit: This tutorial got Featured Article of the Week status in Forum Nokia Wiki at Week 14 - April 4th, 2010.

Creating Image Gallery with Flash Lite and APIBridge

In this my second Forum Nokia Wiki code example article I explain how to create a image gallery with Flash Lite and APIBridge. Technically first all the images data are loaded from device using the Media Management Service, then user can browse images just pressing next or prev buttons on the screen. Device object is used to keep Flash Lite application in landscape mode all the time.

APIBridge and Platform Services API provides great plug-ins for Flash Lite Developers, if you are not familiar with those, you should give some time to them.

Capturing and uploading image, video or audio files to server with Flash Lite and APIBridge

Finally I got time to write my first article in to forum nokia wiki. APIBridge has been there for a while for Flash Lite and JavaScript developers. In my first article I will demonstrate how to capture image, video or audio in Flash Lite 3.0 based devices and how to send those files to server side. This can be done with Flash Lite and ActionScript 2.0 in a few lines of code. You can find FLA and PHP source codes at the bottom of article page in forum nokia wiki.

Here is a direct link to wiki page.

Edit: This tutorial got Featured Article of the Week status in Forum Nokia Wiki at Week 11- March 14th, 2010.