Home  >  Article  >  Java  >  Best practices in secondary development of Java Hikvision SDK

Best practices in secondary development of Java Hikvision SDK

王林
王林Original
2023-09-06 09:04:461119browse

Best practices in secondary development of Java Hikvision SDK

Best Practices in Secondary Development of Java Hikvision SDK

Abstract:
With the widespread application of modern monitoring technology, Hikvision provides SDK (Software Development Kit) has become the first choice for many developers. This article will introduce the best practices in secondary development of Java Hikvision SDK and provide some code examples.

  1. Connect the device
    Before using the Hikvision SDK, you first need to connect the device you want to operate. The following is a sample code for connecting a device:
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;

public class DeviceConnection {

  private static final int TIMEOUT = 5000; // 连接超时时间,单位为毫秒

  public static NativeLong login(String ip, int port, String username, String password) {
    HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
    NativeLong lUserID = HCNetSDK.NET_DVR_Login_V30(ip, port, username, password, deviceInfo);
    if (lUserID.longValue() == -1) {
      System.err.println("登录设备失败,错误码:" + HCNetSDK.NET_DVR_GetLastError());
    }
    return lUserID;
  }

  public static void logout(NativeLong lUserID) {
    if (!HCNetSDK.NET_DVR_Logout(lUserID)) {
      System.err.println("登出设备失败,错误码:" + HCNetSDK.NET_DVR_GetLastError());
    }
  }

  public static void main(String[] args) {
    String ip = "192.168.1.100";
    int port = 8000;
    String username = "admin";
    String password = "123456";

    NativeLong lUserID = login(ip, port, username, password);

    // do something...

    logout(lUserID);
  }
}
  1. Get device status
    Before performing other operations, we usually need to obtain the running status of the device. The following is a sample code to obtain device status:
import com.sun.jna.NativeLong;

public class DeviceStatus {

  public static boolean isDeviceOnline(NativeLong lUserID) {
    HCNetSDK.NET_DVR_WORKSTATE_V30 deviceStatus = new HCNetSDK.NET_DVR_WORKSTATE_V30();
    if (!HCNetSDK.NET_DVR_GetDVRWorkState_V30(lUserID, deviceStatus)) {
      System.err.println("获取设备状态失败,错误码:" + HCNetSDK.NET_DVR_GetLastError());
    }
    return deviceStatus.dwDeviceStatic == 1;
  }

  public static void main(String[] args) {
    NativeLong lUserID = new NativeLong(12345); // 假设已经登录并获取到了lUserID

    if (isDeviceOnline(lUserID)) {
      System.out.println("设备在线");
    } else {
      System.out.println("设备离线");
    }
  }
}
  1. Set device parameters
    Hikvision SDK provides a rich interface to set various parameters of the device. The following is a sample code for setting device time:
import com.sun.jna.NativeLong;

public class DeviceSetting {

  public static boolean setDeviceTime(NativeLong lUserID) {
    HCNetSDK.NET_DVR_TIME time = new HCNetSDK.NET_DVR_TIME();
    time.dwYear = 2022;
    time.dwMonth = 7;
    time.dwDay = 1;
    time.dwHour = 12;
    time.dwMinute = 0;
    time.dwSecond = 0;
    if (!HCNetSDK.NET_DVR_SetDVRConfig(lUserID, HCNetSDK.NET_DVR_SET_TIMECFG, 0, time.getPointer(),
        time.size(), null)) {
      System.err.println("设置设备时间失败,错误码:" + HCNetSDK.NET_DVR_GetLastError());
    }
    return true;
  }

  public static void main(String[] args) {
    NativeLong lUserID = new NativeLong(12345); // 假设已经登录并获取到了lUserID

    if (setDeviceTime(lUserID)) {
      System.out.println("设置设备时间成功");
    } else {
      System.out.println("设置设备时间失败");
    }
  }
}

Summary:
Through the above sample code, we have learned some best practices in the secondary development of Java Hikvision SDK, including device Connect, get device status and set device parameters. In actual development, we can further use the interface provided by Hikvision SDK to complete more complex functions according to project needs. I hope this article will be helpful to developers who are developing Java Hikvision SDK.

The above is the detailed content of Best practices in secondary development of Java Hikvision SDK. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn