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. centos7项目部署

    1. 安装nginx   添加CentOS 7 Nginx yum资源库 sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/ng ...

  2. CMDB资产管理系统开发【day26】:linux客户端开发

    客户端疑难点及获取流程 1.linux客户端支持2就可以,python3就是很麻烦 难道你要求所有的客户端都上pytho3吗? 现在从bin的入口进去 HouseStark.ArgvHandler(s ...

  3. gdb core 调试多线程

    ref :http://blog.sina.com.cn/s/blog_62dc94eb0100flyn.html 如果目标进程已经core dump了,那么 gdb -c core xxx   xx ...

  4. SQLMAP注入教程-11种常见SQLMAP使用方法详解

    sqlmap也是渗透中常用的一个注入工具,其实在注入工具方面,一个sqlmap就足够用了,只要你用的熟,秒杀各种工具,只是一个便捷性问题,sql注入另一方面就是手工党了,这个就另当别论了.今天把我一直 ...

  5. ElasticSearch 索引 剖析

    ElasticSearch index 剖析 在看ElasticSearch权威指南基础入门中关于:分片内部原理这一小节内容后,大致对ElasticSearch的索引.搜索底层实现有了一个初步的认识. ...

  6. How to avoid the 0-byte file on Webclient download error

    _client.DownloadDataAsync(new Uri(url)); _client.DownloadDataCompleted += (sender, e) => { try { ...

  7. Sublime text3 连接sftp/ftp(远程服务器)

    1.按下Ctrl + Shift + P调出命令面板2.在输入框中输入Sftp,按回车下载3.建一个新的文件夹放到左边的项目栏中4.右击文件夹,选中SFTP/FTP,点击Map to Remote5. ...

  8. webstorm javascript 分号提示如何自动补全或去掉?

    转载于: https://segmentfault.com/q/1010000006930809?_ea=1180552 如图片显示,每行末尾都会提示你加上分号,如何让IDE自动完成这个操作或者取消这 ...

  9. ActiveMQ之topic主题模式

    开发环境我们使用的是ActiveMQ 5.11.1 Release的Windows版,官网最新版是ActiveMQ 5.12.0 Release,大家可以自行下载,下载地址.需要注意的是,开发时候,要 ...

  10. HTML及CSS学习笔记

    推荐博客:付铭 day-01 HTML 1.HTML 基本语法 html标签 单标签 <img /> .<img> 双标签 <html> </html> ...