3

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.

Jay
  • 1,055
  • 2
  • 8
  • 17
  • Possible duplicate of [Image Conversion In Java](http://stackoverflow.com/questions/14618953/image-conversion-in-java) – Maxim Jan 07 '17 at 13:34
  • imageIo does not support tiff file format and tiff is hard requriment@MaximDobryakov – Jay Jan 07 '17 at 13:41
  • @MaximDobryakov – Jay Jan 07 '17 at 13:41
  • Possible duplicate of [Reading and Writing out TIFF image in Java](http://stackoverflow.com/questions/2898311/reading-and-writing-out-tiff-image-in-java) – ProgrammersBlock Jan 07 '17 at 14:13
  • @JayTanna You could try using standard ImageIO and my [ImageIO TIFF plugin](https://github.com/haraldk/TwelveMonkeys#tiff---aldusadobe-tagged-image-file-format). – Harald K Jan 07 '17 at 20:26

2 Answers2

1

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.

Roberto Attias
  • 1,883
  • 1
  • 11
  • 21
  • 1
    ...or better yet, use [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) when creating `out`. – Harald K Jan 09 '17 at 12:19
-1

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");
DaFois
  • 2,197
  • 8
  • 26
  • 43
Harvyn
  • 1