java怎么调用海康威视/Streaming/channels/1/picture的接口
时间: 2023-11-12 13:04:32 浏览: 240
调用海康威视的接口需要使用海康威视提供的SDK,以下是调用海康威视接口获取通道1图片的Java代码示例:
```java
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIFunctionMapper;
import com.sun.jna.win32.W32APITypeMapper;
import java.util.HashMap;
import java.util.Map;
public interface HCNetSDK extends StdCallLibrary {
HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary("HCNetSDK", HCNetSDK.class, new HashMap<String, Object>() {
{
put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
}
});
boolean NET_DVR_Init();
NativeLong NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, NET_DVR_DEVICEINFO_V30.ByReference lpDeviceInfo);
boolean NET_DVR_Logout(NativeLong lUserID);
boolean NET_DVR_GetLastError();
NativeLong NET_DVR_RealPlay_V40(NativeLong lUserID, NET_DVR_PREVIEWINFO lpPreviewInfo, RealDataCallBack_V30 fRealDataCallBack_V30, Pointer pUser);
boolean NET_DVR_StopRealPlay(NativeLong lRealHandle);
boolean NET_DVR_CaptureJPEGPicture_NEW(NativeLong lUserID, NativeLong lChannel, NET_DVR_JPEGPARA lpJpegPara, String sPicFileName);
interface RealDataCallBack_V30 extends StdCallCallback {
void invoke(NativeLong lRealHandle, int dwDataType, ByteByReference pBuffer, int dwBufSize, Pointer pUser);
}
class NET_DVR_DEVICEINFO_V30 extends Structure {
public byte[] sSerialNumber = new byte[SERIALNO_LEN];
public byte byAlarmInPortNum;
public byte byAlarmOutPortNum;
public byte byDiskNum;
public byte byDVRType;
public byte byChanNum;
public byte byStartChan;
public byte byAudioChanNum;
public byte byIPChanNum;
public byte[] byRes1 = new byte[24];
public byte byAbility;
public byte byRes2;
public short wDevType;
public byte[] byRes3 = new byte[2];
public static class ByReference extends NET_DVR_DEVICEINFO_V30 implements Structure.ByReference {
}
}
class NET_DVR_PREVIEWINFO extends Structure {
public NativeLong lChannel;
public NativeLong dwStreamType;
public NativeLong dwLinkMode;
public Pointer hPlayWnd;
public NativeLong bBlocked;
public NativeLong bPassbackRecord;
public NativeLongByReference bPreviewMode;
public NativeLong byProtoType;
public byte[] byRes = new byte[215];
}
class NET_DVR_JPEGPARA extends Structure {
public int wPicSize;
public int wPicQuality;
}
int SERIALNO_LEN = 48;
}
```
```java
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.PointerByReference;
import java.io.File;
public class HikvisionDemo {
public static void main(String[] args) {
// 初始化
HCNetSDK.NET_DVR_Init();
// 登录设备
HCNetSDK.NET_DVR_DEVICEINFO_V30.ByReference deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30.ByReference();
NativeLong lUserID = HCNetSDK.INSTANCE.NET_DVR_Login_V30("10.0.0.64", (short) 8000, "admin", "password", deviceInfo);
if (lUserID.longValue() == -1) {
System.out.println("登录失败,错误码:" + HCNetSDK.INSTANCE.NET_DVR_GetLastError());
return;
}
// 设置预览参数
HCNetSDK.NET_DVR_PREVIEWINFO previewInfo = new HCNetSDK.NET_DVR_PREVIEWINFO();
previewInfo.lChannel = new NativeLong(1);
previewInfo.dwStreamType = new NativeLong(0);
previewInfo.dwLinkMode = new NativeLong(0x00000000);
previewInfo.hPlayWnd = null;
previewInfo.bBlocked = new NativeLong(1);
previewInfo.bPassbackRecord = new NativeLong(0);
previewInfo.bPreviewMode = new NativeLongByReference(new NativeLong(0));
previewInfo.byProtoType = new NativeLong(0);
previewInfo.byRes = new byte[215];
// 开始预览
HCNetSDK.RealDataCallBack_V30 realDataCallBack_V30 = new HCNetSDK.RealDataCallBack_V30() {
@Override
public void invoke(NativeLong lRealHandle, int dwDataType, ByteByReference pBuffer, int dwBufSize, Pointer pUser) {
switch (dwDataType) {
case HCNetSDK.NET_DVR_SYSHEAD:
break;
case HCNetSDK.NET_DVR_STREAMDATA:
System.out.println("收到视频数据,长度:" + dwBufSize);
break;
}
}
};
NativeLong lRealHandle = HCNetSDK.INSTANCE.NET_DVR_RealPlay_V40(lUserID, previewInfo, realDataCallBack_V30, null);
// 抓图
HCNetSDK.NET_DVR_JPEGPARA jpegPara = new HCNetSDK.NET_DVR_JPEGPARA();
jpegPara.wPicSize = 0;
jpegPara.wPicQuality = 100;
String filePath = "C:\\temp\\test.jpg";
boolean result = HCNetSDK.INSTANCE.NET_DVR_CaptureJPEGPicture_NEW(lUserID, new NativeLong(1), jpegPara, filePath);
if (result) {
System.out.println("抓图成功,文件路径:" + filePath);
} else {
System.out.println("抓图失败,错误码:" + HCNetSDK.INSTANCE.NET_DVR_GetLastError());
}
// 停止预览
HCNetSDK.INSTANCE.NET_DVR_StopRealPlay(lRealHandle);
// 注销登录
HCNetSDK.INSTANCE.NET_DVR_Logout(lUserID);
// 释放SDK资源
HCNetSDK.INSTANCE.NET_DVR_Cleanup();
}
}
```
其中,需要修改的部分为:
- `NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, NET_DVR_DEVICEINFO_V30.ByReference lpDeviceInfo)` 方法中的 `sDVRIP`、`wDVRPort`、`sUserName` 和 `sPassword` 参数,分别为设备IP地址、端口号、用户名和密码。
- `NET_DVR_RealPlay_V40(NativeLong lUserID, NET_DVR_PREVIEWINFO lpPreviewInfo, RealDataCallBack_V30 fRealDataCallBack_V30, Pointer pUser)` 方法中的 `lUserID` 和 `lpPreviewInfo.lChannel` 参数,分别为登录设备返回的用户ID和通道号。
- `NET_DVR_CaptureJPEGPicture_NEW(NativeLong lUserID, NativeLong lChannel, NET_DVR_JPEGPARA lpJpegPara, String sPicFileName)` 方法中的 `lUserID` 和 `lChannel` 参数,分别为登录设备返回的用户ID和通道号;`sPicFileName` 参数为图片保存路径。
需要注意的是,调用海康威视的接口需要使用海康威视提供的SDK,并且需要将SDK对应的dll文件放在Java的classpath路径下。另外,示例中的代码需要使用JNA库进行调用。
阅读全文