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:
CommandList is the code that contains a list of commands that will be executed sequentially before or after the MediaCollection method. CommandList also examines the results of a command and determines the next action to perform.
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:
CONTINUE. Continue processing the next command or invoke the method (if no more commands are specified).
ABORT. Exit the command sequence without performing an action.
RETURN. Exit the command and return a value.
mediaCollection.analyze is the method containing the before and after commands. After the before command method is executed, the specified EDF will be analyzed.
edf represents the image against which you want to perform a command method and analysis.
the location of commandList in the line indicates that you want to perform the command before the method is passed to the eVe engine.
the location of null in the line indicates that there are no after method commands to perform
The following is the actual code found in the GrayScaleCommand command method. It will perform the following actions:
pull an image from a MediaObject
turn that image into a gray scale image
place the image back into the MediaObject
pass the MediaObject to be analyzed
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:
Basecommand contains the basic parameters that the commandList references to execute the command method. This includes input parameters, output parameters (the results of the command), and an execute command. To enable before and after commands, you must subclass the Basecommand within your command method code.