Burn Annotations to an Image - Android Java

This tutorial shows how to burn annotations inside an AnnContainer onto an image in an Android Java application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to burn annotations to an image in an Android application.
Completion Time 30 minutes
Android Studio Project Download tutorial project (538 KB)
Platform Android (Java)
IDE Android Studio
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project and displaying an image by reviewing the Add References and Set a License and Display Images in an Image Viewer tutorial, before working on the Burn Annotations to an Image - Android Java tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Display Images in an Image Viewer tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. This project requires the following .JAR files:

The .JAR files can be found at: <INSTALL_DIR>\Bin\Java

For a complete list of which .JAR files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Set the Application Layout

In the Project Explorer window, open the activity_main.xml file found in the app/src/main/res/layout directory. Replace the button code with the XML below.

android:id="@+id/button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Annotate" 
android:onClick="addAnnotation"/> 

Add the Burn Annotations Code

Navigate to MainActivity.java and add the following import statements before the MainActivity class.

Java
import leadtools.annotations.engine.AnnContainerMapper; 
import leadtools.annotations.rendering.AnnAndroidRenderingEngine; 
import leadtools.LeadLengthD; 
import leadtools.LeadRectD; 
import leadtools.annotations.engine.AnnContainer; 
import leadtools.annotations.engine.AnnRectangleObject; 
import leadtools.annotations.engine.AnnSolidColorBrush; 
import leadtools.annotations.engine.AnnStroke; 

Add the following member variables.

Java
private AnnContainer container; 
private AnnRectangleObject rectObj; 

Update the onCreate() function as shown below.

Java
 @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        try{ 
            Platform.setLibPath(sharedLibsPath); 
            Platform.loadLibrary(LTLibrary.LEADTOOLS); 
            Platform.loadLibrary(LTLibrary.CODECS); 
        } 
        catch(Exception ex) { 
            Log.d(TAG,"Failed to load LEADTOOLS Native libraries" ); 
        } 
         
        mViewer = (RasterImageViewer)findViewById(R.id.rasterimageviewer); 
        mViewer.setTouchInteractiveMode(new ImageViewerPanZoomInteractiveMode()); 
        codecs = new RasterCodecs(); 
        intent = new Intent(); 
    } 

Add the following function to enable burning an annotation to an image.

Java
public void onSelectImage(View v) 
{ 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    activityResultLaunch.launch(intent); 
} 
 
public void addAnnotation(View v ) 
{ 
    try{ 
        rectObj.setRect(LeadRectD.create(0,0,1000,1000)); 
        rectObj.setStroke(AnnStroke.create(AnnSolidColorBrush.create("Blue"), LeadLengthD.create(2))); 
        rectObj.setFill(AnnSolidColorBrush.create("Yellow")); 
        container.getChildren().add(rectObj); 
 
        RasterImage image = mViewer.getImage(); 
        Bitmap imageBitmap = mViewer.getImageBitmap(); 
        Canvas canvas = new Canvas(imageBitmap); 
        LeadSizeD imageSize = mViewer.getImageSize(); 
 
        int xRes = image.getXResolution(); 
        int yRes = image.getYResolution(); 
        AnnContainerMapper mapper = new AnnContainerMapper(xRes, yRes, xRes, yRes); 
        mapper.updateDestinationRectangle(new LeadRectD(0, 0, imageBitmap.getWidth(), imageBitmap.getHeight()), imageSize); 
        container.setMapper(mapper); 
 
        AnnAndroidRenderingEngine engine = new AnnAndroidRenderingEngine(container, canvas); 
        engine.burn(); 
        engine.detach(); 
    } 
    catch(Exception ex){ 
        Toast.makeText(this, "Load a file, then annotate", Toast.LENGTH_LONG).show(); 
    } 
} 
 
ActivityResultLauncher<Intent> activityResultLaunch = registerForActivityResult( 
    new ActivityResultContracts.StartActivityForResult(), 
    new ActivityResultCallback<ActivityResult>() { 
        @Override 
        public void onActivityResult(ActivityResult result) { 
            if (result.getResultCode() == RESULT_OK) { 
                try { 
                    Intent data = result.getData(); 
                    Uri imageUri = data.getData(); 
 
                    ILeadStream stream = LeadStreamFactory.create(getContentResolver().openInputStream(imageUri), true); 
                    RasterImage image = codecs.load(stream); 
                    mViewer.setImage(image); 
                }  
                catch (Exception ex) { 
                    Context context = getApplicationContext(); 
                    Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show(); 
                } 
            } 
        } 
    }); 

Run the Application

Press Shift + F10 to run the application.

Wrap-up

This tutorial showed how to draw an annotation and burn the annotation to an image. In addition, it showed how to use the AnnContainer, AnnAndroidRenderingEngine, and AnnRectangleObject classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.