This tutorial shows how to read and write TIFF tags and comments using the LEADTOOLS SDK in a Java application.
|Summary||This tutorial covers how to use the RasterCommentMetaData class in a Java Console application.|
|Completion Time||30 minutes|
|Eclipse Project||Download tutorial project (4 KB)|
|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
For this project, the following references are needed:
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:
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.
Main.java class in the Project Explorer. Add the following statements to the
import block at the top.
// import block at the top
Add a new method called
ReadAndWriteTifCommentsTags(), and call it in the
main() method as shown below.
public static void main(String args) throws IOException
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.
static void ReadAndWriteTifCommentsTags()
String fileName = "C:\\LEADTOOLS21\\Resources\\Images\\clean.tif";
ILeadStream stream = new LeadFileStream(fileName);
RasterCodecs codecs = new RasterCodecs();
// Write the comment to the file
RasterCommentMetadata writeComment = new RasterCommentMetadata();
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;
tags = codecs.readTags(stream, 1);
if (tags != null)
final int XresTagID = 282;
RasterTagMetadata ReadTag = codecs.readTag(stream, 1, XresTagID);
RasterMetadataURational rational = ReadTag.toURational();
rational.setNumerator(rational.getNumerator() * 5);
rational.setDenominator(rational.getDenominator() * 1);
codecs.writeTag(stream, 1, ReadTag);
System.out.println("Resolution changed successfully.");
System.out.println("No tags found");
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
RasterCommentMetadata classes to read/write TIFF comments and tags.