海康威视ISAPI协议获取全屏温度数据

获取全屏温度接口

GET http://192.168.3.28/ISAPI/Thermal/channels/2/thermometry/jpegPicWithAppendData?format=json

接口返回三部分内容:json结果、全屏温度图片、全屏温度数据;

调用全屏测温接口

    /**
     * 下载文件
     * @param url 下载地址
     * @param headerMap 请求头
     * @param filePath 文件路径
     * @throws IOException
     */
    public static void download(String url, Map headerMap, String filePath) throws IOException {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("192.168.3.28", 80), // 替换为你的服务器和端口
                new UsernamePasswordCredentials("admin", "kbzn2020") // 替换为你的用户名和密码
        );
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        HttpGet request = new HttpGet(url);
        // 填充请求头
        if (!CollectionUtils.isEmpty(headerMap)) {
            headerMap.forEach((key, value) -> {
                request.setHeader(key, value);
            });
        }
        CloseableHttpResponse response = httpClient.execute(request);
        try (InputStream inputStream =  response.getEntity().getContent();
             OutputStream outputStream = new FileOutputStream(filePath)) {

            int bytesRead = -1;
            byte[] buffer = new byte[4096];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            System.out.println("File downloaded");
        }
    }

解析二进制流为JPEG图片

    /**
     * 保存二进制流为图片
     *
     * @param bytes
     */
    public void saveImage(byte[] bytes) {
        // 二进制流数组
        List byteList = new ArrayList<>();
        // 开始标志位(JPEG格式要求)
        String beginFlag = "FF D8";
        // 结束标志位(JPEG格式要求)
        String endFlag = "FF D9";
        // 开始截取字符串标记
        boolean flag = false;
        for (int i = 1; i < bytes.length; i++) {
            byte preByte = bytes[i - 1];
            byte itemByte = bytes[i];
            String preByteStr = String.format("%02X", preByte);
            String itemByteStr = String.format("%02X", itemByte);
            String content = preByteStr + " " + itemByteStr;

            if (content.equals(beginFlag)) {
                flag = true;
                byteList.add(preByte);
            }
            if (content.equals(endFlag)) {
                flag = false;
            }
            if (flag) {
                byteList.add(itemByte);
            }
        }

        File image = new File("F:\\jpeg\\" + ImageUtils.getImageName());
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(image);
            outputStream.write(convertToByteArray(byteList));
        } catch (IOException e) {
            System.out.println("写入文件失败!");
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                System.out.println("关闭输出流失败!");
            }
        }
    }

字节集合转字节数组

    /**
     * 字节集合转字节数组
     *
     * @param byteList
     * @return
     */
    public static byte[] convertToByteArray(List byteList) {
        // 创建一个与List大小相同的字节数组
        byte[] byteArray = new byte[byteList.size()];

        // 遍历List并将每个Byte对象转换为byte并存储到数组中
        for (int i = 0; i < byteList.size(); i++) {
            byteArray[i] = byteList.get(i); // Byte对象自动拆箱为byte
        }

        return byteArray;
    }

测试

    /**
     * 获取抓热图结果
     *
     * @throws IOException
     */
    @GetMapping("/jpegPicWithAppendData")
    public void jpegPicWithAppendData() throws IOException {
        // 直接下载文件到本地(HttpClient返回的报文不是ANSI编码,需要直接下载到本地,才是ANSI编码)
        String filePath = "F:\\isapi\\response\\response";
        HttpClientUtils.download("http://192.168.3.28/ISAPI/Thermal/channels/2/thermometry/jpegPicWithAppendData?format=json",
                null,
                filePath);
        byte[] bytes = FileUtil.readBytes(filePath);
        // 解析热成像图片
        saveImage(bytes);
    }

你可能感兴趣的:(java,图像处理,音视频)