我正在尝试创建一种在不破坏子目录格式的情况下调整特定文件夹中所有图像大小的方法。我有两个整数数组,它们指定要调整大小的图像的宽度和高度。我对该方法使用了递归调用,以遍历文件夹及其子目录。
问题是,每当我使用递归调用时,当我希望它从中断处开始时,它都从width和height数组的开头开始。我该如何解决?
resizeFiles(File[] folderToResize, String inputImagePath, int backgroundColor) throws IOException {
int width[] = {36, 48, 72, 96, 96, 48, 72, 36, 48, 72, 96, 128, 48, 50, 80, 80, 1024, 40, 114, 57, 58, 60, 120, 180, 144, 72, 144, 76, 152, 87, 57, 114, 72, 144, 114, 57, 144, 72, 180, 128, 64, 173, 48, 62, 180, 800, 480, 320, 200, 480, 320, 1280, 720, 480, 320, 480, 240, 225, 2008, 1024, 1536, 1536, 768, 768, 960, 480, 640, 640, 640, 640, 320, 320, 64, 480};
int height[] = {36, 48, 72, 96, 96, 48, 72, 36,48, 72, 96, 128, 48, 50, 80, 80, 1024, 40, 114, 57, 58, 60, 120, 180, 144, 72, 144, 76, 152, 87, 57, 114, 72, 144, 114, 57, 144, 72, 180, 128, 64, 173, 48, 62, 180, 800, 480, 320, 200, 480, 320, 1280, 720, 480, 320, 480, 240, 225, 1536, 783, 2008, 2008, 1004, 1004, 640, 320, 960, 960, 1136, 1136, 480, 480, 64, 800};
for (int i = 0; i < folderToResize.length; i++) {
if (folderToResize[i].isDirectory()) {
resizeFiles(folderToResize[i].listFiles(), inputImagePath, backgroundColor);
} else {
System.out.println("File Pathway: " + folderToResize[i].getAbsolutePath() + " Width: " + width[i] + " Height: " + height[i]);
resize(inputImagePath, folderToResize[i].getAbsolutePath(), width[i], height[i], backgroundColor);
}
}
}
编辑: 我将代码更改为此,但是现在它无法正确通过目录。奇怪的是,我没有更改其通过目录的方式。任何帮助将不胜感激。
resizeFiles(File[] folderToResize, String inputImagePath, int backgroundColor, int width[], int height[], int previousIndex) throws IOException { //won't walk through the directories properly
for (int i = 0; i < folderToResize.length; i++) {
if (folderToResize[i].isDirectory()) {
resizeFiles(folderToResize[i].listFiles(), inputImagePath, backgroundColor, width, height, i);
} else {
System.out.println("File Pathway: " + folderToResize[i].getAbsolutePath() + " Width: " + width[previousIndex] + " Height: " + height[previousIndex] + " Index: " + previousIndex);
resize(inputImagePath, folderToResize[i].getAbsolutePath(), width[previousIndex], height[previousIndex], backgroundColor);
break;
}
}
}