繁体   English   中英

如何裁剪 1 个文件夹中的所有图像?

[英]How do you crop all images in 1 folder?

我有一个项目需要裁剪文件夹中的所有图像。 到目前为止,它确实裁剪并擦除了它周围的额外空白,但是,我需要将确切的文件名放在文件路径上。 我附上了下面的代码,以向您展示我的代码的作用。 所以简而言之,我需要帮助让我的代码裁剪整个图像文件夹,而不仅仅是 1。然后自动将裁剪后的图像保存在另一个文件夹中。 谢谢你。

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

public class code {
    private BufferedImage img;
    public static void main(String[] args) throws IOException {
        code trim = new code(new File("D:\\eclipse-java-workspace\\image_manipulation\\sample_input_images\\DP936BA.jpg"));
        System.out.println("Starting first image..");
        trim.trim();
        trim.write(new File("D:\\eclipse-java-workspace\\image_manipulation\\sample_out_images\\output5.jpg"));
    
    }
      
    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;
    }


}

您可以通过使用文件夹路径来获取文件列表File[] fileList = directory.listFiles() ,然后使用循环遍历列表并为每个文件创建一个代码对象并将其修剪for (int i = 0; i < fileList.length; i++) {...} .

以下是这可能如何工作的示例。 我添加了解释步骤的注释。 请注意,我尚未测试代码,但它应该可以工作:

//Path to your folder
final static String folderPath = "D:/your/directory/path/";

public static void main(String[] args) throws IOException {
    //Get a list of all files in the folder
    File[] fileList = new File(folderPath).listFiles();

    System.out.println("Starting first image..");

    //Loop through the above list of files
    for (int i = 0; i < fileList.length; i++){
        //For each file create a new code object and trim it
        code trim = new code(fileList[i]);
        trim.trim();

        //Create a file path to save the trimmed file with the same name but add "_TRIMMED" to the end
        //You will need to alter this line to put the file extension in the correct place
        String newFileName = getPath() + "/" + fileList[i].getName() + "_TRIMMED"; 

        //Finally save the updated file to the new path           
        trim.write(new File(newFileName));
    }    

    System.out.println("All images trimmed..");
}

您还应该在尝试修剪之前检查fileList中的文件是否不是文件夹,并且它们是图像,而不是仅仅抛出 RuntimeException 并停止整个应用程序。 但我会把那部分留给你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM