java解析海康摄像机二进制数据获取红外全屏温度

最近项目需要用到后端来解析海康摄像机二进制数据获取红外全屏温度,经过一番查找之后最终找到方法
技术博客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

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果想要通过海康工业相机二次开发获取温度,需要使用支持温度测量功能的海康相机,并使用对应的SDK进行开发。以下是一个简单的获取海康相机温度的C++示例代码: ```c++ #include <iostream> #include "HCNetSDK.h" using namespace std; void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE* pBuffer, DWORD dwBufSize, void* pUser) { // 处理图像数据 } int main() { NET_DVR_Init(); // 登录相机 LONG lUserID = NET_DVR_Login_V30("192.168.1.100", 8000, "admin", "password", NULL); if (lUserID < 0) { cout << "Login failed, error code: " << NET_DVR_GetLastError() << endl; NET_DVR_Cleanup(); return 0; } // 启用温度测量功能 NET_DVR_THERMOMETRY_COND struCond = {0}; struCond.dwSize = sizeof(NET_DVR_THERMOMETRY_COND); struCond.byMode = THERMOMETRY_MODE_PLANAR; struCond.struPoint.byThermometryUnit = 1; struCond.struPoint.byEnableAlarmTemp = 1; struCond.struPoint.fAlarmTemp = 30.0; struCond.struPoint.struPoint.fX = 0.5; struCond.struPoint.struPoint.fY = 0.5; LONG lHandle = NET_DVR_StartRemoteConfig(lUserID, NET_DVR_SET_THERMOMETRY_COND, &struCond, sizeof(NET_DVR_THERMOMETRY_COND), NULL, NULL); if (lHandle < 0) { cout << "StartRemoteConfig failed, error code: " << NET_DVR_GetLastError() << endl; NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return 0; } DWORD dwRetLen = 0; NET_DVR_THERMOMETRY_COND struCondRet = {0}; if (!NET_DVR_GetNextRemoteConfig(lHandle, &struCondRet, sizeof(NET_DVR_THERMOMETRY_COND), &dwRetLen)) { cout << "GetNextRemoteConfig failed, error code: " << NET_DVR_GetLastError() << endl; NET_DVR_StopRemoteConfig(lHandle); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return 0; } // 开始实时预览 NET_DVR_PREVIEWINFO struPreviewInfo = {0}; struPreviewInfo.hPlayWnd = NULL; struPreviewInfo.lChannel = 1; struPreviewInfo.dwStreamType = 0; struPreviewInfo.dwLinkMode = 0; LONG lRealHandle = NET_DVR_RealPlay_V40(lUserID, &struPreviewInfo, fRealDataCallBack, NULL); if (lRealHandle < 0) { cout << "RealPlay failed, error code: " << NET_DVR_GetLastError() << endl; NET_DVR_StopRemoteConfig(lHandle); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return 0; } // 获取温度 NET_DVR_THERMOMETRY_ALARM struAlarm = {0}; if (NET_DVR_GetTempAlarm(lRealHandle, &struAlarm)) { float fTemp = struAlarm.struPoint.fTempValue; cout << "Temperature: " << fTemp << endl; } else { cout << "GetTempAlarm failed, error code: " << NET_DVR_GetLastError() << endl; } // 停止实时预览 NET_DVR_StopRealPlay(lRealHandle); // 停用温度测量功能 NET_DVR_StopRemoteConfig(lHandle); // 注销相机 NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return 0; } ``` 需要注意的是,以上代码仅供参考,实际开发中需要根据具体情况进行修改和优化。同时,使用海康相机进行温度测量需要满足一定的硬件和环境条件,否则可能会影响测量结果的准确性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值