java图片裁剪

读取文件夹内的所有图片并裁剪返回到指定文件夹。用于图片的快速裁剪,精修图片做不到,毕竟这是程序来做的。

package com.nbomb.route.test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageResize {

    public static void main(String[] args) {
        double SCALE_FACTOR = 1.15;
        String sourceFolderPath = "D:\\下载\\小红书";
        String targetFolderPath = "D:\\下载\\裁剪小红书";
        int targetWidth = 900;
        int targetHeight = 1200;

        File sourceFolder = new File(sourceFolderPath);
        File[] sourceFiles = sourceFolder.listFiles();

        for (File sourceFile : sourceFiles) {
            if (sourceFile.isFile() && isImageFile(sourceFile)) {
                try {
                    BufferedImage sourceImage = ImageIO.read(sourceFile);


                    int scaledWidth = (int) (sourceImage.getWidth() * SCALE_FACTOR);
                    if (scaledWidth<900){
                        scaledWidth = 1000;
                    }
                    int scaledHeight = (int) (sourceImage.getHeight() * SCALE_FACTOR);
                    if (scaledHeight<1200){
                        scaledHeight=1200;
                    }

                    BufferedImage scaledImage = scaleImage(sourceImage, scaledWidth, scaledHeight);
                    BufferedImage resizedImage = cropImage(scaledImage, targetWidth, targetHeight);

                    File targetFile = new File(targetFolderPath, sourceFile.getName());
                    ImageIO.write(resizedImage, "jpg", targetFile);

                    System.out.println("Processed: " + sourceFile.getName());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static boolean isImageFile(File file) {
        String extension = getFileExtension(file).toLowerCase();
        return extension.endsWith("jpg") || extension.endsWith("jpeg") || extension.endsWith("png");
    }

    private static String getFileExtension(File file) {
        String fileName = file.getName();
        int indexOfLastDot = fileName.lastIndexOf(".");
        return indexOfLastDot != -1 && indexOfLastDot != 0 ? fileName.substring(indexOfLastDot + 1) : "";
    }

    private static BufferedImage scaleImage(BufferedImage sourceImage, int width, int height) {
        Image scaledImage = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage bufferedImage = new BufferedImage(width, height, sourceImage.getType());
        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.drawImage(scaledImage, 0, 0, null);
        g2d.dispose();
        return bufferedImage;
    }

    private static BufferedImage cropImage(BufferedImage sourceImage, int width, int height) {
        int x = (sourceImage.getWidth() - width) / 2;
        int y = (sourceImage.getHeight() - height) / 2;
        return sourceImage.getSubimage(x, y, width, height);
    }
}

你可能感兴趣的:(java,开发语言,github,idea)