XMP metadata from JPG


/ Published in: ActionScript 3
Save to your folder(s)

you'll need the xmp core lib from adobe for this to work.
http://www.adobe.com/devnet/xmp/library/eula.html

This is before real testing, so there WILL be errors!


Copy this code and paste it in your HTML
  1. private function init(event:Event):void
  2. {
  3. var ldr:Loader = new Loader();
  4. ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
  5. var s:String = "link/to/asset.jpg";
  6. ldr.load(new URLRequest(s));
  7. }
  8. private function imgLoaded(e:Event):void{
  9. var info:LoaderInfo = e.target as LoaderInfo;
  10. var xmpXML:XML = getXMP(info.bytes);
  11.  
  12. //trace(xmpXML);
  13. var meta:XMPMeta = new XMPMeta(xmpXML);
  14. }
  15. private function trim(s:String):String{
  16. return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
  17. }
  18. private function getXMP(ba:ByteArray):XML{
  19. var LP:ByteArray = new ByteArray();
  20. var PACKET:ByteArray = new ByteArray();
  21. var l:int;
  22.  
  23. ba.readBytes(LP, 2, 2);
  24. /*
  25. http://www.adobe.com/devnet/xmp.html
  26. read part 3: Storage in Files.
  27.  
  28. that will explain the -2 -29 and other things you see here.
  29. */
  30. l = LP.readInt() - 2 -29;
  31. ba.readBytes(PACKET, 33, l);
  32.  
  33. var p:String = trim(""+PACKET);
  34. var i:int = p.search('<x:xmpmeta xmlns:x="adobe:ns:meta/"');
  35. /* Delete all in front of the XMP XML */
  36. p = p.substr(i);
  37. /*
  38. For some reason this left some rubbish in front, so I'll hardcode it out for now
  39.  
  40. TODO clean up
  41. */
  42.  
  43. var ar:Array = p.split('<');
  44. var s:String = "";
  45. var q:int;
  46. var j:int = ar.length;
  47. for(q=1;q<j;q++){
  48. s += '<'+ar[q];
  49. }
  50. i = s.search('</x:xmpmeta>');
  51. i += ('</x:xmpmeta>').length;
  52. s = s.slice(0,i);
  53. /* Delete all behind the XMP XML */
  54. return XML(s);
  55. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.