• 找出多组分辨率中最接近目标值的一组


    package com.test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Test {
    	
    	public static void main(String[] args) {
    		
    		// 目标分辨率
    		int targetWidth = 640;
    		int targetHeight = 480;
    		
    		//String str = "3264x2448,3264x1836,2560x1920,3264x1472,3200x1440,2304x1728,2560x1440,1920x1920,2048x1536,2304x1296,1920x1440,2400x1080,1920x1080";
    		String str = "2560x1920,3264x1472,3200x1440,2304x1728,2560x1440,1920x1920,2048x1536,2304x1296,1920x1440,2400x1080,2304x1040,1920x1080,1632x1224,1600x1200,1440x1080,1280x960,1088x1088,1600x720,1280x720,960x540,800x600,800x480,720x480,352x288,320x240,176x144";
    		
    		String[] ary1 = str.split(",");
    		List<String> list = new ArrayList<String>();
    		
    		for (int i = 0; i < ary1.length; i++) {
    			String sss = ary1[i];
    			list.add(sss);
    		}
    		
    		//获取和目标值最接近的一组分辨率
    		String s = getBestResolution(list,targetWidth,targetHeight);
    		
    		// 输出最接近目标分辨率的数据
    		System.out.println("最接近目标分辨率的数据为:"+s);
    		
    	}
    
    	/**
    	 * 	获取和目标值最接近的一组分辨率
    	 * @param list 分辨率集合,例如:[960x540,800x600,800x480,720x480,352x288,320x240,176x144]
    	 * @param targetWidth 目标分辨率宽,例如:640
    	 * @param targetHeight 目标分辨率高,例如:480
    	 * @return 和目标值最接近的一组分辨率,例如:720x480
    	 */
    	public static String getBestResolution(List<String>list,int targetWidth,int targetHeight) {
    		// 数据集合
    		List<Resolution> resolutions = new ArrayList<Resolution>();
    		
    		for (int i = 0; i < list.size(); i++) {
    			String sss = list.get(i);
    			String[] ary2 = sss.split("x");
    			Resolution resolution = new Resolution(Integer.parseInt(ary2[0]), Integer.parseInt(ary2[1]));
    			resolutions.add(resolution);
    		}
    		
    		// 初始化最小差距为最大值
    		int minDifference = Integer.MAX_VALUE;
    		Resolution closestResolution = null;
    
    		// 遍历数据集合,找到最接近目标分辨率的数据
    		for (Resolution resolution : resolutions) {
    			int difference = Math.abs(resolution.width - targetWidth) + Math.abs(resolution.height - targetHeight);
    			if (difference < minDifference) {
    				minDifference = difference;
    				closestResolution = resolution;
    			}
    		}
    		String res = String.format("%dx%d",closestResolution.width,closestResolution.height);
    		return res;
    	}
    
    	// 分辨率类
    	static class Resolution {
    		int width;
    		int height;
    
    		public Resolution(int width, int height) {
    			this.width = width;
    			this.height = height;
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    输出结果

    最接近目标分辨率的数据为:720x480
    
    • 1
  • 相关阅读:
    《Java基础入门第2版》--黑马程序员 课后答案及其详解 第1章 Java开发入门
    STM32Cube学习(5)——PWM
    【linux命令讲解大全】114. 网络状态监测工具iptstate和lnstat的使用
    C++项目实战——基于多设计模式下的同步&异步日志系统-⑫-日志宏&全局接口设计(代理模式)
    数据挖掘、机器学习、深度学习的区别
    Java基础学习总结(195)—— 关于 Java 8 中的日期处理总结
    canvas绘制马路和屏幕
    道光,咸丰,同治,光绪和溥仪,跟慈禧到底是怎样的关系?
    openstack-同一物理机中透传不同GPU时的nova配置记录
    MyBatisPlus入门学习笔记
  • 原文地址:https://blog.csdn.net/xiejunna/article/details/133749129