This tutorial shows how to use the LEADTOOLS SDK to create a Java application that loads and saves an image.
|Summary||This tutorial covers how to load and save an image in a Java application.|
|Completion Time||30 minutes|
|Visual Studio Project||Download tutorial project (1 KB)|
|IDE||Visual Studio 2017, 2019|
|[Runtime License]||Download LEADTOOLS|
Before working on the Load and Save an Image - Java tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.
In Eclipse, create a new Java project, and add the necessary LEADTOOLS references.
The references needed depend upon the purpose of the project. The following JAR files are needed for this tutorial:
The JAR files are located at
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:
Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License Tutorial.
With the project created, the references added, and the license set, coding can begin.
In the Package Explorer, open the
_Main.java class. Add two new methods to the
_Main class called
LoadImage(String filename, RasterCodecs codecs) and
SaveImage(RasterImage _image, String outputFile, RasterCodecs _codecs). call these methods inside the
main() method after the
SetLicense() call, putting the load call first.
Add the following
import statements to the import block at the top.
public static void main(String args) throws IOException
RasterCodecs codecs = new RasterCodecs();
String inputFile = "C:\\LEADTOOLS21\\Resources\\Images\\image1.cmp";
String outputFile = "C:\\LEADTOOLS21\\Resources\\Images\\test.jpg";
RasterImage image = LoadImage(inputFile, codecs);
SaveImage(image, outputFile, codecs);
Add the below code to load an image from file.
static RasterImage LoadImage(String inputFile, RasterCodecs codecs)
RasterImage _image = codecs.load(inputFile);
System.out.println("Image loaded successfully.");
Add the below code to save the
RasterImage loaded from CMP as a new JPEG file.
static void SaveImage(RasterImage _image, String outputFile, RasterCodecs _codecs)
_codecs.save(_image, outputFile, RasterImageFormat.JPEG, _image.getBitsPerPixel());
System.out.println("Image saved successfully.");
Run the project by selecting Run -> Run.
If the steps were followed correctly, the application loads the CMP image as a RasterImage and then exports to file as JPEG.
This tutorial showed how to add the necessary references to load and save an image. Also, it covered how to use the