package cutpicture;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
import javax.imageio.ImageIO;
public class CutPicture {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, IOException {
var dirName = "C:\\test\\";
var des = "C:\\destination\\";
for (var f : fileList(dirName)) {
File fullpath = new File(f.toString());
String number = fullpath.getName().toString().split("_")[0];
cropImage(fullpath.toString(), number , des);
System.out.println(number);
}
}
public static List fileList(String dirName) {
List result = null;
try ( Stream<Path> walk = Files.walk(Paths.get(dirName))) {
result = walk.filter(p -> !Files.isDirectory(p)) // not a directory
.map(p -> p.toString()) // convert path to string
.filter(f -> f.endsWith("png")) // check end with
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
System.out.println("List error ");
return null;
}
return result;
}
public static void cropImage(String filePath , String i , String des) {
try {
Image src = ImageIO.read(new File(filePath));
int x = 572, y = 28, w = 776, h = 1049;
BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
dst.getGraphics().drawImage(src, 0, 0, w, h, x, y, x + w, y + h, null);
ImageIO.write(dst, "png", new File(des + i + "_cropped.jpg"));
} catch (Exception e) {
e.printStackTrace();
System.out.println("Crop Error");
}
}
}