将TIFF文件拆分为多个文件 [英] Split TIFF Files to multiple files

查看:182
本文介绍了将TIFF文件拆分为多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I will need to split a tiff file to multiple tiff file using the delimiter II* , so I'm using the below code to convert the tiff file to base64 and using the substring to extract the first image . However I'm getting the error as below. Please advise how to extract only the first image from the tiff file using this delimiter II* (base64 code is SUkq).

我无需执行子字符串就可以解码为图像.

I'm able to decode to image without performing the sub string.

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

}

推荐答案

在此代码中

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

根据错误消息很清楚,在输入中未找到字符串"SUkq".因此,该语句等同于

it is clear based on the error message that the string "SUkq" is not found in the input. Therefore this statement is equivalent to

substring(0,-1)

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

Which is invalid. You need to add code to handle the condition where the input does not contain the text you are looking for.

其次,该子字符串将永远无法工作.由于您两次都以indexOf a开头,因此即使输入中包含您要查找的字符串,结果也始终为

Secondarily, that substring will never work. Since you start indexOf a the beginning of the string both times, even if the input contains the string you are looking for, the result will always be

substring(n,n-1)

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

where n is the position of the target string. This range is always invalid.

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

It is not clear why you feel it is necessary to base64 encode the image at all. Just search the byte array instead. The base64 string will contain the SUkq string only if the unencoded target string starts at a multiple of 3 offset from the beginning of the file, since base64 encodes 3 input bytes to 4 output bytes. If the delimiter in the input, II*, occurs at either of the other two possible offsets (modulo 3) the encoded result will depend on the previous and subsequent data, so using base64 won't work in general unless you can guarantee the offset of the input delimiter for all cases, and it's always 0 mod 3.

哦,下一次尝试逐步浏览IDE调试器中的代码.您很快就会知道发生了什么.

Oh, and next time try stepping through the code in your IDE debugger. You would have quickly seen what was happening.

这篇关于将TIFF文件拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆