图片裁剪:读取特定目录下所有图片,等分裁剪

原链接:https://blog.csdn.net/hgyu_962464/article/details/79278222

在原链接上进行了调整,直接批量读取特定目录下照片,进行裁剪,可直接运行。

import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static javafx.scene.input.KeyCode.F;
import static javafx.scene.input.KeyCode.SOFTKEY_0;


public class CutImageUtil {

    public static void main(String[] args) {
        List<File> fileList = new ArrayList();
        fileList = CutImageUtil.cutImageToFile("E:\\Program\\testleetcode\\source", "E:\\Program\\testleetcode\\target", 4);
        for (File file : fileList) {
            System.out.println(file.getAbsolutePath());
        }

    }

    /**
     * 切割图片
     *
     * @param sourceFilePath 源文件路径
     * @param sourceFilePath 保存文件路径
     * @param index          水平方向等分切割数
     * @return List<File>   File数组
     */
    public static List<File> cutImageToFile(String sourceFilePath, String targetDir, int index) {
        File file = new File(sourceFilePath);
        if (file.exists()) {
            return cutImageToFile(file, targetDir, index);
        } else {
            return null;
        }
    }

    /**
     * 切割图片
     *
     * @param sourceFile 源文件
     * @param index      水平方向等分切割数
     * @return List<String> base64编码数组
     */
    public static List<String> cutImageToBase64(File sourceFile, int index) {
        List<String> list = new ArrayList<String>();
        int suffixIndex = sourceFile.getName().lastIndexOf(".");
        String suffix = sourceFile.getName().substring(suffixIndex + 1);
        try {
            BufferedImage source = ImageIO.read(sourceFile);
            int width = source.getWidth(); // 图片宽度
            int height = source.getHeight(); // 图片高度
            if (index > 1) {
                int cWidth = width / index; // 切片宽度
                BufferedImage image = null;
                for (int i = 0; i < index; i++) {
                    // x坐标,y坐标,宽度,高度
                    BASE64Encoder encoder = new BASE64Encoder();
                    int cw = i * cWidth;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    image = source.getSubimage(cw, 0, cWidth, height);
                    ImageIO.write(image, "PNG", baos);
                    byte[] bytes = baos.toByteArray();
                    list.add("data:image/" + suffix + ";base64," + encoder.encodeBuffer(bytes));
                    baos.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 切割图片
     *
     * @param sourceFile 源文件
     * @param index      水平方向等分切割数
     * @return List<String> base64编码数组
     */
    public static List<File> cutImageToFile(File sourceFile, String targetDir, int index) {
        List<File> list = new ArrayList<File>();
        File[] fileList = sourceFile.listFiles();
        for(File file : fileList) {
            int suffixIndex = file.getName().lastIndexOf(".");
            String suffix = file.getName().substring(suffixIndex + 1);
            String name = file.getName().substring(0, suffixIndex);
            try {
                BufferedImage source = ImageIO.read(file);
                int width = source.getWidth(); // 图片宽度
                int height = source.getHeight(); // 图片高度
                if (index > 1) {
                    int cheight = height / index; // 切片高度
                    BufferedImage image = null;
                    File file1 = new File(targetDir);
                    if (!file.exists()) { // 存储目录不存在,则创建目录
                        file.mkdirs();
                    }
                    for (int i = 0; i < index; i++) {
                        // x坐标,y坐标,宽度,高度
                        int ch = i * cheight;
                        image = source.getSubimage(0, ch, width, cheight);
                        String fileName = targetDir + "/" + name + "_" + i + "." + suffix;
                        file = new File(fileName);
                        ImageIO.write(image, "PNG", file);
                        list.add(file);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return list;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值