Skip to Content

A question we frequently get asked is, “How do I display images from my catalog in a web page?”.

The MDM API’s provide an easy way to extract images from a repository, although their use isn’t straightforward at first glance. In this blog I will demonstrate how to extract images using the COM and Java API’s. Once you understand the way this works, you should be able to extract any other BLOB information from your own repositories – video, sound, etc.

The API’s provide two ways to get images from a repository. The first way involves calling the function

Catalog.GetImage()

(COM), or

CatalogData.GetImage()

(Java). Both these functions return an array of bytes, which is the image itself. This, however, is an inefficient way to display repository images since the image needs to be extracted from the database every time it is requested. Fortunately, the API’s provide a caching mechanism whereby images are extracted to a folder on the hard drive and the physical path of these images can be obtained. When an image is requested, the API extracts the image from the database only if it has not already been extracted to the cache folder.

The sample code was written and tested against the InQuera demo repository (www.inquera.com ). However, it is easily adaptible to any repository schema. In this particular example, the main table is called “Products”, the image field in the main table is called “Images” and is multi-valued.

     class DemoClass

     {

          

          static void Main(string[] args)

          {

               // Define the cache path. This directory must have appropriate read, write and delete permissions.

               String CACHE_DIRECTORY = “C:
Temp
CachedImages”;

               Catalog catalog = new CatalogClass();

               catalog.Login(“localhost”, 5080, “admin”, “”, “English “, 5, 5, 3600);

               // MUST call the InitCache function!

               catalog.InitCache(CACHE_DIRECTORY, -1);

               // Get a result set, and extract the first image from the first product found

               // with non-null image values.

               Search search = new SearchClass();

               search.SearchTable = “Products”;

               ResultSetDefinition rsd = new ResultSetDefinitionClass();

               rsd.Table = “Products”;

               rsd.Fields.Append(“Images”, null);

               ResultSet rs = catalog.GetResultSet(search, rsd, “Images”, true, 0);

               for(; !rs.EOF; rs.MoveNext())

               {

                    if( rs.Fields.Item(“Images”).Value != null )

                    {

                         object values = rs.Fields.Item(“Images”).Value;

                         System.Array arr = (System.Array) values;

                         int id = (int) arr.GetValue(0);

                         CachedImageInfo imageInfo = catalog.GetCachedImageInfo(“Images”, id, “Thumbnail”);

                         Console.WriteLine(“The image is at: ” + CACHE_DIRECTORY + “
” + imageInfo.CachedPath);

                         break;

                    }

               }

               // Remember to log out!

               catalog.Logout();

          }

     }

}

Java Console Application Using the Java API (MDM4J)


import a2i.cache.CatalogCache;

import a2i.common.A2iResultSet;

import a2i.common.CatalogData;

import a2i.common.ResultSetDefinition;

import a2i.core.A2iValueArray;

import a2i.core.StringException;

import a2i.search.Search;

/**

  • Demonstration – how to retrieve images from an MDM directory

*/

public class ImageDemo

{

     public static void main(String[] args)

     {

          // Define the cache path

          String CACHE_DIRECTORY = “C:
Temp
CachedImages”;

          

          // log in to the repository

          CatalogData catalogData = new CatalogData();

          catalogData.Login(“localhost”, 5080, “admin”, “”, “English “);

          

          // Init a CatalogCache object

          CatalogCache catalogCache = new CatalogCache();

          catalogCache.Init(“localhost”, 5080, “admin”, “”, CACHE_DIRECTORY, “English “);

          

          Search search = new Search(“Products”);

          ResultSetDefinition rsd = new ResultSetDefinition(“Products”);

          rsd.AddField(“Images”);

          

          // Get the products and extract the first image from the first

          // record with non-null Image value.

          try

          {

               A2iResultSet rs = catalogData.GetResultSet(search, rsd, null, true, 0);

               for(int i = 0; i < rs.GetRecordCount(); i++)

               {

                    if(!rs.GetValueAt(i, “Images”).IsNull())

                    {

                         A2iValueArray valueArray = rs.GetValueAt(i, “Images”).GetValueArray();

                         int id = valueArray.GetValueAt(0).GetIntValue();

                         String imagePath = catalogCache.GetImagePath(“Images”, “Thumbnail”, id);

                         System.out.println(“Your image is at: ” + CACHE_DIRECTORY + “
” + imagePath);

                         break;

                    }

               }

          }

          catch (StringException e)

          {

               e.printStackTrace();

          }

          finally

          {

               // Remember to shut down the cache!

               catalogCache.Shutdown();

               try

               {

                    // Remember to log out!

                    catalogData.Logout();

               }

               catch (StringException e)

               {

                    // do nothing

               }

          }

          

     }

}

</pre>

</p>

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Glenn Stevens
    Walter,

    Back when we started using mdm/a2i in 2002 we found that if two connections to the catalog on the same web server tried to access the same cache directory that the API would become unstable. Because of this we always found it much easier to display images/videos/pdfs from our MDM repository using the ObjectServer on our webservers. To be honest since you guys gave us the object server we’ve never had an reason to even try loading or caching images directly using the API’s.

    Have the earlier caching issues we experience been resolved in any of the subsequent versions?

    (0) 
    1. Walter Kahn Post author
      I am not sure whether the stability issues you experienced in the past are still present. You can probably write a small application to test this.

      In any case, use of the ObjectServer is preferable for applications with multiple simultaneous users (eg. web sites).

      Walter

      (0) 
  2. Martin Imme
    Hallo,

    thanks very much for this great blog. Is there going to be an updated version which takes the new Java APi into account?

    Thanks a lot,
    Martin

    (0) 
  3. Prabhakaran TK
    Walter,

    I am using the same code provided by you, but I am getting the below exception,

    java.lang.ClassCastException
         at a2i.core.Value.GetValueArray(Unknown Source)

    I am not able to figure out the reason.
    Why I am getting this exception?

    Thanks,
    Prabhakar.

    (0) 

Leave a Reply