how to convert JPEG image to TIFF image?

1.8k views Asked by At

I am developing an app for image processing for that I need to convert JPEG file to TIFF file.

I have tried below code snippet for conversion. But, it is generating corrupt tiff file.

Here is the code:

package javaapplication2;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.idrsolutions.image.tiff.TiffEncoder;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class JavaApplication2
{
    public static void main(String[] args)
    {
        BufferedImage bufferedImage;
        try 
        {
            bufferedImage = ImageIO.read(new File("C:\\Users\\Jay Tanna\\Desktop\\image1.jpg"));


            BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
            bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);

            newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

            OutputStream   out      =   new FileOutputStream("C:\\Users\\Jay Tanna\\Desktop\\myNew_File.tiff");
            TiffEncoder tiffEncoder =   new TiffEncoder();
            tiffEncoder.setCompressed(true);
            tiffEncoder.write(newBufferedImage, out);

            System.out.println("Done");

    } 
        catch (IOException e) 
        {

            e.printStackTrace();

    }

   }

}

Kindly help me with this issue.

2

There are 2 answers

1
Roberto Attias On BEST ANSWER

Not familiar with com.idrsolutions.image.tiff.TiffEncoder, but you're definitely missing a out.close() without which some data may remain buffered and not make it to disk.

0
Harvyn On

Change the extension of the file, try this:

OutputStream   out = new FileOutputStream("C:\\Users\\Jay Tanna\\Desktop\\myNew_File.tiff");

for this:

OutputStream   out = new FileOutputStream("C:\\Users\\Jay Tanna\\Desktop\\myNew_File.TIF");