actionscript-3airmetadataxmpmetatag

using XMPCore to get metadata of a file through actionscript


Is there a simple example to read/write metadata of e.g. a local .jpg? XMPCore ActionScript Reference

Unfortunately I was not able to apply the instructions from adobe.

A simple example will help me a lot! Thanks


Solution

  • I've written a small snippet on the matter. this snippet is far from being proper tested, and is most definite not written in a clear and coherent way. But for now it seems to work. I'll update as I work on it.

    From http://snipplr.com/view/51037/xmp-metadata-from-jpg/

    private function init(event:Event):void
    {
        var ldr:Loader = new Loader();
        ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
        var s:String = "link/to/asset.jpg";
        ldr.load(new URLRequest(s));
    }
    private function imgLoaded(e:Event):void{
        var info:LoaderInfo = e.target as LoaderInfo;
        var xmpXML:XML = getXMP(info.bytes);
    
        //trace(xmpXML);
        var meta:XMPMeta = new XMPMeta(xmpXML);
    }
    private function trim(s:String):String{
        return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
    }
    private function getXMP(ba:ByteArray):XML{
            var LP:ByteArray = new ByteArray();
        var PACKET:ByteArray = new ByteArray();
        var l:int;
    
        ba.readBytes(LP, 2, 2);
        /* 
        http://www.adobe.com/devnet/xmp.html
        read part 3: Storage in Files.
    
        that will explain the -2 -29 and other things you see here.
         */
        l = LP.readInt() - 2 -29;
        ba.readBytes(PACKET, 33, l);
    
        var p:String = trim(""+PACKET);
        var i:int = p.search('<x:xmpmeta xmlns:x="adobe:ns:meta/"');
        /* Delete all in front of the XMP XML */
        p = p.substr(i);
        /* 
        For some reason this left some rubbish in front, so I'll hardcode it out for now 
    
        TODO clean up
        */
    
        var ar:Array = p.split('<');
        var s:String = "";
        var q:int;
        var j:int = ar.length;
        for(q=1;q<j;q++){
            s += '<'+ar[q];
        }
        i = s.search('</x:xmpmeta>');
        i += ('</x:xmpmeta>').length;
        s = s.slice(0,i);
        /* Delete all behind the XMP XML */
        return XML(s);
    }