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 |
|
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.
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
Leadtools.jar
Leadtools.codecs.jar
Leadtools.controls.android.jar
Leadtools.caching.jar
Leadtools.converters.android.jar
The .SO
files can be found at: <INSTALL_DIR>\Bin\Android
libleadtools.so
libleadtools.codecs.so
libleadtools.codecs.bmp.so
libleadtools.codecs.cmp.so
libleadtools.codecs.fax.so
libleadtools.codecs.png.so
libleadtools.codecs.tif.so
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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.
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:
<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>
In MainActivity.java
, add the following import statements before the MainActivity
class:
import leadtools.codecs.CodecsSavePageMode;
import leadtools.imageprocessing.CloneCommand;
Add the following member variables to the MainActivity class
private boolean doMerge = true;
private CloneCommand ccommand;
Update the onCreate()
function as shown below:
@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.
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();
}
}
}
splitFiles()
function and add the code below to split the pages into new separate files.
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();
}
}
Press Shift + F10 to run the application. Follow the steps below to test the application.
INPUT FILE
button to load a file.SPLIT FILE
button to split the multipage file.MERGE FILE
button, a prompt will provide an indicator to add a new file.Select the INPUT FILE
button to load in a new file, then select the MERGE FILE
button again to combine the two files.
This tutorial shows how to merge and split input files using LEADTOOLS in a Java Android application.