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. 分布式监控系统开发【day38】:监控trigger表结构设计(一)

    一.需求讨论 1.zabbix触发器的模板截图 1.zabbix2.4.7 2.zabbix3.0 2.模板与触发器关联的好处 好处就是可以批量处理,比如我说我有1000机器都要监控cpu.内存.IO ...

  2. Mybatis-generator自动生成器

    目录 前言牢骚 Mybatis-generator是什么? Mybatis-generator怎么使用? 第一步,Maven文件引用 第二步,数据库建立 第三步,写mybatis-generator. ...

  3. 用 Mathematica 获取图片的 RGB 三基色

    ColorConvert[*, "RGB"] // InputForm 其中 * 表示你把你的图片拖入 Mathematica 中.

  4. 打印文章 FZOJ 5190

    传送门 #include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned lon ...

  5. LeetCode第十四题-字符串数组中最长的共同前缀

    Longest Common Prefix 问题简介: 编写一个函数来查找字符串数组中最长的公共前缀字符串,如果没有公共前缀,则返回空字符串"" 举例: 1: 输入: [“xwq” ...

  6. 【原创】大叔经验分享(25)hive通过外部表读写hbase数据

    在hive中创建外部表: CREATE EXTERNAL TABLE hive_hbase_table(key string, name string,desc string) STORED BY ' ...

  7. CENTOS 7 安装 TINYPROXY 代理服务器

    https://www.cnblogs.com/new_2050/p/7658508.html

  8. WPF学习:3.Border & Brush

    上一章<WPF学习:2.Layout-Panels-Countainers>主要介绍了布局,容器和面板.这一章主要开始介绍Border(边界)和Brush(画刷). 代码地址:http:/ ...

  9. 【web】服务器推送的实现方式(转)

    轮询 http流 websocket   https://www.cnblogs.com/freud/p/8397934.html http2.0 浅谈Websocket.Ajax轮询和长连接(lon ...

  10. web开篇

    一.内容回顾 1.python基础 2.网络编程 3.并发编程 4.前端 5.数据库(MySQL) 二.今日概要 1.了解Web应用程序的本质 2.Django简介及安装使用 三.今日详细 1.最简单 ...