图片尺寸相似度匹配

本文介绍了一种用于在多个图片中找到与指定宽高比最接近的图片的算法。该算法通过计算图片与目标尺寸之间的差异来确定最佳匹配,并使用Comparator进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    由于页面布局需要,页面中需要放置图片的位置,其宽高(比)总是固定的,而后台某一类图片尺寸又是千差万别,为了使放置在页面上的图片,其被拉伸程度缩短到最小,需要后台对图片尺寸进行匹配,挑选出一个宽高(比)最近接的图片放置于页面进行显示

如,以下为存储图片的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; // 差别值越小,权重越高
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值