I just wrapped up my first little Android App using Adobe AIR.  As far as development goes, that was one of the smoothest experiences I’ve ever had.

I’m currently developing a mobile app for a project at work.  I’ve never created a mobile app before, and the project app is going to take a few weeks of solid work to complete.  I wanted to see the whole process from dev to release of a mobile app much sooner…. like, now.  So Friday I started writing classes and code that I’m going to need for the project at work, and that I could pull out of the project and use in a small tutorial project that I’m posting here.

I created the Image GPS Extractor app to allow users to extract Exif GPS data from a picture on their phone.  The app really only works if the pictures you are trying to decode were taken with GeoLocation enabled.  So, if a user has his/her GPS disabled for photos, the app is basically useless.

I used Shichiseki.jp’s Exif Library for AS3 to extract the Exif info from JPG files. The library can be found here

We’ll go ahead and jump into the main MobileApplication screen.


	
		
	
	
		
	
		
	


Line 3: firstView is specifying which view component in the “views” folder is going to be the first view once the app starts.
Line 5: splashScreenImage is setting the file “splashscreen.png” to be the image that pops up as your app is loading
Line 14: I created a singleton class called ZFModel to be a global reference to things I need between views. Probably not entirely necessary, but it’s there and I like it.
Line 17: I grab a reference to the MobileApplications ViewNavigator so I can use it in other views.
Line 21: Spark’s navigationContent is an “Array of visual elements that are used as the ActionBar’s navigationContent when this view is active.” Which basically means, it’s the buttons, tabs, words, etc up at the top of your view that handle navigation and let the user know where they are located in your app. I created a button, labeled it “Home” and made it ‘pop back to the first view’ (which in Line 3 we specified as views.Home) when clicked.

Lets take a look at our first (and only… really…) view, Home.


	
		
	


	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	
	
	
	

Alrighty… lets dive in.
Line 3: when the user changes to this view, title will be the text that appears at the top of the screen.
Line 12 & 18: I’m importing and instantiating my ExifGPSUtil class that wraps Shichiseki’s Exif Library
Line 24: The init function checks to see if the mobile device has a camera (if yes, creates a new Camera() object ) and if it has a CameraRoll feature (if yes, creates a new CameraRoll() object )
Line 39: onError only occurs with the cameraRoll if the user selects a file that doesn’t come from the device’s “Gallery” area
Line 45: mediaCaptured occurs when a new photo is taken. It takes the image as a MediaPromise, then casts it to a File. Finally we pass it to the ExifGPSUtil to decode. More on that later.
Line 51: mediaSelected occurs when a photo is selected out of the devices Gallery. Again, MediaPromise casted to a File then decoded.
Line 57: browseGallery executes when the “Browse Gallery” button is pressed. If the phone supports browseForImage then lets browseForImage
Line 61: takePicture executes when the “Take Picture” button is pressed. If the phone has a camera, launch it’s native picture taking process
Line 69: onExifDataCompleteHandler is the callback function assigned to exifGPSUtil.decodeExifData on lines 48 and 54. When the ExifGPSUtil is done processing an image, it calls this function.
Line 70: If the image had actual GeoLocation lat/long data with it… otherwise this will return as “false” and we’ll jump down to Line 80.
Line 72 – 76: Passing the data that was returned into our ZFModel for safe keeping then setting the TextFields text properties to the data so it shows up on the screen.
Line 78: We have data now so lets enable the “Map Coordinates” button so users can press it now.
Line 82-84: No coordinates were found in the image so lets display an alert message letting the user know
Line 88-92: User has clicked the “Map Coordinates” button, get the lat/long from the ZFModel and lets navigate to google maps.

You may ask, “Why did you not just use Google’s Maps API for Flash? I did, originally, but then I found that when I clicked Map Coordinates… it took anywhere from 15-40 seconds for the actual map api to appear. During this time I got to see the “Your application is not responding, force close or wait” screen appear a lot. I decided to just not use the API because it was incredibly slow and unreliable on my phone.

Lets look at the ExifGPSUtil.as next…

package com.zf.utils  {
public class ExifGPSUtil  {
		
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
		
	import jp.shichiseki.exif.*;
		
	private var _exifLoader : ExifLoader = new ExifLoader();
	private var _callBack	: Function;
	private var _type		: String;
		
	public function ExifGPSUtil()  {
	}
	
	/***
	 * Pulls the Exif data out of a JPG image
	 * 
	 * @param url 	String 	URL of the image to be decoded
	 * @param callBack	Function	Function to call back to once image exif info is decoded
	 **/
	public function decodeExifData( url:String , callBack:Function ):void  {
			
		_callBack = callBack;
			
		_exifLoader.addEventListener(Event.COMPLETE, onExifComplete);
		_exifLoader.load( new URLRequest( url ) );
			
	}
		
	private function onExifComplete( e:Event ):void  {
		var obj:Object = new Object();
			
		if( _exifLoader.exif.ifds.gps != null )  
		{
			var rawLat	: String = _exifLoader.exif.ifds.gps["GPSLatitude"];
			var rawLong	: String = _exifLoader.exif.ifds.gps["GPSLongitude"];
				
			var posNegLat	: String = "+";
			var posNegLong	: String = "+";
				
			if( _exifLoader.exif.ifds.gps["GPSLatitudeRef"] == "S" )
				posNegLat = "-";
			if( _exifLoader.exif.ifds.gps["GPSLongitudeRef"] == "W" )
				posNegLong = "-";
				
			obj.success = "true";
			
			obj.latDMS 	= posNegLat + parseLatLong( _exifLoader.exif.ifds.gps["GPSLatitude"] );
			obj.longDMS	= posNegLong + parseLatLong( _exifLoader.exif.ifds.gps["GPSLongitude"] );
				
			obj.latDEC	= posNegLat + parseLatLongToDecimal( _exifLoader.exif.ifds.gps["GPSLatitude"] ).toString();
			obj.longDEC	= posNegLong + parseLatLongToDecimal( _exifLoader.exif.ifds.gps["GPSLongitude"] ).toString();
				
			_callBack( obj );
		}  
		else  
		{
			obj.success = "false";
			_callBack( obj );
		}
	}
		
	private function parseLatLong( val:String ) :String  {
		var v : Array = val.split( ',' );
		return v[0] + "º " + v[1] + "' " + v[2] + "''";
	}
		
	private function parseLatLongToDecimal( val:String ):Number  {
		var v : Array = val.split( ',' );
		var d : Number = v[0];
		var m : Number = v[1];
		var s : Number = v[2];
		
		var decimal:Number = d + ( m / 60 ) + ( s / 60 / 60 );
		return decimal;
	}
}
}

Line 8: Importing Shichiseki’s libraries
Line 23: This is the sole public function of this class. decodeExifData takes a url string and a callback function as params. Shichiseki’s ExifLoader class is basically just a URLLoader with some of his own functions in there so it operates just like a URLLoader.
Line 35: _exifLoader.exif.ifds.gps is how far you have to drill into _exifLoader to find the gps data. If it is null, that means there were no lat/long nor any GPS data found on the image.
Line 37 – 38: gps[“GPSLongitude”] and gps[“GPSLatitude”] contain a string that looks like: “39,48,2”. These numbers represent Degrees, Minutes, and Seconds all separated by commas. I am putting them in “rawLat/Long” because we’re soon going to convert that to a more user-friendly string.
Line 40 – 46: These lines contain the solution to the only real laughable development bug in this whole process. The coordinates the Exif Library returns are unsigned. They do not have + or – signs in front of them. And it was really, really curious to me as to why an image taken in the middle of the United States got mapped to coordinates in the middle of China. Instead, the Library gives us gps[“GPSLatitudeRef”] which contains “N” or “S” and gps[“GPSLongitudeRef”] which contain either “E” or “W”. If your reference is “S” or “W” you need to make that Lat or Long a negative number.
Line 50-54: Parsing values and setting up my obj to be returned.
Line 56: calling the callback function and passing it my coordinates object
Line 61 – 62: If gps is null, set the object success to “false” and call the callback function
Line 65: parseLatLong takes the comma separated string “39,48,2” and parses it to look like: 39º 48′ 2”
Line 70: parseLatLongToDecimal takes the comma separated string “39,48,2” and parses it to be a decimal value that looks like: 39.800555

—-
Project ZIP | Project FXP

Categories:

0 Comments

Leave a Reply

Avatar placeholder