2013-08-23 18 views
0

我必須讀取一張tiff圖像,該圖像被壓縮成6個圖像並將圖像分成6個不同的tiff文件。爲了識別不同的圖像,我從xml文件中獲得像這樣的偏移值。我有一個壓縮的tiff圖像文件,壓縮了六個圖像(不是多頁)。我有每張圖片的偏移和長度的數據

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 

...對於所有6個圖像都是類似的。

我已經偏移和個體images.How的長度與原始TIFF文件分割成不同的TIFF圖像按照偏移量和長度。

我使用下面讀取原始TIFF文件和應對同一file.Output的代碼是與單個圖像TIFF文件。

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()); 
    } 
} 

如果我讓字節爲1,在第一偏移7827戴上電子支票,並嘗試寫的圖像顯示..Its .. ArrayOutofIndex

"for (int readNum; (readNum = fis.read(buf)) !== 7827;)" 

如何使用偏移量和長度分割我的文件?

plz幫助。

+0

你能描述一下TIFF文件的結構嗎?你有一個由6個獨立的TIFF流組成的文件嗎?或者您是否有偏移到單個圖像TIFF的圖像數據?或者其他一些結構?圖像數據是否被壓縮(如果是,壓縮)? – haraldK

回答

0

這裏是你的代碼,從多圖像TIFF文件中讀取每個圖像,將其分解和每個圖像寫回單TIFF圖像的修改版本。

注意,它假定你有6個圖像作爲輸入,這是最常見的情況下,單一的TIFF(見註釋部分)。

public class TiffImageReader { 
    public static void main(String[] args) throws IOException { 
     File file = new File("C://DS.tiff"); 
     ImageInputStream iis = ImageIO.createImageInputStream(file); 

     Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); 
     if (readers.hasNext()) { 
      // Get TIFF reader, and set input 
      ImageReader reader = readers.next(); 
      reader.setInput(iis); 

      // Find number of images, and iterate 
      int numImages = reader.getNumImages(true); 
      for (int i = 0; i < numImages; i++) { 
       // For each image in the file, read and write as standalone TIFF 
       BufferedImage image = reader.read(i, null); 
       File imageFile = new File(String.format("C://DSCOPY_%d.tiff", i)); 
       ImageIO.write(image, "tiff", imageFile); 
       System.out.println(imageFile.getPath()); 
      } 
     } 
     else { 
      System.err.printf("No image reader for %s\n", file.getPath()); 
     } 
    } 
}