环境准备
1. 开发工具:eclipse
2. Jdk版本:jdk1.8
3. 开发语言:java,界面使用swing开发
4. 摄像头:
5. 海康威视SDK下载地址:下载地址
6. SDK版本:CH-HCNetSDKV6.0.2.35_build20190411_Win64
代码实现
使用海康威视SDK,生成监控摄像头预览功能。
1. 实现流程
> SDK初始化->用户注册->获取通道号->开始播放->开启声音。
2. 项目结构
3. SDK目录
4. 实现代码
- 启动类
package com.kx.hcws;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import com.kx.hcws.sdk.HCNetSDK;
import com.kx.hcws.sdk.HCNetSDKManger;
import com.kx.hcws.ui.Player;
public class PreView {
// private static String ip;
// private static String username;
// private static String password;
public static void main(String[] args) {
// 确保一个漂亮的外观风格
JFrame.setDefaultLookAndFeelDecorated(true);
JPopupMenu.setDefaultLightWeightPopupEnabled(false);// 防止被播放窗口(AWT组件)覆盖
// 显示应用 GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// 初始化海康播放器
HCNetSDKManger.init();
HCNetSDK.INSTANCE.NET_DVR_Init();
HCNetSDK.INSTANCE.NET_DVR_SetConnectTime(5000, 5);
HCNetSDK.INSTANCE.NET_DVR_SetReconnect(1000, true);
/*
* IP地址:摄像头IP地址。 用户名:摄像头登录用户名。 密码:摄像头验证码。 摄像头编号:可以自定义。
*/
new Player("192.168.2.18", "admin", "axjy123456", 8000, 1L);
}
});
}
}
- 播放组件
package com.kx.hcws.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.kx.hcws.sdk.HCNetSDK;
import com.kx.hcws.sdk.HCNetSDK.FExceptionCallBack;
import com.kx.hcws.sdk.HCNetSDKManger;
import com.kx.hcws.sdk.PlayCtrl;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.examples.win32.W32API.HWND;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.NativeLongByReference;
public class HcPlayerPanel extends JPanel {
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(HcPlayerPanel.class);
/**
*
*/
private static final long serialVersionUID = 1L;
// 播放控制
private static PlayCtrl playControl = PlayCtrl.INSTANCE;
private String m_sDeviceIP;
private String username;
private String password;
private int port;
private Long deviceId = 1L;
private long iChannelNum;
// 用户参数
private HCNetSDK.NET_DVR_CLIENTINFO m_strClientInfo;
// 预览句柄
private NativeLong lPreviewHandle = new NativeLong(-1);
// 播放界面
public java.awt.Panel panelRealplay = new java.awt.Panel();
public javax.swing.JPanel jPanelRealplayArea = new javax.swing.JPanel();
// 设备指针
private NativeLongByReference devicePoint;
// 播放端口
private NativeLongByReference m_lPort = new NativeLongByReference(new NativeLong(-1));
// 异常回调
private FExceptionCallBack exceptionCallBack = new FExceptionCallBack() {
@Override
public void invoke(int dwType, NativeLong lUserID, NativeLong lHandle, Pointer pUser) {
NativeLong deviceid = pUser.getNativeLong(0);
System.out.println("预览异常:dwType=" + dwType + ",lUserID=" + lUserID + ",lHandle=" + lHandle + ",pUser="
+ deviceid.intValue());
// EXCEPTION_EXCHANGE = 0x8000;// 用户交互时异常
// EXCEPTION_AUDIOEXCHANGE = 0x8001;//语音对讲异常
// EXCEPTION_ALARM = 0x8002;// 报警异常
// EXCEPTION_PREVIEW = 0x8003;// 网络预览异常
// EXCEPTION_SERIAL = 0x8004;// 透明通道异常
// EXCEPTION_RECONNECT = 0x8005; // 预览时重连
// EXCEPTION_ALARMRECONNECT = 0x8006;//报警时重连
// EXCEPTION_SERIALRECONNECT = 0x8007;//透明通道重连
// EXCEPTION_PLAYBACK = 0x8010;// 回放异常
// EXCEPTION_DISKFMT = 0x8011;// 硬盘格式化
if (dwType == HCNetSDK.EXCEPTION_ALARM || dwType == HCNetSDK.EXCEPTION_RECONNECT
|| dwType == HCNetSDK.EXCEPTION_ALARMRECONNECT || dwType == HCNetSDK.EXCEPTION_SERIALRECONNECT
|| dwType == HCNetSDK.EXCEPTION_PLAYBACK || dwType == 32971 || dwType == 32776 || dwType == 32789
|| dwType == 32790 || dwType == 32791 || dwType == 32776 || dwType == 32793 || dwType == 32800
|| dwType == 32804 || dwType == 32805 || dwType == 32832 || dwType == 32833 || dwType == 32834) {
return;
}
}
};
public HcPlayerPanel(String m_sDeviceIP, String username, String password, int port, Long deviceId) {
this.m_sDeviceIP = m_sDeviceIP;
this.username = username;
this.password = password;
this.port = port;
this.deviceId = deviceId;
init();
}
private void init() {
panelRealplay.setBackground(Color.BLACK);
jPanelRealplayArea.setBackground(Color.BLACK);
javax.swing.GroupLayout panelRealplayLayout = new javax.swing.GroupLayout(panelRealplay);
panelRealplay.setLayout(panelRealplayLayout);
javax.swing.GroupLayout jPanelRealplayAreaLayout = new javax.swing.GroupLayout(jPanelRealplayArea);
jPanelRealplayArea.setLayout(jPanelRealplayAreaLayout);
jPanelRealplayAreaLayout.setHorizontalGroup(
jPanelRealplayAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
panelRealplay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
jPanelRealplayAreaLayout.setVerticalGroup(jPanelRealplayAreaLayout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(panelRealplay,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
devicePoint = new NativeLongByReference(new NativeLong(deviceId));// 设备编号
// 注册异常信息
HCNetSDKManger.hCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, exceptionCallBack, devicePoint.getPointer());
}
public void play() {
NativeLong lUserID = new NativeLong(-1);// 用户句柄
if (lUserID.longValue() > -1) {// 不注销
// 先注销
HCNetSDKManger.hCNetSDK.NET_DVR_Logout_V30(lUserID);
lUserID = new NativeLong(-1);
}
// 注册
HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
lUserID = HCNetSDKManger.hCNetSDK.NET_DVR_Login_V30(m_sDeviceIP, (short) port, username, password,
m_strDeviceInfo);
long userID = lUserID.longValue();
if (userID == -1) {
System.out.println(HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError());
System.out.println("设备注册失败:" + m_sDeviceIP + "--" + username + "--" + password + "--" + port);
JOptionPane.showMessageDialog(this, "注册失败");
return;
} else {
// CreateDeviceTree();
}
if (lUserID == null || lUserID.intValue() == -1) {
logger.info("注册失败");
return;
}
panelRealplay.setPreferredSize(new Dimension(getWidth(), getHeight()));
removeAll();
add(jPanelRealplayArea, BorderLayout.CENTER);
revalidate();
// 获取窗口句柄
HWND hwnd = new HWND(Native.getComponentPointer(panelRealplay));
// 获取通道号
IntByReference ibrBytesReturned = new IntByReference(0);// 获取IP接入配置参数
boolean bRet = false;
HCNetSDK.NET_DVR_IPPARACFG m_strIpparaCfg = new HCNetSDK.NET_DVR_IPPARACFG();
m_strIpparaCfg.write();
Pointer lpIpParaConfig = m_strIpparaCfg.getPointer();
bRet = HCNetSDKManger.hCNetSDK.NET_DVR_GetDVRConfig(lUserID, HCNetSDK.NET_DVR_GET_IPPARACFG, new NativeLong(0),
lpIpParaConfig, m_strIpparaCfg.size(), ibrBytesReturned);
m_strIpparaCfg.read();
// 设备支持IP通道
String sChannelName = "";
if (!bRet) {
// 设备不支持,则表示没有IP通道
for (int iChannum = 0; iChannum < m_strDeviceInfo.byChanNum; iChannum++) {
sChannelName = "Camera" + (iChannum + m_strDeviceInfo.byStartChan);
}
} else {
// 设备支持IP通道
for (int iChannum = 0; iChannum < m_strDeviceInfo.byChanNum; iChannum++) {
if (m_strIpparaCfg.byAnalogChanEnable[iChannum] == 1) {
sChannelName = "Camera" + (iChannum + m_strDeviceInfo.byStartChan);
}
}
for (int iChannum = 0; iChannum < HCNetSDK.MAX_IP_CHANNEL; iChannum++)
if (m_strIpparaCfg.struIPChanInfo[iChannum].byEnable == 1) {
sChannelName = "IPCamera" + (iChannum + m_strDeviceInfo.byStartChan);
}
}
iChannelNum = -1;
if (sChannelName.charAt(0) == 'C') {// Camara开头表示模拟通道
// 子字符串中获取通道号
iChannelNum = Integer.parseInt(sChannelName.substring(6));
} else {
if (sChannelName.charAt(0) == 'I') {// IPCamara开头表示IP通道,子字符创中获取通道号,IP通道号要加32
iChannelNum = Integer.parseInt(sChannelName.substring(8)) + 32;
} else {
logger.info("通道号获取失败");
return;
}
}
if (iChannelNum == -1) {
logger.info("请选择要预览的通道");
return;
}
m_strClientInfo = new HCNetSDK.NET_DVR_CLIENTINFO();
m_strClientInfo.lChannel = new NativeLong(iChannelNum);
m_strClientInfo.hPlayWnd = hwnd;
lPreviewHandle = HCNetSDKManger.hCNetSDK.NET_DVR_RealPlay_V30(lUserID, m_strClientInfo, null, null, true);
long previewSucValue = lPreviewHandle.longValue();
// 预览失败时:
if (previewSucValue == -1) {
System.out.println("预览失败:" + HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError());
return;
}
System.out.println("预览成功!");
validate();
}
public void stop() {
logger.info("设备" + deviceId + "停止播放===================");
if (lPreviewHandle != null && lPreviewHandle.longValue() != -1) {
if (!HCNetSDKManger.hCNetSDK.NET_DVR_StopRealPlay(lPreviewHandle)) {
logger.info("设备" + deviceId + "停止播放失败");
}
if (m_lPort.getValue().intValue() != -1) {
if (!playControl.PlayM4_Stop(m_lPort.getValue())) {
logger.info("设备" + deviceId + "停止播放失败");
}
m_lPort.setValue(new NativeLong(-1));
}
}
panelRealplay.repaint();
}
}
- 播放窗体
package com.kx.hcws.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class Player extends JFrame implements WindowListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private HcPlayerPanel playerPanel;
public Player(String m_sDeviceIP, String username, String password, int port, Long deviceId) {
setSize(1000, 600);
setResizable(true);
setTitle("设备预览");
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
playerPanel = new HcPlayerPanel(m_sDeviceIP, username, password, port, deviceId);
playerPanel.setPreferredSize(new Dimension(1000, 600));
getContentPane().add(playerPanel, BorderLayout.CENTER);
setVisible(true);
new Thread(new Runnable() {
@Override
public void run() {
playerPanel.play();
}
}).start();
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
playerPanel.stop();
}
@Override
public void windowDeactivated(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowOpened(WindowEvent e) {
}
}
5. 总结
1.海康威视DDL文件按照说明必须全部拷贝到对应文件夹。
2.java.lang.UnsatisfiedLinkError: Unable to load library 'HCNetSDK': ÕҲ»µ½ָ¶¨问题
处理办法:SDK文件路径不正确, 需要放在项目根目录下,路径改为全路径或者使用代码组装完整路径。