Skip to main content

java.lang.indexoutofbound Exception..when try to convert jpg image to tiff

14 replies [Last post]
mndprasad
Offline
Joined: 2008-05-03
Points: 0

HI all

i am trying to convert multiple jpg images to single tiff image..i am getting an exception that is
"java.lang.indexoutofbound Exception"..can anyone help me

import com.sun.media.imageio.plugins.tiff.TIFFImageWriteParam;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

public class mergeImages
{
public void execute(ArrayList imageFiles , File newTIFF)
throws IOException
{
Iterator iterator = imageFiles.iterator();
Iterator writers = ImageIO.getImageWritersByFormatName("tif");
ImageWriter writer = (ImageWriter)writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(newTIFF);
writer.setOutput(ios);
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setCompressionType("JPEG");
boolean firstImage = true;
while (iterator.hasNext())
{
File imageFile = (File) iterator.next();
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
Iterator readers = ImageIO.getImageReaders(iis);
ImageReader reader = (ImageReader)readers.next();
reader.setInput(iis);
try
{
for(int i = 0;i

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
marcojacob
Offline
Joined: 2006-07-10
Points: 0

As Robert already told you should always post the information needed to help you.
You get compile errors because I wrote the code from scratch and didn't compile.
Some small work should be done by yourself. Please use Eclipse or another development
tool to get the errors. Consult the API yourself to find mistakes. You won't get a complete
solution for your problems.

mndprasad
Offline
Joined: 2008-05-03
Points: 0

Its working fine now....anyway thanks for the help...
i have changed many...irrespective of compression....
[b]
image()
{

String fa="C:\\images";
File dir = new File(fa);
String[] chld = dir.list();
if(chld == null)
{
System.out.println("Specified directory does not exist or is not a directory.");
}
for(int i = 0; i < chld.length; i++)
{
String fileName = chld[i];
System.out.println(fileName);
}
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("tiff");
if (iter.hasNext())
{
writer = (ImageWriter)iter.next();
}
try
{

FileInputStream ins0 = new FileInputStream("c:\\aa.jpg");
BufferedImage img0 = ImageIO.read(ins0);
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("c:\\image12.tiff"));
writer.setOutput(ios);
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
tiffWriteParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT
);
writer.write(new IIOImage(img0, null, null));
writer.writeInsert(1, new IIOImage(img1, null, null), null);
ios.flush();
writer.dispose();
ios.close();
}

catch(Exception e)
{
e.printStackTrace();
}
}[/b]

mndprasad
Offline
Joined: 2008-05-03
Points: 0

Working code..full
image()
{

String fa="C:\\images";
File dir = new File(fa);
String[] chld = dir.list();
if(chld == null)
{
System.out.println("Specified directory does not exist or is not a directory.");
}
for(int i = 0; i < chld.length; i++)
{
String fileName = chld[i];
System.out.println(fileName);
}
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("tiff");
if (iter.hasNext())
{
writer = (ImageWriter)iter.next();
}
try
{

FileInputStream ins0 = new FileInputStream("c:\\aa.jpg");
BufferedImage img0 = ImageIO.read(ins0);
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("c:\\image12.tiff"));
writer.setOutput(ios);
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
tiffWriteParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT
);
writer.write(new IIOImage(img0, null, null));
writer.writeInsert(1, new IIOImage(img1, null, null), null);
ios.flush();
writer.dispose();
ios.close();
}

catch(Exception e)
{
e.printStackTrace();
}
}

mndprasad
Offline
Joined: 2008-05-03
Points: 0

..

marcojacob
Offline
Joined: 2006-07-10
Points: 0

Some more information about where the Exception ist thrown can be helpful.

mndprasad
Offline
Joined: 2008-05-03
Points: 0

Its working now by creating a TIFF image and the image size is also coming..the only thing is now that if i open that image it has no image..if i open that image with photoshop it will open up showing the first image

the altered code is:

***********************************************************

[b]import java.io.*;
import java.util.*;
import java.io.IOException;
import javax.imageio.stream.ImageInputStreamImpl;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import java.awt.image.RenderedImage;
import com.sun.media.jai.codec.FileSeekableStream;
import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.util.ImagingListener;
import java.awt.image.Raster;
import javax.imageio.IIOException;
import javax.imageio.IIOImage;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.*;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import com.sun.image.codec.jpeg.*;
import java.awt.image.DataBuffer;
import java.awt.geom.GeneralPath;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.spi.ImageWriterSpi;
import java.util.Locale;
import com.sun.media.imageio.plugins.tiff.*;

public class mergeImagesMNDPC
{
public void execute(HashMap imageFiles , File newTIFF){
Iterator writers=null;
Set keySet=imageFiles.keySet();
Iterator iterator= keySet.iterator();
ImageWriter writer=null;
try{
writers = ImageIO.getImageWritersByFormatName("tif");
writer = (ImageWriter)writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(newTIFF);
writer.setOutput(ios);
}
catch(Exception e){
e.printStackTrace();
}
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setCompressionType("JPEG");
boolean firstImage = true;
for(int j=0;j {
System.out.println("entering while loop");
ImageReader reader=null;
try
{
//System.out.println("entering 2nd part..."+iterator.next());
File imageFile = (File) imageFiles.get(iterator.next());
System.out.println("entering 2nd part..."+imageFile);
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
Iterator readers = ImageIO.getImageReaders(iis);
reader = (ImageReader)readers.next();
reader.setInput(iis);

for(int i=0;true;i++)
{
//int i=0;
System.out.println("entering for loop");
IIOMetadata iioImageMetadata = reader.getImageMetadata(i);
int imageHeight = reader.getHeight(i);
int imageWidth = reader.getWidth(i);
tiffWriteParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setTiling(imageWidth, imageHeight, 0, 0);
BufferedImage bi = reader.read(i, null);
IIOImage image = new IIOImage(bi, null, iioImageMetadata);
if(firstImage==false)
writer.writeInsert(-1,image, tiffWriteParam);
else {
writer.write(null, image, tiffWriteParam);
firstImage = false;
}
System.out.println("file writing completed");
}
}
catch (Exception e)
{
e.printStackTrace();
}
if(reader!=null)
reader.dispose();
}
writer.dispose();
}
public static void main(String[] args)throws IOException
{
HashMap filesList=new HashMap();
File destFile = new File("c:\\a.tiff");
File in1 = new File("c:\\a.jpg");
File in2 = new File("c:\\b.jpg");
filesList.put("file1",in1);
filesList.put("file2",in2);
mergeImagesMNDPC mI=new mergeImagesMNDPC();
mI.execute(filesList,destFile);
}

}[/b]
**********************

Output of the file when i run::

[b]entering while loop
entering 2nd part...c:\a.jpg
entering for loop
file writing completed
entering for loop
java.lang.IndexOutOfBoundsException
at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:448)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.getImageMetadata(JPEGImageReader.java:851)
at mergeImagesMNDPC.execute(mergeImagesMNDPC.java:70)
at mergeImagesMNDPC.main(mergeImagesMNDPC.java:104)
entering while loop
entering 2nd part...c:\b.jpg
entering for loop
file writing completed
entering for loop
java.lang.IndexOutOfBoundsException: imageIndex != 0!
at com.sun.imageio.plugins.png.PNGImageReader.getImageMetadata(PNGImageReader.java:1518)
at mergeImagesMNDPC.execute(mergeImagesMNDPC.java:70)
at mergeImagesMNDPC.main(mergeImagesMNDPC.java:104)

Process completed.
[b][/b][/b]

mndprasad
Offline
Joined: 2008-05-03
Points: 0

[b]this is the output msg when i run this program[/b]but it writes into the tiff it seems bcoz otherwise it would not hav show that file size

entering while loop
entering 2nd part...c:\a.jpg
entering for loop
file writing completed
entering for loop
java.lang.IndexOutOfBoundsException
at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:448)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.getImageMetadata(JPEGImageReader.java:851)
at mergeImagesMNDPC.execute(mergeImagesMNDPC.java:70)
at mergeImagesMNDPC.main(mergeImagesMNDPC.java:104)
entering while loop
entering 2nd part...c:\b.jpg
entering for loop
file writing completed
entering for loop
java.lang.IndexOutOfBoundsException: imageIndex != 0!
at com.sun.imageio.plugins.png.PNGImageReader.getImageMetadata(PNGImageReader.java:1518)
at mergeImagesMNDPC.execute(mergeImagesMNDPC.java:70)
at mergeImagesMNDPC.main(mergeImagesMNDPC.java:104)

Process completed.

marcojacob
Offline
Joined: 2006-07-10
Points: 0

Sorry, but this all seems for me like wrong code. Please analyse your whole
code first. In the first code you iterate imageFiles then do a for loop through
imageFiles.size() to get image pages? You should iterate OR for-loop and then
do a while-loop to read single pages from a file and write to output until you get
IndexOutOfBounds, then close input and open the next. However you don't
need to iterate pages when reading because jpeg only have one page so get only
page 0.

The second snippet seems to me like you first do a for-loop through imageFiles.size()
then iterate through imageFiles. However this seems not right for me.

Moreover I can't get the connection between your first code, the second snippet and
the output posted. Please consult your code first then post the whole code again.
It might be interesting what you get for input and what you expect for output.

Message was edited by: marcojacob

marcojacob
Offline
Joined: 2006-07-10
Points: 0

However, this code should do the work, not tested on holiday ...

Iterator writers = ImageIO.getImageWritersByFormatName("tif");
ImageWriter writer = null;
if (writers.hasNext()) {
writer = (ImageWriter)writers.next();
} else {
throw new Exception("No TIFFImageWriter? jai_imageio.jar in CLASSPATH?");
}
writer.prepareWriteSequence(null);
ImageOutputStream ios = ImageIO.createImageOutputStream(newTIFF);
writer.setOutput(ios);

ImageWriteParam writeParam = writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("JPEG");

Iterator readers = ImageIO.getImageReadersByMime("image/jpeg");
ImageReader reader = null;
if (readers.hasNext()) {
reader = (ImageReader)readers.next();
} else {
throw new Exception("No JPEG reader found (somehow strange, no JDK?)");
}

Iterator iterator = imageFiles.iterator();
while (iterator.hasNext()) {
File imageFile = (File) iterator.next();
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
reader.setInput(iis);
IIOImage image = reader.readAll(0, null);
writer.writeToSequence(image, tiffWriteParam);
iis.close();
}
writer.endWriteSequence();
ios.flush();
ios.close();
writer.dispose();
reader.dispose();

mndprasad
Offline
Joined: 2008-05-03
Points: 0

I am gettinf tiffparam Exception.i have included all jars...
i hav altered my code by writing main method in hashfile to get input files..

public class merge
{
public void execute(HashMap imageFiles , File newTIFF){
Iterator writers = ImageIO.getImageWritersByFormatName("tif");
ImageWriter writer = null;
if (writers.hasNext())
{
writer = (ImageWriter)writers.next();
}
else
{
throw new Exception("No TIFFImageWriter? jai_imageio.jar in CLASSPATH?");
}
writer.prepareWriteSequence(null);
ImageOutputStream ios = ImageIO.createImageOutputStream(newTIFF);
writer.setOutput(ios);

ImageWriteParam writeParam = writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("JPEG");
Iterator readers = ImageIO.getImageReadersByMime("image/jpeg");
ImageReader reader = null;
if (readers.hasNext())
{
reader = (ImageReader)readers.next();
}
else
{
throw new Exception("No JPEG reader found (somehow strange, no JDK?)");
}
Iterator iterator = imageFiles.iterator();
while (iterator.hasNext())
{
File imageFile = (File) iterator.next();
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
reader.setInput(iis);
IIOImage image = reader.readAll(0, null);
writer.writeToSequence(image, tiffWriteParam);
iis.close();
}
writer.endWriteSequence();
ios.flush();
ios.close();
writer.dispose();
reader.dispose();
}
public static void main(String[] args)throws IOException
{
HashMap filesList=new HashMap();
File destFile = new File("c:\\a.tiff");
File in1 = new File("c:\\a.jpg");
File in2 = new File("c:\\b.jpg");
filesList.put("file1",in1);
filesList.put("file2",in2);
merge mI=new merge();
mI.execute(filesList,destFile);
}
}

mndprasad
Offline
Joined: 2008-05-03
Points: 0

pls help me in this case....
where i am going wrong..
using of hashmap is good or otherwise i can get input files from inputstream directly?

robert engels

You need to post the full stack trace before any help can be offered...

On May 7, 2008, at 9:47 AM, jai-imageio@javadesktop.org wrote:

> pls help me in this case....
> where i am going wrong..
> using of hashmap is good or otherwise i can get input files from
> inputstream directly?
> [Message sent by forum member 'mndprasad' (mndprasad)]
>
> http://forums.java.net/jive/thread.jspa?messageID=273149
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

mndprasad
Offline
Joined: 2008-05-03
Points: 0

--------------------Configuration: --------------------
C:\Program Files\Java\jdk1.5.0_04\bin\merge.java:52: cannot find symbol
symbol : method getImageReadersByMime(java.lang.String)
location: class javax.imageio.ImageIO
Iterator readers = ImageIO.getImageReadersByMime("image/jpeg");
^
C:\Program Files\Java\jdk1.5.0_04\bin\merge.java:62: cannot find symbol
symbol : method iterator()
location: class java.util.HashMap
Iterator iterator = imageFiles.iterator();
^
C:\Program Files\Java\jdk1.5.0_04\bin\merge.java:69: cannot find symbol
symbol : variable tiffWriteParam
location: class merge
writer.writeToSequence(image, tiffWriteParam);
^
C:\Program Files\Java\jdk1.5.0_04\bin\merge.java:80: cannot find symbol
symbol : variable reader
location: class merge
reader.dispose();
^
Note: C:\Program Files\Java\jdk1.5.0_04\bin\merge.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors

Process completed.

robert engels

This is not a stack trace - your code does not compile...

Read some basic java tutorials on compiling (setting the classpath,
including the jars - imageio).

On May 7, 2008, at 9:56 AM, jai-imageio@javadesktop.org wrote:

> --------------------Configuration: --------------------
> C:\Program Files\Java\jdk1.5.0_04\bin\merge.java:52: cannot find
> symbol
> symbol : method getImageReadersByMime(java.lang.String)
> location: class javax.imageio.ImageIO
> Iterator readers = ImageIO.getImageReadersByMime("image/jpeg");
> ^
> C:\Program Files\Java\jdk1.5.0_04\bin\merge.java:62: cannot find
> symbol
> symbol : method iterator()
> location: class java.util.HashMap
> Iterator iterator = imageFiles.iterator();
> ^
> C:\Program Files\Java\jdk1.5.0_04\bin\merge.java:69: cannot find
> symbol
> symbol : variable tiffWriteParam
> location: class merge
> writer.writeToSequence(image, tiffWriteParam);
> ^
> C:\Program Files\Java\jdk1.5.0_04\bin\merge.java:80: cannot find
> symbol
> symbol : variable reader
> location: class merge
> reader.dispose();
> ^
> Note: C:\Program Files\Java\jdk1.5.0_04\bin\merge.java uses
> unchecked or unsafe operations.
> Note: Recompile with -Xlint:unchecked for details.
> 4 errors
>
> Process completed.
> [Message sent by forum member 'mndprasad' (mndprasad)]
>
> http://forums.java.net/jive/thread.jspa?messageID=273151
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net