zoukankan      html  css  js  c++  java
  • 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();
    //        }
        }
    }
  • 相关阅读:
    关于本博客
    洛谷P3387 【模板】缩点 题解
    spfa学习笔记
    Google Chrome Download
    Kosaraju算法学习
    fhq treap 学习笔记
    OIerChat
    python request.get(h.html),用xpath获取数据为空
    k8s 用ingress暴露集群环境中的服务。
    harbor push 报received unexpected HTTP status: 500 Internal Server Error
  • 原文地址:https://www.cnblogs.com/gisblogs/p/6823949.html
Copyright © 2011-2022 走看看