Using Before and Command Methods

 

The methods in the MediaCollection interface of the low-level API allow you to execute commands before and after the action performed by those methods. To do this, each method within MediaCollection accepts the CommandList beforeMethods and CommandList afterMethods parameters. These parameters call a command list before or after a method is run and execute the commands(s) included in that list. You can use before and after command methods to perform actions against an image (EDF) that is specified within a method.

 

The basic steps for incorporating before and after commands within methods are:

 

1. Write the command you want to perform.

 

2. Place the command into the commandList.

 

3. Incorporate a call to the commands within a MediaCollection method.

 

In the following example, we created a before command method that will gray scale an image before it goes to analysis. This command method, named GrayScaleCommand, is included within a mediaCollection.analyze method call. This first section of code calls the command method and passes an EDF to that command.

 

.

.

.

MediaObject edf = (MediaObject) Eve.newMediaObject();

System.out.println("Loading JPG: " + files[i]);

edf.loadImage(directory + Eve.directorySeparator + files[i]);

edf.setProperty("originalDirectory",directory);

edf.setProperty("originalFilename",files[i]);

 

CommandList commandList = context.newCommandList();

commandList.setContext(context);

commandList.add("com.evisionglobal.eve.commands.GrayScaleCommand");

 

try

{

mediaCollection.analyze(edf,commandList,null);

}

catch (Exception e)

{

  System.out.println("Error During Analyze: " + e);

  System.exit(0);

.

.

.

 

where:

 

 

public interface CommandList

{

    final int CONTINUE = 0;

    final int RETURN = 1;

final int ABORT = 2;

 

There are three possible response codes from the result of a command:

 

 

 

 

 

 

 

 

The following is the actual code found in the GrayScaleCommand command method. It will perform the following actions:

 

 

package com.evisionglobal.eve.commands;

 

import com.evisionglobal.eve.*;

import com.evisionglobal.eve.kernel.*;

 

import java.awt.*;

import java.awt.image.*;

import java.awt.image.renderable.*;

 

import java.util.*;

import java.io.*;

 

public class GrayScaleCommand extends BaseCommand

{

public void execute()

{

try

 {

//

// initialize

//

lastStatus = CONTINUE;

lastException = null;

//

// make sure we are being given the right stuff

//

if (parameter1 == null)

  {

    lastStatus = ABORT;

    lastException = new     EveException("GrayScaleCommand","execute","Null Parameter1");

    return;

   }

 

if (! (parameter1 instanceof MediaObject))

   {

      lastStatus = ABORT;

      lastException = new EveException("GrayScaleCommand","execute","Parameter1 Not MediaObject");

      return;

   }

 

MediaObject eve = (MediaObject) parameter1;

 

double red[][] = eve.getRedChannel();

double green[][] = eve.getGreenChannel();

double blue[][] = eve.getBlueChannel();

 

int i,j;

 

int width = red.length;

int height = red[0].length;

 

int pixels[] = new int[width * height];

 

for (i = 0; i < width; i++)

 

  for (j = 0; j < height; j++)

        {

         double temp = (0.30 * red[i][j]) + (0.59 * green[i][j]) + (

0.11 * blue[i][j]);

         red[i][j] = green[i][j] = blue[i][j] = temp;

         pixels[j * width + i] = (new Color((int) temp,(int) temp,(int) temp)).getRGB();

        }

 

Image newImage = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width,height,pixels,0,width));

ByteArrayOutputStream stream = new ByteArrayOutputStream(width * height);

JpegEncoder encoder = new JpegEncoder(newImage,100,stream);

encoder.Compress();

byte buffer[] = stream.toByteArray();

eve.setColorPlanes(red,green,blue);

eve.setProperty("image",buffer);

eve.setProperty("GrayScaleCommand","Executed");

}

catch (Exception e)

{

lastStatus = ABORT;

lastException = new EveException("GrayScaleCommand","execute",e);

}

}

}

 

where: