This simple article encompasses the details of how to convert TIFF to PNG in Java. It covers all the required resources, referred classes and a runnable sample code to Convert TIFF to PNG in Java. The application is useful and can be utilized in Java-supported environments inside Linux, Windows, or macOS.
Steps to Convert TIFF to PNG in Java
- Set up an environment to use Aspose.Imaging for Java using the repository manager
- Load the source TIFF file from the disk using the Image class instance and cast it to TiffImage
- Iterate through every TIFF frame
- Save the selected TIFF frame as a PNG on the disk using default PngOptions
By adhering to the above mentioned steps, one can easily convert every frame inside a TIFF to PNG using Java using a step-by-step approach whereby initially we will establish the environment by utilizing the required resources inside the project. The source TIFF image will be loaded using an Image class object and cast to a TiffImage class instance. You will loop through every TIFF frame inside the loaded image and save that as a PNG file on the disk using default PngOptions.
Code to Convert TIFF to PNG using Java
import com.aspose.imaging.Image; | |
import com.aspose.imaging.ImageOptionsBase; | |
import com.aspose.imaging.License; | |
import com.aspose.imaging.fileformats.tiff.TiffFrame; | |
import com.aspose.imaging.fileformats.tiff.TiffImage; | |
import com.aspose.imaging.imageoptions.PngOptions; | |
public class TIFFToPNGConvert { | |
public static void main (String[] args) { | |
String path = "/Users/KB/TestData/"; | |
// Apply the product license to convert PNG to Icon in Java | |
License pngToIcLicense = new License(); | |
pngToIcLicense.setLicense(path + "Conholdate.Total.Product.Family.lic"); | |
Image image = Image.load(path + "multiple_codes.png"); | |
// Applying product license to convert Tiff to PNG in C# | |
License TiffToPdfLicense = new License(); | |
TiffToPdfLicense.setLicense(path + "Conholdate.Total.Product.Family.lic"); | |
TiffImage tiffImage = (TiffImage)Image.load(path+ "AFREY-Original.tif"); | |
// Initialize the index variable to keep track of the frames inside the tiff | |
// image, Iterate through the tiff image frame collection and | |
// save the PNG image on the disk | |
int i = 0; | |
for (TiffFrame tiffFrame : tiffImage.getFrames()) | |
{ | |
tiffFrame.save(path + ++i + "_out.png", new PngOptions()); | |
} | |
} | |
} |
By using the above example code, you can easily manage to change TIFF to PNG using Java with the help of a very simple API interface. The Image class supports managing multiple image types by exposing numerous overloaded methods that can be utilized to load the images either from a stream or from a disk along with other additional LoadOptions parameters. You can easily set the data background color, progress event handler, data recovery mode, and buffer size hint using different options exposed by LoadOptions class.
This example has taught us about the process to convert TIFF to PNG using Java. If you are interested in process of compressing a PNG file, refer to the article on how to compress a PNG in Java.