Java_图片切片

package com.creditease.fetch.credit.util.similarity;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片切割工具类
 */
public class ImageCuter {
    /**
     * 切割图片
     *
     * @param image     源文件
     * @param width     切片宽度
     * @param height    切片高度
     * @param rowsLimit 纵向切片总数
     * @param colsLimit 横向切片总数
     * @return
     * @throws Exception
     */
    public static List<BufferedImage> cut(BufferedImage image, int width, int height, Integer rowsLimit, Integer colsLimit){
        List<BufferedImage> list = new ArrayList<>();
        int sWidth = image.getWidth(); // 图片宽度
        int sHeight = image.getHeight(); // 图片高度
        if (sWidth > width && sHeight > height) {
            int cols = 0; // 横向切片总数
            int rows = 0; // 纵向切片总数
            int eWidth = 0; // 末端切片宽度
            int eHeight = 0; // 末端切片高度
            if (sWidth % width == 0) {
                cols = sWidth / width;
            } else {
                eWidth = sWidth % width;
                cols = sWidth / width + 1;
            }
            if (sHeight % height == 0) {
                rows = sHeight / height;
            } else {
                eHeight = sHeight % height;
                rows = sHeight / height + 1;
            }
            if (rowsLimit != null) {
                rows = rowsLimit;
            }
            if (colsLimit != null) {
                cols = colsLimit;
            }
            BufferedImage subImage = null;
            int cWidth = 0; // 当前切片宽度
            int cHeight = 0; // 当前切片高度
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    cWidth = getWidth(j, cols, eWidth, width);
                    cHeight = getHeight(i, rows, eHeight, height);
                    // x坐标,y坐标,宽度,高度
                    subImage = image.getSubimage(j * width, i * height, cWidth,
                            cHeight);
                    list.add(subImage);
                }
            }
        }
        return list;
    }

    /**
     * 获取当前切片的宽度
     *
     * @param index    横向索引
     * @param cols     横向切片总数
     * @param endWidth 末端切片宽度
     * @param width    切片宽度
     * @return
     */
    private static int getWidth(int index, int cols, int endWidth, int width) {
        if (index == cols - 1) {
            if (endWidth != 0) {
                return endWidth;
            }
        }
        return width;
    }

    /**
     * 获取当前切片的高度
     *
     * @param index     纵向索引
     * @param rows      纵向切片总数
     * @param endHeight 末端切片高度
     * @param height    切片高度
     * @return
     */
    private static int getHeight(int index, int rows, int endHeight, int height) {
        if (index == rows - 1) {
            if (endHeight != 0) {
                return endHeight;
            }
        }
        return height;
    }

    public static void main(String[] args) {
//        ImageCuter imageCuter = new ImageCuter();
//        try {
//            imageCuter.cut(new File("t5.jpg"), "E:\test2\", 16, 16);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
原文地址:https://www.cnblogs.com/gisblogs/p/6823949.html