How To: Create a Custom Image Reader with MapObjects-Java


Intstructions provided describe how to create a Custom Image Reader with MapObjects-Java. MapObjects-Java allows developers to create and plug-in custom image file handlers in an application to display new image formats on the map.


All the Image Readers implement the interface com.esri.mo2.map.img.ImageReader. All methods within this interface must be properly defined to successfully draw the image on the map.

This article describes how to implement each of these methods within a CustomImageReader and should be viewed in conjunction with the javadoc for the interface.

public class CustomImageReader implements com.esri.mo2.img.ImageReader {

  • In the CustomImageReader constructor, initialize an array of type String with the possible extensions of the image format. For a JPG image it could be 'jpg' or 'jpeg'. This array is returned by the method:

    public String[] getListOfExtension()

  • Return a new object of the the ImageHandler being created with the createInstance method:

    public ImageReader createInstance() {
    return new CustomImageReader();

  • Define a description for the ImageReader with the getDescription method:

    public String getDescription(){
    return "Supports the CUSTOM image format";

  • Use getFilename to set the input source to use the given input stream. In the following method, '_filename' is a global attribute:

    public String getFilename() {
    return _filename;

    The setFilename can also be implemented in a similar manner.

  • Use the getWidth and getHeight methods to return the width and height, in pixels, of the image from the image header.
  • _filename returned from getFilename() can be used to pass the selected file to the readHeader method that handles the image header. readHeader() returns true after successfully reading the image header. The implementation of this method depends on the image file format specification.
  • The getProjection method should return the projection to be applied to the image data on the map. If it returns null, then the standard WGS84 geographic projection datum is applied. The value returned in this method should meet the projection string format required by MapObjects-Java.
  • The getAffineTransform method returns the AffineTransform object for the image. To set the correct AffineTransform, the scale in both the X and Y direction must be known. See the following code:

    public AffineTransform getAffineTransform() {
    double values[] = new double[6];
    double scale = dem.yRange/_width;
    values[0] = scaleX;
    values[1] = 0;
    values[2] = 0;
    values[3] = scaleY;
    values[4] = top_left_x;
    values[5] = top_left_y;
    values[4] -= 0.5 * values[0]; // half pixel correction
    values[5] -= 0.5 * values[3];
    return new AffineTransform(values);

    In this code, the scaleX is the scale in the horizontal direction. This can be calculated by computing the X range of the image and dividing it by the width of the image. ScaleY is the scale in vertical direction. The top_left_x and top_left_y are the X and Y values of the top left corner point.
  • The readImageBody method is the main method that creates an image. This method returns a javax.media.jai.TiledImage that is created using a java.awt.image.BufferedImage. This BufferedImage is created from the file specification. This step completes the creation of the CustomImageReader. It must now be exposed so that these custom contents can be identified using the ContentChooser dialog and added to the map.
  • To expose the CustomImageReader, verify the following steps have been performed:

    a) Create a META-INF/services directory with a text file in this location. Name the file com.esri.mo2.map.img.ImageReader.

    b) Inside the file, write the full path to the CustomImageReader created in earlier steps. For example, the path might be com.xxx.yyy.CustomImageReader.

    c) Jar all the necessary files and place it in the application class path. This allowx the application to automatically pick up the plug-in custom file reader. The contents of the jar file should appear as follows: