如何用Java批量裁剪文件夹内所有图片并自动保存到指定目录
Java 批量图片空白裁剪实现
你原有单张处理的裁剪逻辑无需改动,只需修改main方法增加文件夹遍历、批量处理逻辑即可,完整修改后的代码如下:
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class code { // 配置输入输出文件夹路径,注意Java路径中反斜杠需要转义为双反斜杠 private static final String INPUT_DIR = "D:\\eclipse-java-workspace\\image_manipulation\\sample_input_images\\"; private static final String OUTPUT_DIR = "D:\\eclipse-java-workspace\\image_manipulation\\sample_out_images\\"; private BufferedImage img; public static void main(String[] args) throws IOException { // 输出文件夹不存在则自动创建 File outputDir = new File(OUTPUT_DIR); if (!outputDir.exists()) { outputDir.mkdirs(); } // 遍历输入文件夹下所有jpg格式图片 File inputDir = new File(INPUT_DIR); File[] imageFiles = inputDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".jpg")); if (imageFiles == null || imageFiles.length == 0) { System.out.println("输入文件夹内未找到符合要求的图片"); return; } // 批量处理所有图片 for (File imageFile : imageFiles) { System.out.println("开始处理图片:" + imageFile.getName()); code trim = new code(imageFile); trim.trim(); // 输出文件保留原文件名 File outputFile = new File(OUTPUT_DIR + imageFile.getName()); trim.write(outputFile); System.out.println("图片处理完成,已保存至:" + outputFile.getAbsolutePath()); } } public code(File input) { try { img = ImageIO.read(input); } catch (IOException e) { throw new RuntimeException( "Problem reading image", e ); } } public void trim() { System.out.println(getTrimmedHeight2()); BufferedImage img2 = img.getSubimage(getTrimmedWidth2(), getTrimmedHeight2(), getTrimmedWidth()-getTrimmedWidth2(), getTrimmedHeight()-getTrimmedHeight2()); BufferedImage copyOfImage = new BufferedImage(img2.getWidth(), img2.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = copyOfImage.createGraphics(); g.drawImage(img2, 0, 0, null); img = copyOfImage; } private int getTrimmedWidth2() { int height = this.img.getHeight(); int width = this.img.getWidth(); int lowest = 99999; for(int i = 0; i < height; i++) { for(int j = 0; j < width-1; j++) { if(img.getRGB(j, i) != Color.WHITE.getRGB() ) { if(j<lowest) { lowest = j; } break; } } } return lowest; } public void write(File f) { try { ImageIO.write(img, "jpg", f); } catch (IOException e) { throw new RuntimeException( "Problem writing image", e ); } } private int getTrimmedWidth() { int height = this.img.getHeight(); int width = this.img.getWidth(); int trimmedWidth = 0; for(int i = 0; i < height; i++) { for(int j = width - 1; j >= 0; j--) { if(img.getRGB(j, i) != Color.WHITE.getRGB() && j > trimmedWidth) { trimmedWidth = j; break; } } } return trimmedWidth ; } private int getTrimmedHeight() { int width = this.img.getWidth(); int height = this.img.getHeight(); int trimmedHeight = 0; for(int i = 0; i < width; i++) { for(int j = height - 1; j >= 0; j--) { if(img.getRGB(i, j) != Color.WHITE.getRGB() && j > trimmedHeight) { trimmedHeight = j; break; } } } return trimmedHeight; } private int getTrimmedHeight2() { int width = this.img.getWidth(); int height = this.img.getHeight(); int lowest = 99999; for(int i = 0; i < width; i++) { for(int j = 0; j < height-1; j++) { if(img.getRGB(i, j) != Color.WHITE.getRGB() ) { if(j<lowest) { System.out.println(j); lowest = j; } break; } } } return lowest; } }
使用说明
- 修改代码开头的
INPUT_DIR和OUTPUT_DIR为你本地的实际输入输出文件夹路径即可使用 - 当前默认仅处理后缀为
jpg的图片,如需支持png等其他格式,可修改文件名过滤规则,同时调整ImageIO.write的第二个参数为对应格式 - 输出文件会保留原图片的文件名,不会出现覆盖问题
内容的提问来源于stack exchange,提问作者jigz
相关产品推荐
相关产品推荐

