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 ZIP-file containing the code, PAD file, background image and a PGC file containing strokes from the pages specified in the PAD file. 

Implementation
ASH.java

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:

<span class="wysiwyg-font-size-small">//import java standard libs  import java.io.*;  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; </span>

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:

<span class="wysiwyg-font-size-small">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 = PenHome.read(new 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)pageIter.next();                   //Add the page to the renderer   				</span>

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:

<span class="wysiwyg-font-size-small"> 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...           }       }  } </span>

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 the PAD file, background image and PGC file. Then you should only have to compile and run the application.

Download the source code from here.

Still need help? Contact Us Contact Us