图片尺寸相似度匹配
来源:互联网 发布:淘宝美工摄影 编辑:程序博客网 时间:2024/05/01 18:46
由于页面布局需要,页面中需要放置图片的位置,其宽高(比)总是固定的,而后台某一类图片尺寸又是千差万别,为了使放置在页面上的图片,其被拉伸程度缩短到最小,需要后台对图片尺寸进行匹配,挑选出一个宽高(比)最近接的图片放置于页面进行显示
如,以下为存储图片的PO类
public class PicContentPO { /** * 图片名称。 */ private String name; /** * 图像宽度 单位像素。 */ private Integer picWidth; /** * 图像高度 单位像素。 */ private Integer picHeight;public Integer getPicWidth() {return picWidth;}public void setPicWidth(Integer picWidth) {this.picWidth = picWidth;}public Integer getPicHeight() {return picHeight;}public void setPicHeight(Integer picHeight) {this.picHeight = picHeight;} public String getName() { return name; } public void setName(String name) { this.name = name; }}
为了从众多PicContentPO中,找出与指定宽高(比)最接近的图片,可使用Comparator类对众多PicContentPO进行排序,下标越小的PicContentPO,其匹配程度越高
class PosterComparator implements Comparator<PicContentPO> { private int width; private int height; public PosterComparator(int width, int height) { this.height = height; this.width = width; } /** * 算法说明:值越小和固定尺寸越匹配 * S1=非重叠部分面积 S0=重叠部分面积 K1=V1,V2图形斜率 K0=固定尺寸的斜率 * (|K1-K0|+1) * S1/S0 */ @Override public int compare(PicContentPO v1, PicContentPO v2) { // v1 > v2 v1参数有误,选v2 if (v1 == null || v1.getPicHeight() == null || v1.getPicWidth() == null || v1.getPicHeight() == 0 || v1.getPicWidth() == 0) { return 1; } // v1 < v2 v2参数有误,选v1 if (v2 == null || v2.getPicHeight() == null || v2.getPicWidth() == null || v2.getPicHeight() == 0 || v2.getPicWidth() == 0) { return -1; } // 计算v1和标准区域的差别值 int minHeight = Math.min(height, v1.getPicHeight()); int minWidth = Math.min(width, v1.getPicWidth()); // v1和标准的非重叠部分面积 // 两个图形相互不包含 // ┌────┐ // │ │ // ├────┼────┐ // │ │ │ // └────┴────┘ int result1 = Math.abs((v1.getPicWidth() - width) * minHeight) + Math.abs((v1.getPicHeight() - height) * minWidth); // 两个图形一大包含一小 // ┌──────────────┐ // ├──────┐ │ // │ │ │ // └──────┴───────┘ if ((v1.getPicWidth() - width) * (v1.getPicHeight() - height) > 0) { result1 = result1 + (v1.getPicWidth() - width) * (v1.getPicHeight() - height); } // v1与标准尺寸的匹配系数 result1 = (Math.abs(v1.getPicHeight() / v1.getPicWidth() - height / width) + 1) * result1 / (minHeight * minWidth); // 计算v2和标准区域的差别值 minHeight = Math.min(height, v2.getPicHeight()); minWidth = Math.min(width, v2.getPicWidth()); // v2和标准的非重叠部分面积 // 两个图形相互不包含 int result2 = Math.abs((v2.getPicWidth() - width) * minHeight) + Math.abs((v2.getPicHeight() - height) * minWidth); // 两个图形一大包含一小 if ((v2.getPicWidth() - width) * (v2.getPicHeight() - height) > 0) { result2 = result2 + (v2.getPicWidth() - width) * (v2.getPicHeight() - height); } // v2与标准尺寸的匹配系数 result2 = (Math.abs(v2.getPicHeight() / v2.getPicWidth() - height / width) + 1) * result2 / (minHeight * minWidth); return result1 - result2; // 差别值越小,权重越高 } }
0 0
- 图片尺寸相似度匹配
- 文本相似度匹配
- SimilarityUtil相似度匹配
- 名称相似匹配度算法
- 中文相似度匹配算法
- 计算字符匹配相似度
- 中文相似度匹配算法
- 通用算法-sql相似度模糊匹配
- 图像与轮廓的相似度匹配
- 图片匹配(图片相似度对比)
- 矩形相似度匹配 , 两条件决定因素的相似度匹配
- 入栈出栈括号匹配相似
- 相似文档匹配
- solr的相似匹配
- solr 相似匹配
- Solr之相似匹配。
- 分词查询按照匹配度相似度进行排序
- 向量相似度匹配之豪氏距离
- acm-uva488(水题)
- java术语(PO/POJO/VO/BO/DAO/DTO)
- 关于websocket
- 【出差】家乡的味道--哈尔滨
- 旋转数组的最小数字
- 图片尺寸相似度匹配
- 常量池
- [leetcode] Swap Nodes in Pairs
- 情人节表白程序
- Zookeeper 3.4.6 试水 & Hbase 0.98.3 for Hadoop 2 单节点配置
- bzoj1432: [ZJOI2009]Function
- HDU 1009 FatMouse' Trade (贪心、结构体排序)
- 利用MJRefresh下拉刷新上拉加载
- URL的一些符号处理