I have to read a tiff image which is compressed of 6 images and separate the images into 6 different tiff files. To identify the different images I am getting offset values like this from a xml file.
First image :data_offset :0
data_length :7827
Second Image: data_offset :7827
data_length :9624
Third Image: data_offset :17451 ( i.e 7827+9624)
data_length :5713
Fourth Image: data_offset :23164 (7827+9624+5713)
data_length :9624
… similarly for all 6 images.
I have offset and length of individual images.How to split the original tiff file into different tiff image as per offset and length.
The code I am using below is reading the original tiff file and coping the same file.Output is the tiff file with single image.
public class TiffImageReader {
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("C://DS.tiff");
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1; ) {
//Writes to this byte array output stream
bos.write(buf, 0, readNum);
System.out.println("read " + readNum + " bytes,");
}
}
catch (IOException ex) {
Logger.getLogger(ConvertImage.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("tiff");
ImageReader reader = (ImageReader) readers.next();
Object source = bis;
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Image image = reader.read(0, param);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);
File imageFile = new File("C:// DSCOPY.tiff");
ImageIO.write(bufferedImage, "tiff", imageFile);
System.out.println(imageFile.getPath());
}
}
If I make byte to 1 and put on echeck at first offset 7827 and try to write the image ..Its showing ArrayOutofIndex..
"for (int readNum; (readNum = fis.read(buf)) !== 7827;)"
How to use offset and length to split my file?
Plz help.