Home  >  Article  >  Java  >  Use Java Hikvision SDK secondary development to build a powerful video surveillance system

Use Java Hikvision SDK secondary development to build a powerful video surveillance system

WBOY
WBOYOriginal
2023-09-06 12:37:45984browse

Use Java Hikvision SDK secondary development to build a powerful video surveillance system

Use Java Hikvision SDK for secondary development to build a powerful video surveillance system

Abstract:
This article will introduce how to use Java Hikvision SDK for secondary development , build a powerful video surveillance system. Through the rich interfaces and functions provided by Hikvision SDK, we can easily implement video recording, real-time preview, remote control and other functions, and add customized business logic. This article will introduce in detail how to use Java Hikvision SDK for secondary development, and provide some sample code to help readers better understand.

Part One: Environment Preparation
Before starting, we need to make the following preparations:

  1. Download and install Java JDK.
  2. Download and install IDE, such as Eclipse.
  3. Download and install Hikvision SDK.

Part 2: Project Creation and Configuration

  1. Create a Java project and import the jar package of Hikvision SDK.
  2. Configure the relevant parameters of Hikvision SDK, such as device IP, port number, user name, password, etc.
  3. Create a main class to initialize the SDK and perform related operations.

Part 3: Real-time preview function
Real-time preview is one of the most basic functions in the video surveillance system. The following is a sample code that demonstrates how to use Hikvision SDK for real-time preview:

import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.PointerByReference;

import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.NET_DVR_DEVICEINFO_V30;
import com.hikvision.netsdk.NET_DVR_PREVIEWINFO;

public class RealTimePreview {

    private static HCNetSDK hikSDK = HCNetSDK.INSTANCE;

    public static void main(String[] args) {
        // 初始化SDK
        hikSDK.NET_DVR_Init();

        // 登录设备
        NativeLong lUserID;
        NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
        lUserID = hikSDK.NET_DVR_Login_V30("192.168.1.100", (short) 8000, "admin", "password", deviceInfo);
        if (lUserID.intValue() == -1) {
            System.out.println("登录失败,错误码:" + hikSDK.NET_DVR_GetLastError());
            return;
        }

        // 设置预览参数
        NativeLong lRealPlayHandle;
        NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO();
        previewInfo.lChannel = 1;
        previewInfo.dwStreamType = 0;
        previewInfo.dwLinkMode = 0;
        previewInfo.hPlayWnd = null;

        // 开始预览
        lRealPlayHandle = hikSDK.NET_DVR_RealPlay_V40(lUserID, previewInfo, null);
        if (lRealPlayHandle.intValue() == -1) {
            System.out.println("预览失败,错误码:" + hikSDK.NET_DVR_GetLastError());
            return;
        }

        // 等待预览停止
        System.out.println("按任意键停止预览...");
        try {
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 停止预览
        if (!hikSDK.NET_DVR_StopRealPlay(lRealPlayHandle)) {
            System.out.println("停止预览失败,错误码:" + hikSDK.NET_DVR_GetLastError());
            return;
        }

        // 退出登录
        hikSDK.NET_DVR_Logout(lUserID);
        hikSDK.NET_DVR_Cleanup();
        System.out.println("退出成功");
    }
}

Part 4: Video recording function
In addition to real-time preview, the video surveillance system also needs to provide video recording function. The following is a sample code that demonstrates how to use Hikvision SDK for video recording:

import com.sun.jna.NativeLong;
import com.sun.jna.ptr.NativeLongByReference;

import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.NET_DVR_DEVICEINFO_V30;
import com.hikvision.netsdk.NET_DVR_PREVIEWINFO;
import com.hikvision.netsdk.NET_DVR_TIME;

public class VideoRecording {

    private static HCNetSDK hikSDK = HCNetSDK.INSTANCE;

    public static void main(String[] args) {
        // 初始化SDK
        hikSDK.NET_DVR_Init();

        // 登录设备
        NativeLong lUserID;
        NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
        lUserID = hikSDK.NET_DVR_Login_V30("192.168.1.100", (short) 8000, "admin", "password", deviceInfo);
        if (lUserID.intValue() == -1) {
            System.out.println("登录失败,错误码:" + hikSDK.NET_DVR_GetLastError());
            return;
        }

        // 设置预览参数
        NativeLong lRealPlayHandle;
        NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO();
        previewInfo.lChannel = 1;
        previewInfo.dwStreamType = 0;
        previewInfo.dwLinkMode = 0;
        previewInfo.hPlayWnd = null;

        // 开始预览
        lRealPlayHandle = hikSDK.NET_DVR_RealPlay_V40(lUserID, previewInfo, null);
        if (lRealPlayHandle.intValue() == -1) {
            System.out.println("预览失败,错误码:" + hikSDK.NET_DVR_GetLastError());
            return;
        }

        // 开始录像
        NativeLongByReference lRecordHandle = new NativeLongByReference();
        NET_DVR_TIME startTime = new NET_DVR_TIME();
        startTime.dwYear = 2022;
        startTime.dwMonth = 12;
        startTime.dwDay = 1;
        startTime.dwHour = 0;
        startTime.dwMinute = 0;
        startTime.dwSecond = 0;
        if (!hikSDK.NET_DVR_SetFileTime(0, startTime)) {
            System.out.println("设置录像时间失败,错误码:" + hikSDK.NET_DVR_GetLastError());
            return;
        }
        lRecordHandle.setValue(hikSDK.NET_DVR_RealPlay_V40(lUserID, previewInfo, null));
        if (lRecordHandle.intValue() == -1) {
            System.out.println("录像失败,错误码:" + hikSDK.NET_DVR_GetLastError());
            return;
        }

        // 等待录像停止
        System.out.println("按任意键停止录像...");
        try {
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 停止录像
        if (!hikSDK.NET_DVR_StopRealPlay(lRecordHandle.getValue())) {
            System.out.println("停止录像失败,错误码:" + hikSDK.NET_DVR_GetLastError());
            return;
        }

        // 退出登录
        hikSDK.NET_DVR_Logout(lUserID);
        hikSDK.NET_DVR_Cleanup();
        System.out.println("退出成功");
    }
}

Summary:
This article introduces how to use Java Hikvision SDK for secondary development to build a powerful video surveillance system. Through the interfaces and functions provided by Hikvision SDK, we can implement functions such as real-time preview and video recording, and add customized business logic. I hope this article can help readers learn and use Java Hikvision SDK.

The above is the detailed content of Use Java Hikvision SDK secondary development to build a powerful video surveillance system. 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