Network SDK for Java - How to handle copied pattern

This example shows how to handle forms and strokes for copied pattern. In this case the pgc data is transferred from the pen to PC via Bluetooth by ticking the pidget “send book to pc”. This will have some impact on how we extract pages from the pgc file in the code.

Implementation

The general structure of this code is similar to the simple ash example. If you are new to the Network SDK for Java it’s therefore recommended to read this example first since it contains more comments.

CopiedPatternAsh.java

<span class="wysiwyg-font-size-small">//Import java standard lib  import java.io.*;  import java.util.*;  //Import anoto api  import com.anoto.api.*;    public class CopiedExample  {       //Define paths to files and application name       private static final String BASE_PATH = "C:\\development\\";       private static final String PAD_FILE = BASE_PATH + "21.0.15.0.pad";       private static final String BG_IMAGE = BASE_PATH + "Demo_copied_pattern_1_200.png";       private static final String APPL_NAME = "testApp";       private static final String PGC_FILE = BASE_PATH + "bluetooth_pgc.pgc";         //Empty constructor       public CopiedExample() {}         public static void main(String[] args)       {           //Create a stroke renderer for rendering forms           Renderer strokeRenderer;             try           {               //Register a pad file for our application               PenHome.registerPad(APPL_NAME, PAD_FILE);               //Create a new Pen object from a PGC file               Pen myPen = PenHome.read(new FileInputStream(PGC_FILE), APPL_NAME);   </span>

Since the pgc files have been transferred via Bluetooth and we have triggered the transfer by ticking the “Send book to PC” pidget we can use the method getMagicBoxPage() of the Pen class to determine the page containing logical pages. We can extract logical pages and assign them to an Iterator by calling getLogicalPages() of the Page class. From each page in the Iterator we create a new Page and render an image (the process is exactly the same as in the simple ash example).

<span class="wysiwyg-font-size-small">//Find the page with the pidget used to trigger the data transfer.               //Note that getMagicBoxPage() will not work with the pidget               //MagicBoxDone.               String address = myPen.getMagicBoxPage();               //Create a new page object from address               Page page = myPen.getPage(address);               //Extract all logical pages               Iterator myLogicalPages = page.getLogicalPages();                 //Counter to keep track on the number of logical pages               int logicalPageNr = 0;                 //For each logical page availiable               while (myLogicalPages!=null && myLogicalPages.hasNext())               {                   //Create a new Page from next element in Iterator                   Page logicalPage = (Page)myLogicalPages.next();                     //Add a new page to the renderer                   strokeRenderer = RendererFactory.create(logicalPage);                   //Add a background image                   strokeRenderer.setBackground(BG_IMAGE);                   //Render page to a png image                   strokeRenderer.renderToFile(BASE_PATH + "output_Address_" +                       logicalPage.getPageAddress() + "_logicalPageNr_" +                       logicalPageNr +".png");                   logicalPageNr++;               }           }           catch (Exception e)           {               //Something went wrong           }       }  } </span>

To test the code edit the BASE_PATH so that the application can access the pad file, background image and pgc file. Compile and run.

Still need help? Contact Us Contact Us