Friday, 30 of July of 2010

Category » Uncategorized

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.


Fisix engine vector class not working with flex.

While working with fisix engine with flex, I stucked in a situation…

1067: Implicit coercion of a value of type __AS3__.vec:Vector to an unrelated type com.fileitup.fisixengine.core:Vector.

I checked http://blog.maxkunz.de/?p=170 for the solution. Yeah, good solution but not worked on my problem.

There is another solution. Go to compiler option and add “-target-player=9.0.0″ as shown below.

Compiler option

Compiler option


Do your comment

Flex repeater control part 2

Return back again.

In this section we’ll see how to retrieve repeater item and its data.

Lets have our example…

<mx:VBox id="box" height="100%" width="100%">
     <mx:Spacer height="10"/>
     <mx:Repeater id="r" dataProvider="{xmlColMenu}" startingIndex="0" recycleChildren="true">
         <mx:LinkButton id="idx" label="{r.currentItem.name}" click="{clbClick(event);}" styleName="RightLink"/>
         <mx:Repeater id="s" dataProvider="{r.currentItem.link}" 
             startingIndex="0" recycleChildren="true">
            <mx:LinkButton id="idy" label="{s.currentItem.name}"
                click="{clbClick(event);}" fontFamily="Verdana"
                fontSize="9" fontStyle="normal" fontWeight="normal"
                textSelectedColor="#03598B" textRollOverColor="#47A318" disabledColor="#47A318" height="15"
                color="#666666" themeColor="#FFFFFF"/>
         </mx:Repeater>
     </mx:Repeater>
 </mx:VBox>

Now we will write clbClick event for the repeater item. Indeed, your event handler will be written in <mx:Script> block.

/**
 * Navigation item click event.
 * @param event of type MouseEvent.
 */
 private function clbClick(event:MouseEvent):void
 {
     trace(event.target.getRepeaterItem().id);
 }

If you use getRepeaterItem(), it gives you access to the data item that you’d set earlier while repeating the item.

<mx:LinkButton id="idx" label="{r.currentItem.name}" click="{clbClick(event);}" styleName="RightLink"/>

In my case, I need to access id. You may have different data.

In next section we’ll check how to update repeater control dynamically. Till then c. y.


Do your comment

Flex repeater control part1

Repeater control is very useful but little bit complex to understand.

Why flex introduced this repeater control. Very simple. To avoid unnecessary looping and to reduce complexity of component/s or control/s repeatation.

In simple way, we can do the same repeatation by using for loop or while or do-while loop. Repeater control saves our time.

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*">

  <mx:Script>
    <![CDATA[
      [Bindable]
      public var myArray:Array=[1,2,3,4];
    ]]>
  </mx:Script>

  <mx:ArrayCollection id="myAC" source="{myArray}"/>
  <mx:Repeater id="myrep" dataProvider="{myAC}">
    <mx:Label id="Label1" text="This is loop #{myrep.currentItem}"/>
  </mx:Repeater>
</mx:Application>

This is a simple example how the repeater component is used. Please note here that the repeater control is itself a loop so don’t need to use another loop to support the control.
We can also use nested repeater controls. Just like example below.

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*">

  <mx:Script>
    <![CDATA[
      [Bindable]
      public var myArray:Array=[1,2,3,4];
    ]]>
  </mx:Script>

  <mx:ArrayCollection id="myAC" source="{myArray}"/>
  <mx:Repeater id="myrep" dataProvider="{myAC}">
    <mx:Label id="Label1" text="This is loop #{myrep.currentItem}"/>
    <mx:Repeater id="nestedrep" dataProvider="{myrep.currentItem.secondaryXMLList}">
        <mx:Label id="Label2" text="This is loop #{nestedrep.currentItem}"/>
     </mx:Repeater>  
   </mx:Repeater>
</mx:Application>

In next series, we’ll see how to retrieve data from repeated controls or components. We’ll also see controlling and refreshing the repeater component.

ciao.


Do your comment

The site name is now very simple

The site name is changed from pravinranjan.com to pranjan.com.

Its simple to say now pranjan [p r a n j a n].
:)


Do your comment

The site name is now very simple

The site name is changed from pravinranjan.com to pranjan.com.

Its simple to say now pranjan [p r a n j a n].
:)


Do your comment

Download MathFP jar file

You can download MathFP jar file from here. For more information visit its parent website http://home.comcast.net/~ohommes/MathFP/


1 comment

How to use MathFP for j2me

So here we are….

1. Download MathFP for CLDC.
2. Extract MathFP class.
3. Create net/jscience/util directory and then paste your extracted MathFP class file.
4. Right click the net directory and create a zip file. Mac and linux users should take care that archive top folder must be net.
[You can rename the zip file into jar and it will work.]

5. Copy and paste your newely created file to use it. Thats it.

Another way is to create a jar file but also you will need to use points 1, 2 and 3.

Please do write me if you’ll need. Have a nice time.


Do your comment

Async JPEG Encoder From Derrick Grigg

If you want to export big jpeg images from your AIR based application and hang your computer for a while in exporting. Try to use Derrick Grigg’s new async jpeg encoder. Nice utility class and you’ll certainly love it.


2 comments

Nice transformation tool. Grab it now…

Jack Doyle has launched a new transform manager class that would certainly reduce at least 20% weight from a developer back.
The tool is quite handy and don’t need much attention.

Features are simple and much easier. Boundary constraints, deletion and easy implementation are few great things that can touch you.

Certainly it worth it more than its price.

@Jack, You have done great. Thumbs up.

So don’t wait...just get it from here.

[http://blog.greensock.com/transformmanageras3/]

jd


5 comments