Split TIFF Files to multiple files

2019-07-25 03:55发布

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)

Class File

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

}

标签: java base64 tiff
1条回答
乱世女痞
2楼-- · 2019-07-25 04:21

In this code

substring(imageDataString.indexOf("SUkq") + 1, imageDataString.indexOf("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.

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)

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

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.

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

查看更多
登录 后发表回答