Take the following steps to start a project and add some code that demonstrates the automated annotation features of the LEADTOOLS Android Annotations.
In the Project Explorer window, right-click on the AndroidAutomation project and select Properties... from the context menu. In the Project Properties dialog box, select the Java Build Path node, and then select the Libraries tab and click Add External JARs. Select the following .jar files:
In the Order and Export tab, make sure all the check boxes of the added jar files are checked.
In the Project Explorer window, right-click on the AndroidAutomation project and select New -> Folder... from the context menu. Then type "libs" in the Folder name and click OK.
| CPU | Folder Name |
| Arm | armeabi |
| Arm-v7a | armeabi-v7a |
| Mips | mips |
| X86 | x86 |
Copy the following lib to the target cpu folders:
In the Project Explorer window, select res/layout/*activity_main.xml* file, and replace its content with the following:
[XAML]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:weightSum="1.0"android:background="@android:color/black"><HorizontalScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="horizontal"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Load Annotations"android:onClick="onLoadAnnotations"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Save Annotations"android:onClick="onSaveAnnotations"/><Buttonandroid:id="@+id/btn_runmode"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Run Mode"android:onClick="onAnnotationsModeChanged"/><Buttonandroid:id="@+id/btn_designmode"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Design Mode"android:onClick="onAnnotationsModeChanged"/></LinearLayout></HorizontalScrollView><RelativeLayoutandroid:id="@+id/relativelayout"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight=".96" ><ImageViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/white"android:src="@drawable/yourfile" /></RelativeLayout><HorizontalScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="horizontal"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Select"android:tag="-1"android:onClick="onAnnotationsObjectChanged"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Pointer"android:tag="-9"android:onClick="onAnnotationsObjectChanged"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Rectangle"android:tag="-3"android:onClick="onAnnotationsObjectChanged"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Text"android:tag="-12"android:onClick="onAnnotationsObjectChanged"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Ruler"android:tag="-23"android:onClick="onAnnotationsObjectChanged"/></LinearLayout></HorizontalScrollView></LinearLayout>
In the previous code, you need to replace "yourfile" with the name of the file that will be drawn in the ImageViewer. To do so, perform the following steps: In the "Package Explorer", select your project, expand it. Now select "res", right-click it and select "New", and then select "Folder". In the folder name field enter "drawable" and then copy your file to that folder and replace its name into the activity_main.xml.
In the Project Explorer window, edit the src/leadtools.androidautomation/*MainActivity.java* file, and add the following lines before the Main activity class:
[Java]
import java.io.File;import java.io.FileWriter;import java.util.ArrayList;import leadtools.LeadMatrix;import leadtools.LeadPointD;import leadtools.LeadRectD;import leadtools.LeadSizeD;import leadtools.RasterImage;import leadtools.RasterSupport;import leadtools.annotations.automation.AnnAutomation;import leadtools.annotations.automation.AnnAutomationManager;import leadtools.annotations.core.AnnCodecs;import leadtools.annotations.core.AnnContainer;import leadtools.annotations.core.AnnEventListener;import leadtools.annotations.core.AnnFormat;import leadtools.annotations.core.AnnObject;import leadtools.annotations.core.AnnPointerEvent;import leadtools.annotations.core.AnnPointerListener;import leadtools.annotations.core.AnnRenderingEngine;import leadtools.annotations.core.AnnUserMode;import leadtools.annotations.core.IAnnAutomationControl;import leadtools.annotations.rendering.AnnAndroidRenderingEngine;import android.os.Environment;import android.content.Context;import android.graphics.Canvas;import android.view.MotionEvent;import android.view.View;import android.widget.RelativeLayout;import android.widget.Toast;
Add the following member variables to the MainActivity class:
[Java]
private AnnAutomation mAutomation;private final String mAnnFilePath = Environment.getExternalStorageDirectory() + "/LEAD_AnnFile.xml";
Update the onCreate() function as shown below:
[Java]
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativelayout);// Initialize supportRasterSupport.initialize(this);// Create automation manager objectAnnAutomationManager manager = new AnnAutomationManager();// Create the default annotations objectsmanager.createDefaultObjects();// Initialize custom automation controlAutomationControl automatioControl = new AutomationControl(this);layout.addView(automatioControl);mAutomation = new AnnAutomation(manager, automatioControl);mAutomation.setActive(true);
Add the following functions:
[Java]
public void onLoadAnnotations(View v){if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){return;}// Check if the file exist before loadFile file = new File(mAnnFilePath);if(file.exists()){// Create AnnCodecs ObjectsAnnCodecs codecs = new AnnCodecs();// Load fileAnnContainer container = codecs.load(file, 1);// Clear the current annotation objectsmAutomation.getContainer().getChildren().clear();// add the loaded objectsfor(AnnObject annObject: container.getChildren()){mAutomation.getContainer().getChildren().add(annObject);}mAutomation.invalidate(LeadRectD.getEmpty());}}public void onSaveAnnotations(View v){if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){return;}File file = new File(mAnnFilePath);// if the file exist, delete it.if(file.exists())file.delete();try{// Save AnnotationsFileWriter fw = new FileWriter(mAnnFilePath);AnnCodecs codecs = new AnnCodecs();codecs.save(fw, mAutomation.getContainer(), AnnFormat.ANNOTATIONS, null, 0);}catch(Exception ex){Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();}}public void onAnnotationsModeChanged(View v){// Set annotation user modeAnnUserMode mode = v.getId() == R.id.btn_runmode ? AnnUserMode.RUN : AnnUserMode.DESIGN;mAutomation.getManager().setUserMode(mode);}public void onAnnotationsObjectChanged(View v){// set current objectint objectIndex = Integer.parseInt((String)v.getTag());mAutomation.getManager().setCurrentObjectId(objectIndex);}// Custom automation controlprivate class AutomationControl extends View implements IAnnAutomationControl{private AnnContainer mContainer = null;private AnnAndroidRenderingEngine mEngine = null;private LeadPointD mOffset = new LeadPointD(0, 0);private ArrayList<AnnPointerListener> mAutomationTouchListenersList = new ArrayList<AnnPointerListener>();public AutomationControl(Context context){super(context);}@Overridepublic void onDraw(Canvas canvas){super.onDraw(canvas);mEngine.setContext(canvas);mEngine.render(LeadRectD.getEmpty());mEngine.setContext(null);}@Overridepublic boolean onTouchEvent(MotionEvent event){switch(event.getAction()){case MotionEvent.ACTION_DOWN:AnnPointerEvent annPointerDownEvent = new AnnPointerEvent(this, getAutomationContainer().getMapper().pointToContainerCoordinates(new LeadPointD(event.getX(), event.getY())));for(AnnPointerListener listener: mAutomationTouchListenersList)listener.onPointerDown(annPointerDownEvent);this.automationInvalidate(LeadRectD.getEmpty());break;case MotionEvent.ACTION_MOVE:AnnPointerEvent annPointerMoveEvent = new AnnPointerEvent(this, getAutomationContainer().getMapper().pointToContainerCoordinates(new LeadPointD(event.getX(), event.getY())));for(AnnPointerListener listener: mAutomationTouchListenersList)listener.onPointerMove(annPointerMoveEvent);this.automationInvalidate(LeadRectD.getEmpty());break;case MotionEvent.ACTION_UP:AnnPointerEvent annPointerUpEvent = new AnnPointerEvent(this, getAutomationContainer().getMapper().pointToContainerCoordinates(new LeadPointD(event.getX(), event.getY())));for(AnnPointerListener listener: mAutomationTouchListenersList)listener.onPointerUp(annPointerUpEvent);this.automationInvalidate(LeadRectD.getEmpty());break;}return true;}@Overridepublic void addAutomationEnabledChangedListener(AnnEventListener listener){}@Overridepublic void addAutomationGotFocusListener(AnnEventListener listener){}@Overridepublic void addAutomationLostFocusListener(AnnEventListener listener){}@Overridepublic void addAutomationSizeChangedListener(AnnEventListener listener){}@Overridepublic void addAutomationTouchListener(AnnPointerListener listener){if(!mAutomationTouchListenersList.contains(listener))mAutomationTouchListenersList.add(listener);}@Overridepublic void addAutomationTransformChangedListener(AnnEventListener listener){}@Overridepublic void addAutomationUseDpiChangedListener(AnnEventListener listener){}@Overridepublic void automationAttach(AnnContainer container){mContainer = container;mEngine = new AnnAndroidRenderingEngine(mContainer, null);invalidate();}@Overridepublic void automationDetach(){mContainer = null;}@Overridepublic void automationInvalidate(LeadRectD rc){invalidate();}@Overridepublic AnnContainer getAutomationContainer(){return mContainer;}@Overridepublic double getAutomationDpiX(){return 96.0;}@Overridepublic double getAutomationDpiY(){return 96.0;}@Overridepublic boolean getAutomationEnabled(){return true;}@Overridepublic LeadPointD getAutomationOffset(){return mOffset;}@Overridepublic LeadSizeD getAutomationSize(){return new LeadSizeD(getWidth(), getHeight());}@Overridepublic LeadMatrix getAutomationTransform(){return LeadMatrix.getIdentity();}@Overridepublic boolean getAutomationUseDpi(){return false;}@Overridepublic double getAutomationXResolution(){return 96.0;}@Overridepublic double getAutomationYResolution(){return 96.0;}@Overridepublic RasterImage getImage(){return null;}@Overridepublic AnnRenderingEngine getRenderingEngine(){return mEngine;}@Overridepublic void removeAutomationEnabledChangedListener(AnnEventListener listener){}@Overridepublic void removeAutomationGotFocusListener(AnnEventListener listener){}@Overridepublic void removeAutomationLostFocusListener(AnnEventListener listener){}@Overridepublic void removeAutomationSizeChangedListener(AnnEventListener listener){}@Overridepublic void removeAutomationTouchListener(AnnPointerListener listener){if(mAutomationTouchListenersList.contains(listener))mAutomationTouchListenersList.remove(listener);}@Overridepublic void removeAutomationTransformChangedListener(AnnEventListener listener){}@Overridepublic void removeAutomationUseDpiChangedListener(AnnEventListener listener){}@Overridepublic void setRenderingEngine(AnnRenderingEngine engine){mEngine = (AnnAndroidRenderingEngine)((engine instanceof AnnAndroidRenderingEngine) ? engine : null);}}
Add the following line to the AndroidManifest.xml before the application tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />