2013-08-23 7 views
0

나는 6 개의 이미지로 압축 된 tiff 이미지를 읽고 6 개의 다른 tiff 파일로 이미지를 분리해야한다. 다른 이미지를 식별하려면 xml 파일에서 이와 같은 오프셋 값을 얻고 있습니다.압축 된 6 개의 이미지가 압축 된 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 개 이미지 모두.

개별 이미지의 오프셋과 길이가 있습니다. 원래 tiff 파일을 오프셋 및 길이에 따라 다른 tiff 이미지로 분할하는 방법.

아래에서 사용중인 코드는 원본 TIFF 파일을 읽고 동일한 파일을 처리합니다. 출력은 단일 이미지 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 오프셋에서 echeck에 넣어 ArrayOutofIndex을 보여주는 이미지 ..Its를 작성하려고하면

..

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

어떻게

오프셋 사용하고 길이 분할 내 파일?

Plz 도움말.

+0

아마도 TIFF 파일이 어떻게 구성되어 있는지 설명 할 수 있습니까? 6 개의 개별 TIFF 스트림으로 구성된 하나의 파일이 있습니까? 아니면 단일 이미지 TIFF의 이미지 데이터를 오프셋합니까? 아니면 다른 구조? 이미지 데이터가 압축되어 있습니까 (그렇다면 어떤 압축)? – haraldK

답변

0

다음은 다중 이미지 TIFF 파일에서 각 이미지를 읽고이를 분할하고 각 이미지를 단일 이미지 TIFF로 다시 쓰는 코드 수정 버전입니다.

여섯 개의 이미지가 입력 된 단일 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()); 
     } 
    } 
}