1
zhanghua
2024-09-26 c775c6953d9759e70f08acbfa8f6d7490aaae3d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.netsdk.demo.customize.heatmap;
 
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.callback.impl.DefaultVideoStatHeatMapCallBack;
import com.netsdk.module.HeatMapModule;
import com.netsdk.module.entity.BmpInfo;
import com.netsdk.module.entity.HeatMapGrayData;
import com.sun.jna.Pointer;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;
 
/**
 * @author 47081
 * @version 1.0
 * @description 远程抓图合成热度图回调函数, 回调函数建议写成单例模式, 不然可能会有意想不到的错误
 * @date 2020/9/27
 */
public class SnapPicHeatMapCallBack implements NetSDKLib.fSnapRev {
  private static volatile SnapPicHeatMapCallBack instance;
  private HeatMapModule heatMapModule;
 
  private SnapPicHeatMapCallBack() {
    this.heatMapModule = new HeatMapModule();
  }
 
  public static SnapPicHeatMapCallBack getInstance() {
    if (instance == null) {
      synchronized (SnapPicHeatMapCallBack.class) {
        if (instance == null) {
          instance = new SnapPicHeatMapCallBack();
        }
      }
    }
    return instance;
  }
 
  @Override
  public void invoke(
      NetSDKLib.LLong lLoginID,
      Pointer pBuf,
      int RevLen,
      int EncodeType,
      int CmdSerial,
      Pointer dwUser) {
    // jpg格式的Buf
    byte[] buf = pBuf.getByteArray(0, RevLen);
    ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
    try {
      BufferedImage bufferedImage = ImageIO.read(byteArrInput);
      if (bufferedImage == null) {
        return;
      }
      ByteArrayOutputStream byteArrOutput = new ByteArrayOutputStream();
      ImageIO.write(bufferedImage, "bmp", byteArrOutput);
 
      ////////////////// 获取背景图的缓存、宽高 /////////////
      byte[] buffer = byteArrOutput.toByteArray(); // bmp格式的Buf
      int width = bufferedImage.getWidth();
      int height = bufferedImage.getHeight();
      /** 获取位深度 */
      int bitCount = bufferedImage.getColorModel().getPixelSize();
      // 54是头
      int nBackPicLen = width * height * bitCount / 8 + 54;
      BmpInfo backInfo = new BmpInfo(buffer, width, height, bitCount, 1, nBackPicLen);
 
      HeatMapGrayData grayData = DefaultVideoStatHeatMapCallBack.grayDatas.get(HeatMapDemo.token);
      BmpInfo grayInfo =
          new BmpInfo(
              grayData.getData(),
              grayData.getWidth(),
              grayData.getHeight(),
              grayData.getnBit(),
              0,
              grayData.getLength());
      // 叠加背景图,生成bmp文件
      String random = UUID.randomUUID().toString().replace(".", "");
      String fileName = "D:/test" + random + ".bmp";
      if (heatMapModule.createHeatMap(grayInfo, backInfo, fileName, 0.5f)) {
        System.out.println("叠加图保存到" + fileName);
        // 叠加成功,移除灰度数据
        DefaultVideoStatHeatMapCallBack.grayDatas.remove(HeatMapDemo.token);
      } else {
        System.out.println("生成叠加图失败");
      }
 
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}