Java crop / cut image - 使用Java 批量剪割图片

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

}


<<:  区块链与物联网的两人三脚

>>:  计算机概论 - 作业系统 Operating systems

Day27:测试 Coroutine

Coroutine 是非同步程序的解决方案,我们将耗时的任务置放在 suspend 函式中,在正常的...

Day2关於『程序』的起源和特性&演算法

最一开始的程序是机器代码(machine code),演变成组合代码(assembly code),...

成为工具人应有的工具包-05 DataProtectionDecryptor

DataProtectionDecryptor 今天来认识 DataProtectionDecryp...

Day13:内建的 suspend 函式,好函式不用吗? (2)

withContext suspend fun<T> withContext(conte...

[13th][Day20] http request header(下)

接续昨天 header 的部分: If-Modified-Since:只在最近有来源最近有异动时发送...