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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.netsdk.demo.frame.vto;
 
import com.sun.jna.Callback;
import com.sun.jna.Pointer;
import com.netsdk.common.PaintPanel;
import com.netsdk.common.Res;
import com.netsdk.common.SavePath;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
 
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
 
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
 
import static com.netsdk.lib.NetSDKLib.EVENT_IVS_ACCESS_CTL;
 
/**
 * @author 47081
 * @version 1.0
 * @description vto智能分析的回调函数, 建议写成单例模式
 * 对应接口 {@link NetSDKLib#CLIENT_RealLoadPictureEx(NetSDKLib.LLong, int, int, int, Callback, Pointer, Pointer)}
 * @date 2020/8/15
 */
public class VTOAnalyzerCallBack implements NetSDKLib.fAnalyzerDataCallBack {
    private static VTOAnalyzerCallBack INSTANCE;
    private JTable table;
    private PaintPanel paintPanel;
    private BufferedImage bufferedImage;
 
    private VTOAnalyzerCallBack(JTable table,PaintPanel panel) {
        this.table = table;
        this.paintPanel=panel;
    }
 
    public static VTOAnalyzerCallBack getINSTANCE(JTable table,PaintPanel paintPanel) {
        if (INSTANCE == null) {
            INSTANCE = new VTOAnalyzerCallBack(table,paintPanel);
        }
        return INSTANCE;
    }
 
    @Override
    public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) {
        //门禁事件
        if (dwAlarmType == EVENT_IVS_ACCESS_CTL) {
            NetSDKLib.DEV_EVENT_ACCESS_CTL_INFO info = new NetSDKLib.DEV_EVENT_ACCESS_CTL_INFO();
            ToolKits.GetPointerDataToStruct(pAlarmInfo, 0, info);
            //更新列表
            if(table!=null){
                DefaultTableModel model= (DefaultTableModel) table.getModel();
                NetSDKLib.NET_TIME_EX time=info.UTC;
                if(info.UTC.dwYear==0&&info.UTC.dwMonth==0&&info.UTC.dwDay==0){
                    time=info.stuFileInfo.stuFileTime;
                }
                model.addRow(new Object[]{new String(info.szUserID).trim(),new String(info.szCardNo).trim(),time.toString().trim(),getEventInfo(info).trim()});
            }
            if(pBuffer != null && dwBufSize > 0) {
                String strFileName = SavePath.getSavePath().getSaveCapturePath();
                byte[] buf = pBuffer.getByteArray(0, dwBufSize);
                ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
                try {
                    bufferedImage = ImageIO.read(byteArrInput);
                    if (bufferedImage == null) {
                        return 0;
                    }
                    ImageIO.write(bufferedImage, "jpg", new File(strFileName));
                } catch (IOException e) {
                    e.printStackTrace();
                }
 
                // 界面显示抓图
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        paintPanel.setOpaque(false);
                        paintPanel.setImage(bufferedImage);
                        paintPanel.repaint();
                    }
                });
            }
        }
        return 1;
    }
 
    /**
     * 获取事件信息
     * @param info
     * @return
     */
    private String getEventInfo(NetSDKLib.DEV_EVENT_ACCESS_CTL_INFO info){
        StringBuilder builder=new StringBuilder();
        builder.append(Res.string().getChannel()).append(info.nChannelID).append(",")
                .append(Res.string().getOpenMethod()).append(openDoorMethod(info.emOpenMethod)).append(",")
                .append(Res.string().getOpenStatus()).append(info.bStatus==1?Res.string().getSucceed():Res.string().getFailed());
        return builder.toString();
    }
    /**
     * 开门方式
     * @param emOpenMethod
     * @return
     */
    private String openDoorMethod(int emOpenMethod) {
        String method;
        switch (emOpenMethod) {
            case NetSDKLib.NET_ACCESS_DOOROPEN_METHOD.NET_ACCESS_DOOROPEN_METHOD_CARD:
                method = Res.string().getCard();
                break;
            case NetSDKLib.NET_ACCESS_DOOROPEN_METHOD.NET_ACCESS_DOOROPEN_METHOD_FACE_RECOGNITION:
                method = Res.string().getTargetRecognition();
                break;
            case NetSDKLib.NET_ACCESS_DOOROPEN_METHOD.NET_ACCESS_DOOROPEN_METHOD_FINGERPRINT:
                method = Res.string().getFingerPrint();
                break;
            case NetSDKLib.NET_ACCESS_DOOROPEN_METHOD.NET_ACCESS_DOOROPEN_METHOD_REMOTE:
                method = Res.string().getRemoteCapture();
                break;
            default:
                method = Res.string().getUnKnow();
                break;
        }
        return method;
    }
}