Merge and Split Multipage Images - Java Android

This tutorial shows how to merge files into a multipage image and then save each page into separate image files using the LEADTOOLS SDK in a Java Android application.

Overview  
Summary This tutorial covers how to merge and split multipage image files in a Java Android application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (488 KB)
Platform Java (Android)
IDE Android Studio
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and Display Images in an Image Viewer tutorials, before working on the Merge and Split Multipage Images - Java Android tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License 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 tutorial requires the following .JAR and .SO files:

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

The .SO files can be found at: <INSTALL_DIR>\Bin\Android

For a complete list of which DLL 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:

Note

Adding LEADTOOLS local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Update Activity_main.xml file

In the Project Explorer window, open the activity_main.xml file found in the app/src/main/res/layout directory. Replace all the code present with the following:

Java
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1.0" 
    android:background="@android:color/darker_gray"> 
 
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Input file" 
        android:onClick="onSelectImage"/> 
 
    <leadtools.controls.RasterImageViewer 
        android:id="@+id/rasterimageviewer" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight=".96" 
        android:background="@android:color/white"/> 
 
    <Button 
        android:id="@+id/button" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Merge files" 
        android:onClick="mergeFiles"/> 
 
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Split file" 
        android:onClick="splitFiles"/> 
</LinearLayout> 

Update MainActivity.java the Code

In MainActivity.java, add the following import statements before the MainActivity class:

Java
import leadtools.codecs.CodecsSavePageMode; 
import leadtools.imageprocessing.CloneCommand; 

Add the following member variables to the MainActivity class

Java
private boolean doMerge = true; 
private CloneCommand ccommand; 

Update the onCreate() function as shown below:

Java
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
        codecs.getOptions().getLoad().setAllPages(true); 
        ccommand = new CloneCommand(); 
        ccommand.setAllPages(true); 
    } 

Create a mergeFiles() function and add the code below to allow the merging of files.

Java
public void mergeFiles(View v ) // Merge file 
{ 
    RasterImage image = mViewer.getImage(); 
 
if(image != null) 
    { 
        if(doMerge) {  
            ccommand.run(image); 
            doMerge = false; 
            Toast.makeText(this, "Insert another file to merge, then select merge to combine", Toast.LENGTH_LONG).show(); 
            return; 
        } 
        try 
        { 
            image.addPages(ccommand.getDestinationImage(),1,-1); 
 
            File file = new File(this.getFilesDir(), "combine.tif"); 
            String path = file.getAbsolutePath();  
            codecs.save(image, LeadStreamFactory.create(path), RasterImageFormat.TIF, 0); 
 
            Toast.makeText(this, "Saved to: "+ path, Toast.LENGTH_LONG).show(); 
        } 
        catch(Exception ex) 
        { 
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
    } 
} 
Create a splitFiles() function and add the code below to split the pages into new separate files.

Java
public void splitFiles(View v ) // Split file 
{ 
    try 
    { 
        RasterImage image = mViewer.getImage(); 
        if(image != null) 
        { 
            int pagecount = image.getPageCount(); 
            for(int page = 1; page <= pagecount; page++) 
            { 
                File file = new File(this.getFilesDir(), "output"+ page +".png"); 
                String path = file.getAbsolutePath();  
                codecs.save(image, LeadStreamFactory.create(path), RasterImageFormat.PNG, 0,page,-1,page, CodecsSavePageMode.APPEND); 
 
                Toast.makeText(this, "Saved to: "+ path, Toast.LENGTH_LONG).show(); 
            } 
        } 
    } 
    catch(Exception ex) 
    { 
        Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 

Run the Project

Press Shift + F10 to run the application. Follow the steps below to test the application.

Wrap-up

This tutorial shows how to merge and split input files using LEADTOOLS in a Java Android application.

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.