我需要使用分隔符II *將一個tiff文件拆分爲多個tiff文件,因此我使用下面的代碼將tiff文件轉換爲base64並使用子字符串提取第一個圖片 。不過,我得到如下錯誤。請告知如何僅使用此分隔符II *提取tiff文件中的第一張圖像(base64代碼爲SUkq)。將TIFF文件拆分爲多個文件
我可以在不執行子字符串的情況下解碼圖像。
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);
}
}
你可以試試這個解決方案,讓我知道你的意見。 https://stackoverflow.com/a/45583553/7731623 –
該代碼不會爲我工作,因爲我的文件包含多個tiff文件在一個單獨的,每個都有一個單獨的元數據。所以這段代碼只會讀取我文件中的第一張圖片。感謝您的建議,我已經找到了解決方案,將圖像轉換爲字節數組,並逐字讀取每個圖像,並將其分配給單獨的輸出流,然後使用twlevemonkeys tiff編寫器將所有流合併到單個tiff。 – Vinoth