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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.netsdk.demo.customize;
 
import com.netsdk.demo.util.CaseMenu;
import com.netsdk.lib.Enum.NET_EM_CFG_OPERATE_TYPE;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.callback.impl.DefaultHaveReconnectCallBack;
import com.netsdk.lib.structure.NET_CFG_GBMODE_INFO;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
 
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import static com.netsdk.lib.Utils.getOsPrefix;
 
public class VideoWidgetDemo {
 
    // SDk对象初始化
    public static final NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
    public static final NetSDKLib configsdk = NetSDKLib.CONFIG_INSTANCE;
 
    /**
     * 选择通道
     */
    private int channelId = -1;
    // 判断是否初始化
    private static boolean bInit = false;
    // 判断log是否打开
    private static boolean bLogOpen = false;
    // 设备信息
    private NetSDKLib.NET_DEVICEINFO_Ex deviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
    // 登录句柄
    private NetSDKLib.LLong loginHandle = new NetSDKLib.LLong(0);
    // 智能事件订阅句柄
    private NetSDKLib.LLong m_attachHandle = new NetSDKLib.LLong(0);
 
    //实现断线重连静态单例
    public static class DefaultDisconnectCallback implements NetSDKLib.fDisConnect {
        private static volatile DefaultDisconnectCallback INSTANCE;
 
        private DefaultDisconnectCallback() {}
 
        public static DefaultDisconnectCallback getINSTANCE() {
            if (INSTANCE == null) {
                synchronized (DefaultDisconnectCallback.class){
                    if (INSTANCE == null){
                        INSTANCE = new DefaultDisconnectCallback();
                    }
                }
            }
            return INSTANCE;
        }
 
        @Override
        public void invoke(NetSDKLib.LLong lLoginID, String pchDVRIP, int nDVRPort, Pointer dwUser) {
            System.out.printf("Device[%s] Port[%d] DisConnected!\n", pchDVRIP, nDVRPort);
        }
    }
 
    // 回调函数需要是静态的,防止被系统回收
    // 断线回调
    private static NetSDKLib.fDisConnect disConnectCB = MultiTalkDevDemo.DefaultDisconnectCallback.getINSTANCE();
    // 重连回调
    private static NetSDKLib.fHaveReConnect haveReConnectCB = DefaultHaveReconnectCallBack.getINSTANCE();
 
    //订阅句柄
    NetSDKLib.LLong lAttachHandle = new NetSDKLib.LLong(0);
    // 编码格式
    public static String encode;
 
    static {
        String osPrefix = getOsPrefix();
        if (osPrefix.toLowerCase().startsWith("win32-amd64")) {
            encode = "GBK";
        } else if (osPrefix.toLowerCase().startsWith("linux-amd64")) {
            encode = "UTF-8";
        }
    }
 
    /**
     * 按照指定格式,获取当前时间
     */
    public static String getDate() {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDate.format(new Date()).replaceAll("[^0-9]", "-");
    }
 
    /**
     * 初始化SDK库
     */
    public static boolean initSdk() {
        bInit = netsdk.CLIENT_Init(disConnectCB, null);// 进程启动时,初始化一次
        if (!bInit) {
            System.out.println("Initialize SDK failed");
            return false;
        }
        // 配置日志
        enableLog();
        // 设置断线重连回调接口, 此操作为可选操作,但建议用户进行设置
        netsdk.CLIENT_SetAutoReconnect(haveReConnectCB, null);
        // 设置登录超时时间和尝试次数,可选
        // 登录请求响应超时时间设置为3S
        int waitTime = 3000; //单位为ms
        // 登录时尝试建立链接 1 次
        int tryTimes = 1;
        // 设置连接设备超时时间和尝试次数
        netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);
        // 设置更多网络参数, NET_PARAM 的nWaittime , nConnectTryNum 成员与 CLIENT_SetConnectTime
        // 接口设置的登录设备超时时间和尝试次数意义相同,可选
        NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
        // 登录时尝试建立链接的超时时间
        netParam.nConnectTime = 10000;
        // 设置子连接的超时时间
        netParam.nGetConnInfoTime = 3000;
        //设置登陆网络环境
        netsdk.CLIENT_SetNetworkParam(netParam);
        return true;
    }
 
    /**
     * 打开 sdk log
     */
    private static void enableLog() {
        // SDK全局日志打印信息
        NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
        //设置日志路径
        File path = new File("sdklog/");
        //判断日志路径是否存在,若不存在则创建
        if (!path.exists()){
            path.mkdir();
        }
        // 这里的log保存地址依据实际情况自己调整
        String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + "sdklog" + getDate() + ".log";
        //日志输出策略,0:输出到文件(默认); 1:输出到窗口,
        setLog.nPrintStrategy = 0;
        //是否重设日志路径, 取值0否 ,1是
        setLog.bSetFilePath = 1;
        //日志路径(默认"./sdk_log/sdk_log.log")
        byte[] szLogFilePath = setLog.szLogFilePath;
        //自定义log保存地址,将数据logPath数据copy到LOG_SET_PRINT_INFO-->szLogFilePath变量中
        System.arraycopy(logPath.getBytes(), 0, szLogFilePath, 0, logPath.getBytes().length);
        //是否重设日志打印输出策略 取值0否 ,1是
        setLog.bSetPrintStrategy = 1;
        // 打开日志功能
        bLogOpen = netsdk.CLIENT_LogOpen(setLog);
        if (!bLogOpen){
            System.err.println("Failed to open NetSDK log "+ ToolKits.getErrorCode());
        }else {
            System.out.println("Success to open NetSDK log ");
        }
    }
 
    /**
     * 高安全登录
     */
    public void loginWithHighLevel() {
        // 输入结构体参数
        NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstlnParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY() {
            {
                // IP
                szIP = m_strIpAddr.getBytes();
                // 端口
                nPort = m_nPort;
                // 用户名
                szUserName = m_strUser.getBytes();
                // 密码
                szPassword = m_strPassword.getBytes();
            }
        };
        // 登录的输出结构体参数
        NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
        // 高安全级别登陆
        loginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstlnParam, pstOutParam);
        if (loginHandle.longValue() == 0) {  //登陆失败
            System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", m_strIpAddr, m_nPort, ToolKits.getErrorCode());
 
        } else {
            //登陆成功
            // 获取设备信息
            deviceInfo = pstOutParam.stuDeviceInfo;
            System.out.printf("Login Success Device Address[%s] 设备包含[%d]个通道 \n",m_strIpAddr,deviceInfo.byChanNum);
        }
    }
 
    /**
     * 停止侦听智能事件
     */
    public void DetachEventRealLoadPic() {
        if (m_attachHandle.longValue() != 0) {
            netsdk.CLIENT_StopLoadPic(m_attachHandle);
            System.out.println("CLIENT_StopLoadPic Success");
        }
    }
 
    /**
     * 退出
     */
    public void logOut() {
        //判断是否已登录
        if (loginHandle.longValue() != 0) {
            netsdk.CLIENT_Logout(loginHandle);
            System.out.println("LogOut Success");
        }
    }
 
    /**
     * 清理sdk环境并退出
     */
    public static void cleanAndExit() {
        //判断log是否打开
        if (bLogOpen) {
            // 关闭sdk日志打印
            netsdk.CLIENT_LogClose();
        }
        // 进程关闭时,调用一次
        netsdk.CLIENT_Cleanup();
        System.exit(0);
    }
 
    /**
     * 初始化测试
     */
    public void initTest() {
        initSdk();
 
        this.loginWithHighLevel();
    }
 
    /**
     * 结束测试
     */
    public void endTest() {
        System.out.println("End Test");
        this.logOut(); // 登出设备
        System.out.println("See You...");
        cleanAndExit(); // 清理资源并退出
    }
 
    public void runTest()
    {
        System.out.println("Run Test");
        CaseMenu menu = new CaseMenu();
        menu.addItem(new CaseMenu.Item(this, "获取视频OSD国标模式配置", "getVideoWidgetConfig"));
        menu.addItem(new CaseMenu.Item(this, "设置视频OSD国标模式配置", "setVideoWidgetConfig"));
        menu.run();
    }
 
    public void getVideoWidgetConfig() {
        NET_CFG_GBMODE_INFO stuCfg = new NET_CFG_GBMODE_INFO();
        IntByReference nReturnLen = new IntByReference(0);
        stuCfg.write();
        //获取配置
        if (!netsdk.CLIENT_GetConfig(loginHandle, NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_GBMODE, 0,
                stuCfg.getPointer(), stuCfg.size(), 5000, null)) {
            System.out.println("CLIENT_GetConfig NET_EM_CFG_ACCESS_DISPLAY Config Failed!" + ToolKits.getErrorCode());
        } else {
            stuCfg.read();
            System.out.println("-----------------------Get Config-----------------------");
            System.out.println("bEncodeBlend:" + stuCfg.bEncodeBlend);
            System.out.println("bPreviewBlend:" + stuCfg.bPreviewBlend);
            System.out.println("nCustomTitleNum:" + stuCfg.nCustomTitleNum);
            System.out.println("nRowSpacing:" + stuCfg.nRowSpacing);
            System.out.println("nMargin:" + stuCfg.nMargin);
            System.out.println("stuChannelTitle:" + stuCfg.stuChannelTitle.emPositon);
            System.out.println("nTextAlign:" + stuCfg.stuChannelTitle.nTextAlign);
            for(int i = 0;i < stuCfg.nCustomTitleNum;i++){
                try {
                    System.out.println("szPositon:" + new String(stuCfg.stuCustomTitle[i].szPositon,encode));
                }catch (Exception e){
                    System.out.println(e.getMessage());
                }
                System.out.println("nOSDNum:" + stuCfg.stuCustomTitle[i].nOSDNum);
                for(int j = 0;j < stuCfg.stuCustomTitle[i].nOSDNum;j++){
                    try {
                        System.out.println("nEnable:" + stuCfg.stuCustomTitle[i].stuOSD[j].nEnable);
                        System.out.println("szText:" + new String(stuCfg.stuCustomTitle[i].stuOSD[j].szText,encode));
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                }
            }
        }
 
        stuCfg.write();
    }
 
    public void setVideoWidgetConfig() {
        NET_CFG_GBMODE_INFO stuCfg = new NET_CFG_GBMODE_INFO();
        IntByReference nReturnLen = new IntByReference(0);
        stuCfg.write();
        //获取配置
        if (!netsdk.CLIENT_GetConfig(loginHandle, NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_GBMODE, 0,
                stuCfg.getPointer(), stuCfg.size(), 5000, null)) {
            System.out.println("CLIENT_GetConfig NET_EM_CFG_ACCESS_DISPLAY Config Failed!" + ToolKits.getErrorCode());
        } else {
            stuCfg.read();
            System.out.println("-----------------------Get Config-----------------------");
            System.out.println("bEncodeBlend:" + stuCfg.bEncodeBlend);
            stuCfg.bEncodeBlend = 1;
            System.out.println("bPreviewBlend:" + stuCfg.bPreviewBlend);
            stuCfg.bPreviewBlend = 1;
            System.out.println("nCustomTitleNum:" + stuCfg.nCustomTitleNum);
            stuCfg.nCustomTitleNum = 1;
            System.out.println("nRowSpacing:" + stuCfg.nRowSpacing);
            stuCfg.nRowSpacing = 2;
            System.out.println("nMargin:" + stuCfg.nMargin);
            stuCfg.nMargin = 1;
            System.out.println("stuChannelTitle:" + stuCfg.stuChannelTitle.emPositon);
            stuCfg.stuChannelTitle.emPositon = 3;
            System.out.println("nTextAlign:" + stuCfg.stuChannelTitle.nTextAlign);
            stuCfg.stuChannelTitle.nTextAlign = 1;
            for(int i = 0;i < stuCfg.nCustomTitleNum;i++){
                System.out.println("nOSDNum:" + stuCfg.stuCustomTitle[i].nOSDNum);
                stuCfg.stuCustomTitle[i].nOSDNum = 1;
                try {
                    byte[] chars = "LeftDown".getBytes(encode);
                    stuCfg.stuCustomTitle[i].szPositon = new byte[256];//先清除一下该字段内容
                    System.arraycopy(chars, 0, stuCfg.stuCustomTitle[i].szPositon, 0,
                            chars.length);
                }catch (Exception e){
                    System.out.println(e.getMessage());
                }
                for(int j = 0;j < stuCfg.stuCustomTitle[i].nOSDNum;j++){
                    stuCfg.stuCustomTitle[i].stuOSD[j].nEnable = 1;
                    try {
                        byte[] chars = "IPC".getBytes(encode);
                        stuCfg.stuCustomTitle[i].stuOSD[j].szText = new byte[1024];//先清除一下该字段内容
                        System.arraycopy(chars, 0, stuCfg.stuCustomTitle[i].stuOSD[j].szText, 0,
                                chars.length);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                }
            }
        }
 
        stuCfg.write();
        //设置配置
        if (!netsdk.CLIENT_SetConfig(loginHandle, NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_GBMODE, 0,
                stuCfg.getPointer(), stuCfg.size(), 5000, nReturnLen, null)) {
            System.out.println("CLIENT_SetConfig NET_EM_CFG_ACCESS_DISPLAY Config Failed!" + ToolKits.getErrorCode());
        } else {
            System.out.println("CLIENT_SetConfig NET_EM_CFG_ACCESS_DISPLAY Config Succeed!");
        }
    }
 
 
 
    // 配置登陆地址,端口,用户名,密码
    private String m_strIpAddr = "172.8.101.167";
    private int m_nPort = 37777;
    private String m_strUser = "admin";
    private String m_strPassword = "admin123";
 
    public static void main(String[] args) {
        VideoWidgetDemo demo=new VideoWidgetDemo();
        demo.initTest();
        demo.runTest();
        demo.endTest();
    }
}