Skip to main content

Convert JPEG to TIFF

4 replies [Last post]
m_chabs
Offline
Joined: 2010-06-22

Hi there,
im trying to convert a .jpeg image into .tiff but im facing a problem that seems to be common based on what i read online. The tiff file produced opens up using IrfanView but not Adobe photoshop. Here is the code im using( the code is not clean and some lines are there because of mr trying things out but anyways ull get the idea) and please let me know if there is a different approach to this as well Thank you.

public Boolean JPEGtoTIFF(String InputimgFile, String OutputFileName) throws IOException
{
File imgFile = new File(InputimgFile);
ImageReader rdr = (ImageReader)Readers.next();
InputStream fis = new BufferedInputStream(new FileInputStream(imgFile));
ImageInputStream iis = ImageIO.createImageInputStream(fis);
rdr.setInput(iis, false);
BufferedImage bi = rdr.read(0);*/
//**********************************************************************************************

InputStream fis = new BufferedInputStream(new FileInputStream(imgFile));
JPEGImageReaderSpi spi = new JPEGImageReaderSpi();
ImageReader rdr = spi.createReaderInstance();

ImageInputStream iis = ImageIO.createImageInputStream(fis);
rdr.setInput(iis, true);
JPEGImageReadParam rdparam = new JPEGImageReadParam();
BufferedImage bi = rdr.read(0,rdparam);

TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();
ImageWriter wtr = tiffspi.createWriterInstance();

File newTIFF = new File(OutputFileName + ".tif");

ImageOutputStream ios = ImageIO.createImageOutputStream(newTIFF);
wtr.setOutput(ios);

//TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
ImageWriteParam tiffWriteParam = wtr.getDefaultWriteParam();
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setCompressionType("LZW");
tiffWriteParam.setCompressionQuality(0.5f);
//**********************************************************************************************

try
{
int i=0;
IIOMetadata iioImageMetadata = rdr.getImageMetadata(i);

//set tile height to image height to write single strip
int imageHeight = rdr.getHeight(i);
int imageWidth = rdr.getWidth(i);

tiffWriteParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setTiling(imageWidth, imageHeight, 0, 0);

//BufferedImage bi = reader.read(i, null);

BufferedImage buffimg = new BufferedImage(imageWidth, imageHeight, bi.TYPE_BYTE_BINARY);
//BufferedImage bi = (BufferedImage)component.createImage(imageWidth, imageHeight);

IIOImage image = new IIOImage(bi, null, iioImageMetadata);

long start = System.currentTimeMillis();
try
{
wtr.write(null,image,tiffWriteParam);
//writer.writeToSequence(image,tiffWriteParam);
}
catch(Exception e)
{
System.out.println(e.toString());
}
long end = System.currentTimeMillis();
System.out.println("Write took (ms):" + (end-start));

wtr.dispose();
//writer.write(null, image, tiffWriteParam);
}
catch (IndexOutOfBoundsException e)
{
rdr.dispose();
}

rdr.dispose();
return true;
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
m_chabs
Offline
Joined: 2010-06-22

so does anyone know what i seem to be missing? Please any help is appreciated..im kinda stuck here

m_chabs
Offline
Joined: 2010-06-22

Actually nevermind.. figured it out.. this is very stupid lol

marcojacob
Offline
Joined: 2006-07-10

It's difficult because your code contains about 3 approaches.
Which approach keeps Photoshop from opening this target file?

One cause might be a missing flush() on the OutputStream.

m_chabs
Offline
Joined: 2010-06-22

Hey here is a "cleaner" version of this function im using.

public Boolean JPEGtoTIFF(String InputimgFile, String OutputFileName) throws IOException
{
File imgFile = new File(InputimgFile);
InputStream fis = new BufferedInputStream(new FileInputStream(imgFile));

JPEGImageReaderSpi spi = new JPEGImageReaderSpi();
ImageReader rdr = spi.createReaderInstance();
ImageInputStream iis = ImageIO.createImageInputStream(fis);

rdr.setInput(iis, true);
JPEGImageReadParam rdparam = new JPEGImageReadParam();
BufferedImage bi = rdr.read(0,rdparam);

TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();
ImageWriter wtr = tiffspi.createWriterInstance();

File newTIFF = new File(OutputFileName + ".tif");

ImageOutputStream ios = ImageIO.createImageOutputStream(newTIFF);
wtr.setOutput(ios);

//TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
ImageWriteParam tiffWriteParam = wtr.getDefaultWriteParam();

String [] CompTypes = tiffWriteParam.getCompressionTypes();
for(int j=0; j System.out.println(CompTypes[j]);

tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setCompressionType("LZW");
tiffWriteParam.setCompressionQuality(0.5f);

try
{
int i=0;

IIOMetadata iioImageMetadata = rdr.getImageMetadata(i);

int imageHeight = rdr.getHeight(i);
int imageWidth = rdr.getWidth(i);

tiffWriteParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setTiling(imageWidth, imageHeight, 0, 0);

BufferedImage buffimg = new BufferedImage(imageWidth, imageHeight, bi.TYPE_BYTE_BINARY);

IIOImage image = new IIOImage(bi, null, iioImageMetadata);

try
{
wtr.write(null,image,tiffWriteParam);
}
catch(Exception e)
{
System.out.println(e.toString());
}
wtr.dispose();

}
catch (IndexOutOfBoundsException e)
{
rdr.dispose();
}

//Done writing all images for this image
rdr.dispose();
}