Sunday, 5 of September of 2010

Tag » extendedDesktop

AIR2 with NativeProcess and ImageMagick

Hi all again. AIR 2.0 is launched. A much awaited version that I was waiting for last 2 years. Lots of good things but what I like most is NativeProcess (run executables within as native process). I had done lots of tricks to execute commandline executables. But now its much easier. I bet…this feature alone, will give Adobe AIR a new height.

I have done some work with ImageMagick.

Here is the MagickUtils class.

package pravin.magick
{
 import flash.desktop.NativeProcess;
 import flash.desktop.NativeProcessStartupInfo;
 import flash.events.Event;
 import flash.events.ProgressEvent;
 import flash.events.IOErrorEvent;
 import flash.events.NativeProcessExitEvent;
 import flash.filesystem.File;
 /**
 * Image magick utility to use in Adobe AIR applications using NativeProcess.
 *  @author Pravin Ranjan
 *  @Date 16 June, 2010.
 */
 public class MagickUtils
 {
 private var objWorkingFolder:File;
 private var objOutFolder:File;
 private var process:NativeProcess;
 private var objConvertExe:File;
 /*Explicitely declared constructor.*/
 public function MagickUtils(objWorkingFolder:File,
 objOutFolder:File, onOutputData:Function,
 onErrorData:Function, onExit:Function, onIOError:Function)
 {
 this.objWorkingFolder = objWorkingFolder;
 this.objOutFolder = objOutFolder;

 objConvertExe = File.applicationDirectory.resolvePath("imagemagik/win/convert.exe");

 process = new NativeProcess();
 process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
 process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
 process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
 process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
 process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
 }

 public function convertTo(strFromImage:String, strToImage:String):void
 {
 var objFile:File = objWorkingFolder.resolvePath(strFromImage);
 strFromImage = objFile.nativePath;
 strToImage = objOutFolder.nativePath + File.separator + strToImage;
 trace("strFromImage: " + strFromImage);
 trace("strToImage: " + strToImage);

 var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
 nativeProcessStartupInfo.executable = objConvertExe;
 var processArgs:Vector.<String> = new Vector.<String>();
 processArgs.push(strFromImage);
 processArgs.push(strToImage);
 nativeProcessStartupInfo.arguments = processArgs;
 process.start(nativeProcessStartupInfo);
 }
 }
}

AND here is main class…

package
{
 import flash.display.Sprite;
 import flash.desktop.NativeProcess;
 import flash.desktop.NativeProcessStartupInfo;
 import flash.events.Event;
 import flash.events.ProgressEvent;
 import flash.events.IOErrorEvent;
 import flash.events.NativeProcessExitEvent;
 import flash.filesystem.File;
 import pravin.magick.MagickUtils;

 public class NativeProcessExample extends Sprite
 {
 public var process:NativeProcess;

 public function NativeProcessExample()
 {
 if(NativeProcess.isSupported)
 {
 setupAndLaunch();
 }
 else
 {
 trace("NativeProcess not supported.");
 }
 }

 public function setupAndLaunch():void
 {     
 var magik:MagickUtils = new MagickUtils(File.applicationDirectory, File.applicationDirectory, onOutputData, onErrorData, onExit, onIOError);
 magik.convertTo("MyImage.jpg", "OutImage.png");
 }

 public function onOutputData(event:ProgressEvent):void
 {
 trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
 }

 public function onErrorData(event:ProgressEvent):void
 {
 trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable));
 }

 public function onExit(event:NativeProcessExitEvent):void
 {
 trace("Process exited with ", event.exitCode);
 }

 public function onIOError(event:IOErrorEvent):void
 {
 trace(event.toString());
 }
 }
}

ImageMagick path need to be changed and wow. It works perfectly. My app executes convert.exe and my desired output is generated.

Wait a minute…

Now what you need. AIR 2.0 SDK, AIR 2.0 runtime, ImageMagick and flash CS4 (latest one).

Your app xml would look like this…

<?xml version =”1.0″ encoding=”utf-8″ ?><application xmlns=”http://ns.adobe.com/air/application/2.0″><id>com.adobe.example.nativeprocess</id><supportedProfiles>extendedDesktop</supportedProfiles><version>1.0</version><filename>nativeprocess</filename><description></description><!– To localize the description, use the following format for the description element.<description><text xml:lang=”en”>English App description goes here</text><text xml:lang=”fr”>French App description goes here</text><text xml:lang=”ja”>Japanese App description goes here</text></description>–><name>nativeprocess</name><!– To localize the name, use the following format for the name element.<name><text xml:lang=”en”>English App name goes here</text><text xml:lang=”fr”>French App name goes here</text><text xml:lang=”ja”>Japanese App name goes here</text></name>–><copyright></copyright><initialWindow><content>nativeprocess.swf</content><systemChrome>standard</systemChrome><transparent>false</transparent><visible>true</visible></initialWindow><icon></icon><customUpdateUI>false</customUpdateUI><allowBrowserInvocation>false</allowBrowserInvocation></application>

Now compile. If all well, you’ll get a converted image. You can also create preformatted commands in actionscript to perform actions.

Thats it for now. See you soon.