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();
// }
}
}
Java_图片切片的更多相关文章
- iOS 图片切片的简单实现
图片切片就是将一张图片按一定比例切分,中间部分系统自动填充空白, 这样在文本多行输入的时候,将具有特定形状的图片作为背景, 即使文本行数很多,也不会使图片走形. //即使你按5/5分,还是3/7分系统 ...
- Java_图片处理_02_图片处理工具类库
二.参考文档 1.Java图片处理工具类库
- Java_图片转字符
把高达头像转换成字符[-V-] 调节双循环里y与x的增量改变字符输出的细节.高和长 public class ImgToStr { public static void main(String arg ...
- CSS魔法堂:重拾Border之——图片作边框
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- 9个超绚丽的HTML5 3D图片动画特效
在Web 1.0时代,我们的网页中图片数量非常少,而且都是以静态图片为主.HTML5的出现,推动了Web 2.0的发展,同时也催生出了很多绚丽的HTML5图片动画特效,特别是有些还有3D的动画效果.本 ...
- 前端之Photoshop切片
什么是切片 ? (Photoshop中的切片) 切片:将图片切成几部分,一片一片往上传,这样上传的速度比较快.每个切片作为一个独立的文件传输,文件中包含切片自己的设置.颜色调板.链接.翻转效果 ...
- Bing Maps进阶系列九:使用MapCruncher进行地图切片并集成进Bing Maps
Bing Maps进阶系列九:使用MapCruncher进行地图切片并集成进Bing Maps 在Bing Maps开发中,由于各种应用功能的不同,更多的时候用户可能需要将自己的一部分图片数据作为地图 ...
- css3 border img 边框图片
摘自http://www.html-js.com/article/CSS3-tutorial-css3borderimage-frame-image-Xiangjie-on border-image摘 ...
- GIS技术在医疗行业的应用:利用切片地图发布技术解决dmetrix数字病理切片在线浏览
最近一直在研究切片地图发布技术,解决各种矢量和栅格数据的切片地图制作和发布问题.这块的技术在土地评估和调查类公司中应用较多,因为他们经常需要使用各地地图,传统的文件管理方式很难适应工作现状,如果将各种 ...
随机推荐
- javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/websocket/{sid}] : existing endpoint was class com.sanyi.qibaobusiness.framework.webSocket.WebSocketServe
报错: javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/w ...
- dom4j,json,pattern性能对比【原】
报文大概2000字节,对比时为只取其中某个节点的值即可. 以下对比可知取少量节点时pattern性能是远大于dom4j,和json的, 但取大量的时候就不能这么以偏概全了. dom4j和pattern ...
- Linux查看系统信息的命令及已安装软件包的命令
系统 uname -a查看内核/操作系统/CPU信息head -n 1 /etc/issue查看操作系统版本cat /proc/cpuinfo查看CPU信息hostname查看计算机名lspci -t ...
- [物理学与PDEs]第1章习题1 无限长直线的电场强度与电势
设有一均匀分布着电荷的无限长直线, 其上的电荷线密度 (即单位长度上的电荷量) 为 $\sigma$. 试求该直线所形成的电场的电场强度及电势. 解答: 设空间上点 $P$ 到直线的距离为 $r$, ...
- SSH框架之Hibernate《一》
hibernate的基础入门 一:hibernate和ORM的概念部分 1.1hibernate概述: Hibernate框架是当今主流的Java持久层框架之一 ...
- 如何让vue项目兼容IE浏览器
一般来说项目开发到后期都需要做各种兼容性处理例如:360.IE9以上.QQ浏览器....等等 那么现在来介绍一个工具 babel-cli 跟 babel-preset-es2015 babel-cli ...
- Centos7.5 安装VirtualBox增强工具
一.安装 1.自带tools: 选择VirtualBox工具栏 => 设备 => 安装增强功能 2.挂载光驱 3.进入光驱目录,执行(一定要用root权限执行) ①安装gcc yum i ...
- ES进阶--02
第11节深度探秘搜索技术_案例实战基于dis_max实现best fields策略进行多字段搜索 课程大纲 1.为帖子数据增加content字段 POST /forum/article/_bulk{ ...
- Windows 下安装Git工具及基础使用
Git简介 git是很好一个工具使用,可以执行liunx命令,有git环境后windows系统就可以进行shell命令操作,就可以添加其他liunx辅助软件进行执行,git也代码库管理工具,无论是上传 ...
- 【原创】大叔问题定位分享(6)Dubbo monitor服务iowait高,负载高
一 问题 Dubbo monitor所在服务器状态异常,iowait一直很高,load也一直很高,监控如下: iowait如图: load如图: 二 分析 通过iotop命令可以查看当前系统中磁盘io ...