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();
// }
}
}

Java_图片切片的更多相关文章

  1. iOS 图片切片的简单实现

    图片切片就是将一张图片按一定比例切分,中间部分系统自动填充空白, 这样在文本多行输入的时候,将具有特定形状的图片作为背景, 即使文本行数很多,也不会使图片走形. //即使你按5/5分,还是3/7分系统 ...

  2. Java_图片处理_02_图片处理工具类库

    二.参考文档 1.Java图片处理工具类库

  3. Java_图片转字符

    把高达头像转换成字符[-V-] 调节双循环里y与x的增量改变字符输出的细节.高和长 public class ImgToStr { public static void main(String arg ...

  4. CSS魔法堂:重拾Border之——图片作边框

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  5. 9个超绚丽的HTML5 3D图片动画特效

    在Web 1.0时代,我们的网页中图片数量非常少,而且都是以静态图片为主.HTML5的出现,推动了Web 2.0的发展,同时也催生出了很多绚丽的HTML5图片动画特效,特别是有些还有3D的动画效果.本 ...

  6. 前端之Photoshop切片

    什么是切片 ?     (Photoshop中的切片) 切片:将图片切成几部分,一片一片往上传,这样上传的速度比较快.每个切片作为一个独立的文件传输,文件中包含切片自己的设置.颜色调板.链接.翻转效果 ...

  7. Bing Maps进阶系列九:使用MapCruncher进行地图切片并集成进Bing Maps

    Bing Maps进阶系列九:使用MapCruncher进行地图切片并集成进Bing Maps 在Bing Maps开发中,由于各种应用功能的不同,更多的时候用户可能需要将自己的一部分图片数据作为地图 ...

  8. css3 border img 边框图片

    摘自http://www.html-js.com/article/CSS3-tutorial-css3borderimage-frame-image-Xiangjie-on border-image摘 ...

  9. GIS技术在医疗行业的应用:利用切片地图发布技术解决dmetrix数字病理切片在线浏览

    最近一直在研究切片地图发布技术,解决各种矢量和栅格数据的切片地图制作和发布问题.这块的技术在土地评估和调查类公司中应用较多,因为他们经常需要使用各地地图,传统的文件管理方式很难适应工作现状,如果将各种 ...

随机推荐

  1. 第三节:ThreadPool的线程开启、线程等待、线程池的设置、定时功能

    一. ThreadPool简介 ThreadPool简介:ThreadPool是一个线程池,当你需要开启n个线程时候,只需把这个指令抛给线程池,它将自动分配线程进行处理,它诞生于.Net 2.0时代. ...

  2. SpringBoot系列: url重定向和转发

    Web UI项目中, 很多 Spring controller 视图函数直接返回 html 页面, 还有一些视图函数是要重定向或转发到其他的 url 上. redirect 和 forward的区别: ...

  3. Gram 矩阵与向量到子空间的距离

    设 $W$ 是 $n$ 维 Euclidean 空间 $V$ 的子空间, $\beta\in V$, 定义 $\beta$ 到 $W$ 的距离  $$\bex  \rd (\beta,W)=|\bet ...

  4. luogu 1731 搜索剪枝好题

    搜索剪枝这个东西真的是骗分利器,然鹅我这方面菜的不行,所以搜索数学dp三方面是真的应该好好训练一下 一本通的确该认真的刷嗯 #include<bits/stdc++.h> using na ...

  5. FTP主动及被动模式效果图

  6. Coursera, Big Data 2, Modeling and Management Systems (week 1/2/3)

    Introduction to data management 整个coures 2 是讲data management and storage 的,主要内容就是分布式文件系统,HDFS, Redis ...

  7. 「NOI2017」泳池

    DP式子比后面的东西难推多了 LOJ2304 Luogu P3824 UOJ #316 题意 给定一个长度为$ n$高为$ \infty$的矩形 每个点有$ 1-P$的概率不可被选择 求最大的和底边重 ...

  8. idea一款颜值很高的theme

    在idea的plugins搜索theme,能看到一款人气值超高的插件,下载使用了确实很漂亮!

  9. Android Studio项目Gradle内网配置

    由于内网无法连接到外部网络,在使用Gradle编译Android Studio项目时就会面临一些问题: 1.Gradle安装文件无法下载 2.Gradle Android插件无法下载 3.项目依赖文件 ...

  10. [转载]解决在win10中webstrom无法使用命令行(Terminal)

    转载地址:https://qiaolevip.iteye.com/blog/2217688 原因:计算机从win7更新到win10,webstorm9命令框无法输入,以为是webstorm问题和win ...