最近项目需要用到后端来解析海康摄像机二进制数据获取红外全屏温度,经过一番查找之后最终找到方法
技术博客http://idea.coderyj.com/
1.拿到的温度数据
65, 13, -88, 65, 44, -117, -87, 65, 13, 30, -87, 65, 35, -96, -89, 65, -87, -21, -91, 65, 77, -92, -92
- 是16进制的
将四个字节解析为一个温度数据
,封装类的代码 - 核心代码
int l;
l = buffer[0];
l &= 0xff;
l |= ((long) buffer[1] << 8);
l &= 0xffff;
l |= ((long) buffer[2] << 16);
l &= 0xffffff;
l |= ((long) buffer[3] << 24);
String tempData = Float.intBitsToFloat(l) + "";
- 完整代码
// 根据红外二进制原始图片 获取全屏温度
public static String handleImageData(String srcPath, int width){
int k = srcPath.indexOf(".");
if (k == -1){
return null;
}
// 目标文件
String desPath = srcPath.substring(0, k) + ".json";
File file = new File(srcPath);
if (!file.exists()){
return null;
}
// 65, 13, -88, 65, 44, -117, -87, 65, 13, 30, -87, 65, 35, -96, -89, 65, -87, -21, -91, 65, 77, -92, -92
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[4];
ArrayList<String> list = new ArrayList<>();
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
// 处理读取的字节数据
int l;
l = buffer[0];
l &= 0xff;
l |= ((long) buffer[1] << 8);
l &= 0xffff;
l |= ((long) buffer[2] << 16);
l &= 0xffffff;
l |= ((long) buffer[3] << 24);
String tempData = Float.intBitsToFloat(l) + "";
String number = String.format("%.2f", Double.parseDouble(tempData));
list.add(number);
}
BufferedWriter bw = new BufferedWriter(new FileWriter(desPath));
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
bw.write(s);
bw.write(",");
int j = i + 1;
if (j % width == 0){
bw.newLine();
}
}
bw.close();
return desPath;
// for (int i = 0; i < 512; i++) {
// for (int j = 0; j < 640; j++) {
// int index = j + i*640;
// String s = list.get(index);
// System.out.println("s" + s);
// bw.write(s);
// bw.write(",");
// }
// bw.newLine();
// }
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
2.根据坐标获取最高温
// 根据json文件以及区域获取最大值
public static Double getJsonHeightTemp(String path, int x, int y, int width, int height){
File file = new File(path);
try {
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String line;
ArrayList<String[]> list = new ArrayList<>();
while ((line = reader.readLine()) != null){
// System.out.println("line--" + line);
String[] split = line.split(",");
// System.out.println("split--" + Arrays.toString(split));
list.add(split);
}
reader.close();
fileReader.close();
// 0 0 2 2
ArrayList<Double> doubles = new ArrayList<Double>();
for (int i = y; i < 512; i++) {
if (i > y + width){
continue;
}
for (int j = x; j < 640; j++) {
if (j > x + height){
continue;
}
String[] strings = list.get(j);
String s = strings[i];
String number = String.format("%.2f", Double.parseDouble(s));
// System.out.println("x:" + j + ",y:" + i + ",温度值:" + number);
doubles.add(Double.parseDouble(number));
}
}
Double max = Collections.max(doubles);
// System.out.println("max--" + max);
return max;
}catch (Exception e){
}
return -1.0;
}
3.使用
public static void main(String[] args) throws IOException {
String path = "D:\\桌面1\\双圆轨\\红外测温\\hot_M1PL0006_36517_00_002_545_18396_100_2024_03_28_16_24_53.csv";
String s = ImageUtils.handleImageData(path, 640);
System.out.println("path--" + s);
int x = 0;
int y = 0;
int width = 1;
int height = 1;
Double jsonHeightTemp = ImageUtils.getJsonHeightTemp(s, x, y, width, height);
System.out.println("jsonHeightTemp--" + jsonHeightTemp);
}
4.海康官方demo
分享链接: https://drive.ticklink.com/hcs/controller/hik-manage/fileDownload?link=09D1mBTu& 提取码:V5F9
备用地址 https://wwk.lanzouj.com/i7TrR1szgrng 密码:6qs0