This tutorial shows how to read and write TIFF tags and comments using the LEADTOOLS SDK in a Java application.
Overview | |
---|---|
Summary | This tutorial covers how to use the RasterCommentMetaData class in a Java Console application. |
Completion Time | 30 minutes |
Eclipse Project | Download tutorial project (5 KB) |
Platform | Java Application |
IDE | Eclipse |
Development 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 tutorial, before working on the Read and Write TIFF Tags and Comments - Java tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If that project is unavailable, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by local .jar
files located at <INSTALL_DIR>\LEADTOOLS23\Bin\Java
.
For this project, the following references are needed:
leadtools.jar
leadtools.codecs.jar
This tutorial uses LEADTOOLS Codec library support. For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java 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 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.
Open the Main.java
class in the Project Explorer. Add the following statements to the import
block at the top.
import leadtools.*;
import leadtools.codecs.*;
import java.io.IOException;
import java.nio.file.*;
import java.util.List;
Add a new method called ReadAndWriteTifCommentsTags()
, and call it in the run()
method as shown below.
public static void main(String[] args) throws IOException {
new Main().run(args);
}
private void run(String[] args) {
try {
Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.LEADTOOLS);
Platform.loadLibrary(LTLibrary.CODECS);
SetLicense();
ReadAndWriteTifCommentsTags();
}
catch(Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
Add the below code inside the newly created method to write a new TIFF comment, read the comment, and then display it to the console. Also, add the below code inside the new method to read the XResolution
from the TIFF image and then modify it.
void ReadAndWriteTifCommentsTags()
{
String fileName = "C:\\LEADTOOLS23\\Resources\\Images\\clean.tif";
ILeadStream stream = new LeadFileStream(fileName);
RasterCodecs codecs = new RasterCodecs();
// Write the comment to the file
RasterCommentMetadata writeComment = new RasterCommentMetadata();
writeComment.setType(RasterCommentMetadataType.SOFTWARE);
writeComment.fromAscii("LEADTOOLS Demo");
codecs.writeComment(stream, 1, writeComment);
// Read the comment
RasterCommentMetadata readComment = codecs.readComment(stream, 1, RasterCommentMetadataType.SOFTWARE);
System.out.println("The following comment has been read:\n" + readComment.toAscii() + "\n");
// Read the resolution tag and modify it
RasterImage image = codecs.load(stream);
RasterImageFormat format = image.getOriginalFormat();
List<RasterTagMetadata> tags = null;
if (RasterCodecs.tagsSupported(format))
{
tags = codecs.readTags(stream, 1);
if (tags != null)
{
final int XresTagID = 282;
RasterTagMetadata ReadTag = codecs.readTag(stream, 1, XresTagID);
RasterMetadataURational[] rational = ReadTag.toURational();
rational[0].setNumerator(rational[0].getNumerator() * 5);
rational[0].setDenominator(rational[0].getDenominator() * 1);
ReadTag.fromURational(rational);
codecs.writeTag(stream, 1, ReadTag);
System.out.println("Resolution changed successfully.");
}
else
{
System.out.println("No tags found");
}
}
codecs.dispose();
}
Run the project by pressing Ctrl + F11, or by selecting Run -> Run.
If the steps were followed correctly, the application writes a new TIFF comment and then displays the comment in the console. Then, the application reads the XResolution
from the TIFF image, modifies it, and writes the tag back to the TIFF image.
This tutorial showed how to use the RasterTagMetadata
and RasterCommentMetadata
classes to read/write TIFF comments and tags.