ActionScript 3 Meets EXIF 2.1

If you have a digital camera, you’re no doubt already familiar with the concept of EXIF data. For those of you that don’t know, when you take a picture with most modern digital cameras, extra information about the configuration of the camera when that picture was taken, is stored in the image file itself. When the picture was taken, shutter speed, f-stop and even GPS information in some cameras are all stored values. Getting at this data programmatically has historically been for the likes of programming languages with fine-grained, low-level control (read: not ActionScript).

Inspired by Darron Schall’s VNC client, it occurred to me that ActionScript 3 and Flash Player 8.5 now have the abilities to go much deeper, much more easily than previously possible. With this in mind, I set to exploring the URLLoader and ByteArray classes and was really impressed. Binary access to data streams, most significant byte order, convenience methods for various read operations and more were all there! This meant that I could now load an image, and then have access to the bits that make up that image. My interest wasn’t so much in the bits that allow for amazing visual effects such as those seen from Andre Michelle and his lightning particles, but rather in the reading of EXIF data.

I had undertaken Flash/EXIF prototypes before for customers interested in Flex, but I leveraged Doug Hughes’ (a.k.a. Alagad) EXIF web service (written in C#). I would get a URL for the target image from the user, call the EXIF web service to process the image located at that endpoint, and then display the data in my Flex application. That’s works, but it’s a lot of network traffic, and if I wanted to account for file upload (prior to Flash Player 8), then I had to get into the various Flash/JavaScript hacks and all that they entail.

Wouldn’t it be grand if I could simply read the bits from the image myself – especially since I’m likely to be loading and displaying the image in the UI anyways?

The first challenge was in nailing down the EXIF data structures, which I’m still not sure I completely understand. Pooling the work of various resources, and from libraries coded in everything from PHP to C, I was able to create a quick and dirty port of Thierry Bousch’s EXIF information decoder. The original is written in Python, and I’m proud to say that it now runs quite smoothly, and amazing fast, in Flash Player 8.5 thanks to ActionScript 3 (sample output).

Since Bousch’s original EXIF information decoder simply dumps the data to the console, that’s all my ActionScript 3 version does as well. You can run the code in Flex Builder 2, as an ActionScript project, in debug mode and see all the camera/image details that are fit for viewing. Next up is the obvious port to a more complete object-oriented library, and eventual sharing as open source, but I’ll first need to explore and better understand the various packaging, namespaces and custom errors of ActionScript 3. In the meantime, you’re welcome to peruse my initial (and very sloppy) source.

8 Responses to “ActionScript 3 Meets EXIF 2.1”

  1. Chris Charlton Says:

    Very cool. Bookmarking this idea! :)

  2. Christine Says:

    hi i want to ask some question about flash lite

    I am currently try to develop a flash content which the content will be download to a mobile phone from the server, i feel curious in some of the operating in the medium of connecting. here it goes

    If my flash content is embed in a webpage and can be view in a mobile phone, how are we going to dynamically displaying the updated content in the page. ?

    how to code in flash lite?

    If we are using .NET , how the flash lite going to related itself to the .NET and retrive the data from the database?

    Christine

  3. Arul Prasad Says:

    Man o Man!

    You guys are simply superb. I mean, I just thought about - “why not read EXIF with AS”, and try out googling once, and here you are - already made it working!

    Awesome work!

  4. Richard Says:

    I’ve tried implementing this class. It works great with photos that come straight out of the camera untampered. Thanks.

    However, this class awkwardly doesn’t work when you resize and save out of Photoshop cs2, even though the photo still contains the same exif data (exif is NOT stripped). Thoughts anyone?

  5. Matt Says:

    Thought you might be interested in an AIR app that I’ve just launched which uses some of this old code of yours. It’s a panoramic photo stitcher, and it uses the EXIF data to work out orientation and pixel focal length. I’ve made quite a few changes to the code to make the tags accessible. I’m also in the process of adding support for images that have both APP0 and APP1 headers. I can let you have the code when I’m done, if you’d like.
    I’ll credit you just as soon as the app has a credits window.
    You’ll find the app here: http://www.clevr.com/blog/2007/adobe-air-panoramic-photo-stitcher/

  6. Rohan Rehman Says:

    Hello Kevin I really need ur help I got a hold of your Exif class. but when calling the parse method with a jpeg I get null.

    readImage(file);

    private function readImage(file){
    trace (file.nativePath); // make sure I am sending a file.
    var stream:FileStream = new FileStream();
    stream.addEventListener( Event.COMPLETE, getExif );
    stream.open( file, FileMode.READ );

    }

    private function getExif( event:Event ):void{
    var stream:FileStream = event.target as FileStream;
    var exif:Exif = new Exif();
    exif.parse(stream);
    trace(”exif “+exif.parse( stream ));

    }

  7. Ryan D Johnson Says:

    Hi, Kevin.

    I was really glad to find that someone else had already implemented an EXIF parser in AS3. :-)

    I was writing up a credit for you, but I noticed that there’s no explicit licensing on your code (though it appears that the python script you based it on is Public Domain).

    Right now I’m operating on the assumption that since you work for Adobe that this is basically sample code, but I wanted to double check with you to make sure that it’s ok for me to use the code in my own flex application.

    I’m unable to find an email address for you, so I hope you’ll get this blog comment.

    Thanks.
    /rdj

  8. Kevin Hoyt Says:

    Hey Ryan,

    Yup, public domain. I’ve been considering a public code repository and some fashion of liberal licensing (seems to be expected these days), so if you have any recommendations, please let me know.

    I should note that my Exif parser has been reported to be hit and miss. It’s a good start if nothing else. I know a number of Flex/AIR projects that have used it and told me that they’ve tweaked XYZ to make it more reliable. So … You get what you pay for. I have an article on thumbnail extraction too, which I did better (IMHO), but that skips the Exif. Put the two together and you’ll probably have something (smile).

    Hope it helps,
    Kevin

Leave a Reply