【问题标题】:Split TIFF Files to multiple files将 TIFF 文件拆分为多个文件
【发布时间】:2016-07-15 01:57:04
【问题描述】:

我需要使用分隔符 II* 将 tiff 文件拆分为多个 tiff 文件,因此我使用以下代码将 tiff 文件转换为 base64 并使用子字符串提取第一个图像。但是我收到如下错误。请告知如何使用此分隔符 II* 仅从 tiff 文件中提取第一张图像(base64 代码为 SUkq)。

我能够在不执行子字符串的情况下解码为图像。

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String   index out of range: -1
at java.lang.String.substring(String.java:1954)
at EncodeStringTest.main(EncodeStringTest.java:63)

类文件

public class EncodeStringTest {

public static void main(String[] args) {
    File file = new File("D:\\Users\\Vinoth\\workspace\\image.tif");

    try {
        /*
         * Reading a Image file from file system
         */
        FileInputStream imageInFile = new FileInputStream(file);
        byte imageData[] = new byte[(int)file.length()];
        imageInFile.read(imageData);

        /*
         * Converting Image byte array into Base64 String 
         */
        String imageDataString = encodeImage(imageData);
                    System.out.println(imageDataString);
        String result = imageDataString.substring(imageDataString.indexOf("SUkq") + 1, imageDataString.indexOf("SUkq"));
        /*
         * Converting a Base64 String into Image byte array 
         */
                  System.out.println("Resulted String"+imageDataString);
        byte[] imageByteArray = decodeImage(result);

        /*
         * Write a image byte array into file system  
         */
        FileOutputStream imageOutFile = 
                            new FileOutputStream("D:\\Users\\Vinoth\\workspace\\image_2.tif");
        imageOutFile.write(imageByteArray);

        imageInFile.close();
        imageOutFile.close();

        System.out.println("Image Successfully Manipulated!");
    } catch (FileNotFoundException e) {
        System.out.println("Image not found" + e);
    } catch (IOException ioe) {
        System.out.println("Exception while reading the Image " + ioe);
    }

}


public static String encodeImage(byte[] imageByteArray){        
    return Base64.encodeBase64URLSafeString(imageByteArray);        
}


public static byte[] decodeImage(String imageDataString) {      
    return Base64.decodeBase64(imageDataString);
}

}

【问题讨论】:

  • 你能试试这个解决方案,让我知道你的 cmets。 stackoverflow.com/a/45583553/7731623
  • 该代码对我不起作用,因为我的文件包含多个 tiff 文件,每个文件都有单独的元数据。所以该代码只会读取我文件中的第一个图像。感谢您的建议,我已经通过将图像转换为字节数组并逐个字符读取每个图像并分配给单独的输出流并使用 twlevemonkeys tiff writer 将所有流合并到单个 tiff 找到了解决方案。

标签: java base64 tiff


【解决方案1】:

在这段代码中

substring(imageDataString.indexOf("SUkq") + 1, imageDataString.indexOf("SUkq"))

根据错误消息很清楚,在输入中找不到字符串"SUkq"。因此这个语句等价于

substring(0,-1)

这是无效的。您需要添加代码来处理输入不包含您要查找的文本的情况。

其次,该子字符串永远不会起作用。由于您两次都以字符串开头indexOf 开头,因此即使输入包含您要查找的字符串,结果也将始终为

substring(n,n-1)

其中n 是目标字符串的位置。此范围始终无效。

不清楚为什么您觉得有必要对图像进行 base64 编码。只需搜索字节数组即可。仅当未编码的目标字符串从文件开头偏移 3 的倍数开始时,base64 字符串才会包含 SUkq 字符串,因为 base64 将 3 个输入字节编码为 4 个输出字节。如果输入中的分隔符 II* 出现在其他两个可能的偏移量(模 3)中,则编码结果将取决于先前和后续数据,因此使用 base64 通常不会起作用,除非您可以保证所有情况下输入分隔符的偏移量,它始终为 0 mod 3。

哦,下次尝试在 IDE 调试器中单步调试代码。你会很快看到发生了什么。

【讨论】:

  • 谢谢,是否可以使用 bytearray 分割 TIFF 文件?
  • 可以将任何文件“拆分”成块。我不知道这对 TIFF 是否有意义(即它是否会产生有效的单独图像文件)。试试看吧。
猜你喜欢
  • 2011-05-18
  • 1970-01-01
  • 2021-09-01
  • 2013-08-24
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
相关资源
最近更新 更多