Return to Snippet

Revision: 43441
at March 24, 2011 01:23 by pjetr


Updated Code
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);
}

Revision: 43440
at March 24, 2011 00:20 by pjetr


Updated Code
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 xmp:Namespace = XMPConst.xmp;
	var rdf:Namespace = XMPConst.rdf;
	var tiff:Namespace = XMPConst.tiff;
	var exif:Namespace = XMPConst.exif;
	var dc:Namespace = XMPConst.dc;
	var xapRights:Namespace = XMPConst.xmpRights;
	
	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 APP1:ByteArray = new ByteArray();
	var LP:ByteArray = new ByteArray();
	var NS:ByteArray = new ByteArray();
	var PACKET:ByteArray = new ByteArray();
	var l:int;
	
	ba.readBytes(APP1, 0, 2);
	ba.readBytes(LP, 2, 2);
	ba.readBytes(NS, 4, 29);
	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/"');
	p = p.substr(i);
	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);
	return XML(s);
}

Revision: 43439
at March 24, 2011 00:18 by pjetr


Initial Code
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 xmp:Namespace = XMPConst.xmp;
	var rdf:Namespace = XMPConst.rdf;
	var tiff:Namespace = XMPConst.tiff;
	var exif:Namespace = XMPConst.exif;
	var dc:Namespace = XMPConst.dc;
	var xapRights:Namespace = XMPConst.xmpRights;
	
	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 APP1:ByteArray = new ByteArray();
	var LP:ByteArray = new ByteArray();
	var NS:ByteArray = new ByteArray();
	var PACKET:ByteArray = new ByteArray();
	var l:int;
	
	ba.readBytes(APP1, 0, 2);
	ba.readBytes(LP, 2, 2);
	ba.readBytes(NS, 4, 29);
	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/"');
	p = p.substr(i);
	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>');
	s = s.slice(0,i);
	s+= '</x:xmpmeta>';
	return XML(s);
}

Initial URL

                                

Initial Description
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!

Initial Title
XMP metadata from JPG

Initial Tags

                                

Initial Language
ActionScript 3