获取指定范围像素点

  1 package com.coalbot.isapidemo.utils;
  2 
  3 import lombok.Data;
  4 
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 import java.util.stream.Collectors;
  8 
  9 /**
 10  * @ClassName:TempDataUtils
 11  * @Author: XinHai.Ma
 12  * @Date: 2025/1/24 17:01
 13  * @Description: 必须描述类做什么事情, 实现什么功能
 14  */
 15 public class TempDataUtils {
 16 
 17     /**
 18      * 温度数据表格
 19      */
 20     private static List<List<Float>> tempDataTable = null;
 21 
 22     /**
 23      * 初始化表格
 24      *
 25      * @param width  宽
 26      * @param height 高
 27      */
 28     public static void initDataTable(int width, int height) {
 29         tempDataTable = new ArrayList<>(height);
 30         for (int h = 0; h < height; h++) {
 31             List<Float> row = new ArrayList<>();
 32             for (int w = 0; w < width; w++) {
 33                 row.add(Float.parseFloat(String.valueOf(h * h)));
 34             }
 35             tempDataTable.add(row);
 36         }
 37     }
 38 
 39     /**
 40      * 获取最大值
 41      *
 42      * @param one   左上
 43      * @param two   右上
 44      * @param three 右下
 45      * @param four  左下
 46      * @return
 47      */
 48     public static Float getMaxValue(Point one, Point two, Point three, Point four) {
 49         List<List<Float>> range = getRange(one, two, three, four);
 50         List<Float> result = range.stream()
 51                 .flatMap(List::stream)
 52                 .collect(Collectors.toList());
 53         return result.stream().max(Float::compareTo).get();
 54     }
 55 
 56     /**
 57      * 获取最小值
 58      *
 59      * @param one   左上
 60      * @param two   右上
 61      * @param three 右下
 62      * @param four  左下
 63      * @return
 64      */
 65     public static Float getMinValue(Point one, Point two, Point three, Point four) {
 66         List<List<Float>> range = getRange(one, two, three, four);
 67         List<Float> result = range.stream()
 68                 .flatMap(List::stream)
 69                 .collect(Collectors.toList());
 70         return result.stream().min(Float::compareTo).get();
 71     }
 72 
 73     /**
 74      * 获取平均值
 75      * @param one   左上
 76      * @param two   右上
 77      * @param three 右下
 78      * @param four  左下
 79      * @return
 80      */
 81     public static Double getAverage(Point one, Point two, Point three, Point four) {
 82         List<List<Float>> range = getRange(one, two, three, four);
 83         List<Float> result = range.stream()
 84                 .flatMap(List::stream)
 85                 .collect(Collectors.toList());
 86         return result.stream().mapToDouble(Float::doubleValue).average().getAsDouble();
 87     }
 88 
 89     /**
 90      * 获取坐标范围
 91      *
 92      * @param one   左上
 93      * @param two   右上
 94      * @param three 右下
 95      * @param four  左下
 96      * @return
 97      */
 98     public static List<List<Float>> getRange(Point one, Point two, Point three, Point four) {
 99         int beginY = two.getY();
100         int endY = three.getY();
101         List<List<Float>> result = new ArrayList<>(endY - beginY);
102         for (int h = beginY; h < endY; h++) {
103             int beginX = one.getX();
104             int endX = two.getX();
105             List<Float> newRow = new ArrayList<>(endX - beginX);
106             for (int w = beginX; w < endX; w++) {
107                 List<Float> row = tempDataTable.get(h);
108                 Float tempData = row.get(w);
109                 newRow.add(tempData);
110             }
111             result.add(newRow);
112         }
113         return result;
114     }
115 
116     /**
117      * 根据两个坐标生成四个坐标
118      * @param beginPoint
119      * @param endPoint
120      * @return
121      */
122     public static Point[] generatePoint(Point beginPoint, Point endPoint) {
123         Point[] result = new Point[4];
124         result[0] = beginPoint;
125         result[1] = new Point(endPoint.getX(), beginPoint.getY());
126         result[2] = endPoint;
127         result[3] = new Point(beginPoint.getX(), endPoint.getY());
128         return result;
129     }
130 
131     public static void main(String[] args) {
132         TempDataUtils.initDataTable(20, 20);
133         Point one = new Point(0, 0);
134         Point two = new Point(10, 0);
135         Point three = new Point(10, 10);
136         Point four = new Point(0, 10);
137         Float maxValue = TempDataUtils.getMaxValue(one, two, three, four);
138         Float minValue = TempDataUtils.getMinValue(one, two, three, four);
139         Double average = getAverage(one, two, three, four);
140         System.out.println("maxValue: " + maxValue + ",minValue: " + minValue + ",average: " + average);
141 
142         Point[] points = generatePoint(one, three);
143         for (Point point : points) {
144             System.out.println(point);
145         }
146     }
147 
148 }
149 
150 /**
151  * 坐标
152  */
153 @Data
154 class Point {
155     private int x;
156     private int y;
157 
158     public Point() {
159     }
160 
161     public Point(int x, int y) {
162         this.x = x;
163         this.y = y;
164     }
165 }

 

posted @ 2025-01-24 17:58  尘世间迷茫的小书童  阅读(16)  评论(0)    收藏  举报