Network SDK for Java - Developing a simple ASH

This example describes how to build a very simple Application Service Handler (ASH). The ASH will load a stored pgc file and render the strokes to a png image based on a pad file for unique pattern.
At the end of this page you can download a zipfile containing the code, pad file, background image and a pgc file containing strokes from the pages specified in the pad file.


First we have to include the Anoto API and some other useful libraries (make sure that you have placed the Anoto API Jar file so that it can be reached by your development environment). We also need to specify some directories and files used later on in the code.

//import java standard libs  import*;  import java.util.*;    //Import anoto java api  import com.anoto.api.*;    /**  * Create a new class Ash  */  public class Ash {         //Define paths to files and application name       private final static String BASE_PATH = "C:\\development\\";       private final static String PAD_FILE = BASE_PATH + "169.1.11.X.pad";       private final static String BG_IMAGE = BASE_PATH + "formula1_1_85.png";       private final static String PGC_FILE = BASE_PATH + "myPGC.pgc";       private final static String APPL_NAME = "test1";         //Empty constructor       public Ash() {}         /**       * Main method       */       public static void main(String[] args)       {          //Create a renderer for rendering strokes to an image           Renderer strokeRenderer; 

Before we can access any pages in the pgc file we need to load a valid pad file (a pad file containing a description of the pages in the pgc file). When the pad file is loaded we can create a Pen object and access the data in the pgc file. The page(s) in the pgc file can be accessed by retrieving an Iterator from the getPages() method in the pen class.

try           {               //Load the correct pad file               PenHome.loadPad(APPL_NAME, new File(PAD_FILE));               //Create a new pen object and read the strokes from the pgc file               Pen myPen = FileInputStream(PGC_FILE), APPL_NAME);               //Extract all pages from the pen object               Iterator pageIter = myPen.getPages();               //Loop through all pages               while (pageIter != null && pageIter.hasNext())               {                   //Create a new page                   Page newPage = (Page);                   //Add the page to the renderer   				

We assign each page in the Iterator file to a Renderer using the create() method in the RendererFactory class. After adding a background image we can render the strokes in the page to a png file.

 strokeRenderer = RendererFactory.create(newPage);                   //Add a background image                   strokeRenderer.setBackground(BG_IMAGE);                   String fileName = BASE_PATH + "output_" +                       newPage.getPageAddress() + ".png";                   //Render page to a png image                   strokeRenderer.renderToFile(fileName);               }           }           catch (Exception e)           {               //Something went wrong...           }       }  } 

Now we should have a working but minimalistic Ash that can handle pen data and render output images. You need to edit the BASE_PATH to the directory containing pad file, background image and pgc file. Then you should only have to compile and run the application.

Download source code

Still need help? Contact Us Contact Us