Load and Save Images - Java

This tutorial shows how to use the LEADTOOLS SDK to create a Java application that loads and saves an image.

Overview
Summary This tutorial covers how to load and save an image in a Java application.
Completion Time 30 minutes
Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on theLoad and Save an Image - Javatutorial, get familiar with the basic steps of creating a project by reviewing theAdd References and Set a Licensetutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in theAdd References and Set a Licensetutorial. If that project is unavailable, follow the steps in that tutorial to create it.

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\LEADTOOLS22\Bin\Java

For a complete list of which JAR files are required for your application, refer toFiles to be Included with your Java 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 toSetting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in theAdd References and Set a Licensetutorial.

Add the Load Image Code

With the project created, the references added, and the license set, coding can begin.

In thePackage Explorer, open the_Main.javaclass. Add two new methods to the_Mainclass calledLoadImage(String filename, RasterCodecs codecs)andSaveImage(RasterImage _image, String outputFile, RasterCodecs _codecs). call these methods inside therun()method after theSetLicense()call, putting the load call first.

Add the following进口statements to the import block at the top.

Java
进口java.io.*;进口java.nio.file.Files;进口java.nio.file.Paths;进口leadtools.*;进口leadtools.codecs.*;
Java
publicstaticvoidmain(String[] args)throwsIOException{new_Main().run(args);}privatevoidrun(String[] args) {try{Platform.setLibPath("C:\\LEADTOOLS22\\Bin\\CDLL\\x64");Platform.loadLibrary(LTLibrary.LEADTOOLS);Platform.loadLibrary(LTLibrary.CODECS);SetLicense();RasterCodecs codecs =newRasterCodecs();String inputFile ="C:\\LEADTOOLS22\\Resources\\Images\\image1.cmp";String outputFile ="C:\\LEADTOOLS22\\Resources\\Images\\test.jpg";RasterImage image = LoadImage(inputFile, codecs);SaveImage(image, outputFile, codecs);// Dispose of resourcescodecs.dispose();image.dispose();}catch(Exception ex) {System.err.println(ex.getMessage());ex.printStackTrace();}}

Add the below code to load an image from file.

C#
RasterImage LoadImage(String inputFile, RasterCodecs codecs) throws IOException{// If wanting to load from stream//InputStream srcStream = Files.newInputStream(Paths.get(inputFile));//LeadStream inputStream = new LeadStream(srcStream, true);//RasterImage _image = codecs.load(inputStream);RasterImage _image = codecs.load(inputFile);System.out.println("Image loaded successfully.");return_image;}

Note

LEADTOOLS supports loading from memory streams. The commented out code above shows one way to load a stream into a RasterImage object.

Add the below code to save theRasterImageloaded from CMP as a new JPEG file.

Java
voidSaveImage(RasterImage _image, String outputFile, RasterCodecs _codecs)throwsIOException{// If wanting to save to stream//OutputStream outStream = Files.newOutputStream(Paths.get(outputFile));//LeadStream leadOutStream = new LeadStream(outStream, true);//_codecs.save(_image, leadOutStream, RasterImageFormat.JPEG, _image.getBitsPerPixel());_codecs.save(_image, outputFile, RasterImageFormat.JPEG, _image.getBitsPerPixel());System.out.println("Image saved successfully.");}

Note

LEADTOOLS also supports saving to memory streams. The commented out code above shows how to save to a stream from a RasterImage object.

Run the Project

Run the project by selectingRun->Run.

If the steps were followed correctly, the application loads the CMP image as aRasterImageand then exports to file as JPEG.

Wrap-up

This tutorial showed how to add the necessary references to load and save an image. Also, it covered how to use theRasterImageandRasterCodecsclasses.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2023LEAD Technologies, Inc.All Rights Reserved.