File io 我有一个压缩的tiff图像文件,压缩了六个图像(不是多页)。我有每个图像的偏移量和数据长度

File io 我有一个压缩的tiff图像文件,压缩了六个图像(不是多页)。我有每个图像的偏移量和数据长度,file-io,javax.imageio,jai,File Io,Javax.imageio,Jai,我必须读取一个tiff图像,该图像由6个图像压缩而成,并将图像分为6个不同的tiff文件。为了识别不同的图像,我从一个xml文件中获得了这样的偏移值 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)

我必须读取一个tiff图像,该图像由6个图像压缩而成,并将图像分为6个不同的tiff文件。为了识别不同的图像,我从一个xml文件中获得了这样的偏移值

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());
    }
}
如何使用偏移量和长度分割我的文件


Plz帮助。

这是您的代码的修改版本,它从多图像TIFF文件中读取每个图像,将其拆分并将每个图像作为单个图像TIFF写回

请注意,它假设您有一个TIFF,其中有6个图像作为输入,这是最常见的情况(请参阅注释部分)

公共类TiffImageReader{
公共静态void main(字符串[]args)引发IOException{
File File=新文件(“C://DS.tiff”);
ImageInputStream iis=ImageIO.createImageInputStream(文件);
迭代器阅读器=ImageIO.getImageReaders(iis);
if(readers.hasNext()){
//获取TIFF阅读器,并设置输入
ImageReader=readers.next();
reader.setInput(iis);
//找到图像的数量,然后迭代
int numImages=reader.getNumImages(true);
对于(int i=0;i
您能否描述一下TIFF文件的结构?您是否有一个包含6个独立TIFF流的文件?或者在单个图像TIFF的图像数据中是否有偏移?还是其他结构?是否对图像数据进行了压缩(如果是,是什么压缩)?
"for (int readNum; (readNum = fis.read(buf)) !== 7827;)"
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());
        }
    }
}