使用海康威视ISAPI协议获取测温图片

使用海康威视ISAPI协议获取测温图片

ISAPI 全屏测温接口:

 1 http://<IP>:<PORT>/ISAPI/Thermal/channels/<channelId>/thermometry/jpegPicWithAppendData?format=json 

接口会返回三部分数据:json数据、jpeg二进制流、全屏温度数据;

 

调用全屏测温接口:

 1     /**
 2      * 下载文件
 3      * @param url 下载地址
 4      * @param headerMap 请求头
 5      * @param filePath 文件路径
 6      * @throws IOException
 7      */
 8     public static void download(String url, Map<String, String> headerMap, String filePath) throws IOException {
 9         CredentialsProvider credsProvider = new BasicCredentialsProvider();
10         credsProvider.setCredentials(
11                 new AuthScope("192.168.3.28", 80), // 替换为你的服务器和端口
12                 new UsernamePasswordCredentials("admin", "kbzn2020") // 替换为你的用户名和密码
13         );
14         CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
15         HttpGet request = new HttpGet(url);
16         // 填充请求头
17         if (!CollectionUtils.isEmpty(headerMap)) {
18             headerMap.forEach((key, value) -> {
19                 request.setHeader(key, value);
20             });
21         }
22         CloseableHttpResponse response = httpClient.execute(request);
23         try (InputStream inputStream =  response.getEntity().getContent();
24              OutputStream outputStream = new FileOutputStream(filePath)) {
25 
26             int bytesRead = -1;
27             byte[] buffer = new byte[4096];
28             while ((bytesRead = inputStream.read(buffer)) != -1) {
29                 outputStream.write(buffer, 0, bytesRead);
30             }
31             System.out.println("File downloaded");
32         }
33     }

 

解析二进制流为jpeg图片

 1     /**
 2      * 保存二进制流为图片
 3      * @param bytes
 4      */
 5     public void saveImage(byte[] bytes) {
 6         // 二进制流数组
 7         List<Byte> byteList = new ArrayList<>();
 8         // 开始标志位(JPEG格式要求)
 9         String beginFlag = "FF D8";
10         // 结束标志位(JPEG格式要求)
11         String endFlag = "FF D9";
12         // 开始截取字符串标记
13         boolean flag = false;
14         for (int i = 1; i < bytes.length; i++) {
15             byte preByte = bytes[i - 1];
16             byte itemByte = bytes[i];
17             String preByteStr = String.format("%02X", preByte);
18             String itemByteStr = String.format("%02X", itemByte);
19             String content = preByteStr + " " + itemByteStr;
20 
21             if (content.equals(beginFlag)) {
22                 flag = true;
23                 byteList.add(preByte);
24             }
25             if (content.equals(endFlag)) {
26                 flag = false;
27             }
28             if (flag) {
29                 byteList.add(itemByte);
30             }
31         }
32 
33         File image = new File("F:\\jpeg\\" + ImageUtils.getImageName());
34         FileOutputStream outputStream = null;
35         try {
36             outputStream = new FileOutputStream(image);
37             outputStream.write(convertToByteArray(byteList));
38         } catch (IOException e) {
39             System.out.println("写入文件失败!");
40         } finally {
41             try {
42                 outputStream.close();
43             } catch (IOException e) {
44                 System.out.println("关闭输出流失败!");
45             }
46         }
47     }

 

字节集合转字节数组:

 1     /**
 2      * 字节集合转字节数组
 3      * @param byteList
 4      * @return
 5      */
 6     public static byte[] convertToByteArray(List<Byte> byteList) {
 7         // 创建一个与List大小相同的字节数组
 8         byte[] byteArray = new byte[byteList.size()];
 9 
10         // 遍历List并将每个Byte对象转换为byte并存储到数组中
11         for (int i = 0; i < byteList.size(); i++) {
12             byteArray[i] = byteList.get(i); // Byte对象自动拆箱为byte
13         }
14 
15         return byteArray;
16     }

 

调用方法:

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

 

posted @ 2025-01-25 09:19  尘世间迷茫的小书童  阅读(708)  评论(0)    收藏  举报