Friday, 30 of July of 2010

News

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

Concept of float datatype in j2me

Nearly everyone would feel pains for float datatype in j2me. This article will give you a broad concept to implement or more precisely simulate float in j2me.

First of all, how a float data look like?

78.98, 34.98885, 3.141759 …. and like this.

If you look closer, you will find every float has very small variable unit.

Say 78.98. Its unit is 0.01.  Similarly 3.141759. Its unit is 0.000001.

To implement in j2me, we will have smallest unit is 1 and also this is fixed. So what to do?

First of all for every data we will need to pre-assume maximum length. This is so because it will help to choose appropriate type like short, int or long.

int speed = 0;

//Say we need speed similar to 0.5.

Next is enlarge our data as per necessity.

Now,

speed = speed+1;

To get simulated speed of 0.5, we need speed to divide by 2. So the next step will be…

speed = speed / 2;

Actually this will give you 2 consecutive same values like 1,1,2,2,3,3,4,4,….

The same concept will work for larger values.

That’s it for now.


Do your comment

Future of flash?

Adobe is going to release next version of Flex with the name of Flash.

Question is where is our old Flash?

Current versions has already lost its legend characteristics of Flash.

I am okay with flex. Good for business based applications. Lots of graphs, easy to use services, components and libraries, rapid development and many revolutionary things. Flash is separate thing to do things differently. Adobe is try to make it dot net kind of environment but I recall this to adobe that people have already things similar to flex but what they do not have is FLASH.

Another problem is Adobe is not keeping its promise to make flash platform independent. I am specifically targeting towards linux. More than a decade and flash or flex is still a dream for linux users.

Therefore, I request Adobe to not to loose legend core of flash.

[The article is not targeting flash environment but that Flash language and IDE]


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