接上一章:二、JAVA调用海康威视SDK实现摄像头预览完整版
本章实现摄像头本地存储功能。
代码:
PreView.java
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地址。 用户名:摄像头登录用户名。 密码:摄像头验证码。 摄像头编号:可以自定义。
*
* "192.168.2.18", "admin", "axjy123456", 8000, 1L
*/
new Player();
}
});
}
}
HcPlayerPanel.java
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 boolean isplay = false;
// 用户参数
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() {
init();
}
/**
* 开始,传入对应参数
*
* @param m_sDeviceIP
* @param username
* @param password
* @param port
* @param deviceId
*/
public void start(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;
}
/**
* 初始化组件
*/
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));// 设备编号
panelRealplay.setPreferredSize(new Dimension(getWidth(), getHeight()));
// 注册异常信息
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();
isplay = true;
}
/**
* 停止播放
*/
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();
isplay = false;
}
public boolean isIsplay() {
return isplay;
}
/**
* 实现截图
*/
public void snapshot(String filepath) {
if (!HCNetSDKManger.hCNetSDK.NET_DVR_CapturePicture(lPreviewHandle, filepath)) {
JOptionPane.showMessageDialog(this, "设备截屏失败:" + HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError());
System.out.println("设备截屏失败:" + HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError());
} else {
JOptionPane.showMessageDialog(this, "截图成功!");
}
}
public void save(String file) {
}
}
Player.java
package com.kx.hcws.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.kx.hcws.HCSaveProcess;
public class Player extends JFrame implements WindowListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private HcPlayerPanel playerPanel;
private JTextField ipField;
private JTextField usernameField;
private JPasswordField pwdField;
private HCSaveProcess process;
public Player() {
setSize(1200, 600);
setResizable(true);
setTitle("设备预览");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭窗口时关闭系统
getContentPane().setLayout(new BorderLayout());
playerPanel = new HcPlayerPanel();
playerPanel.setBackground(Color.BLACK);
playerPanel.setPreferredSize(new Dimension(1000, 600));
getContentPane().add(playerPanel, BorderLayout.CENTER);
initCtrlPanel();
setVisible(true);
}
private void initCtrlPanel() {
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
{// IP
JLabel jLabel = new JLabel("IP:");
jLabel.setPreferredSize(new Dimension(60, 30));
jLabel.setHorizontalTextPosition(JLabel.CENTER);
jLabel.setVerticalAlignment(JLabel.CENTER);
content.add(jLabel);
ipField = new JTextField();
ipField.setPreferredSize(new Dimension(130, 30));
content.add(ipField);
}
{// 用户名
JLabel jLabel = new JLabel("用户名:");
jLabel.setPreferredSize(new Dimension(60, 30));
jLabel.setHorizontalTextPosition(JLabel.CENTER);
jLabel.setVerticalAlignment(JLabel.CENTER);
content.add(jLabel);
usernameField = new JTextField("admin");
usernameField.setPreferredSize(new Dimension(130, 30));
content.add(usernameField);
}
{// 密码
JLabel jLabel = new JLabel("密码:");
jLabel.setPreferredSize(new Dimension(60, 30));
jLabel.setHorizontalTextPosition(JLabel.CENTER);
jLabel.setVerticalAlignment(JLabel.CENTER);
content.add(jLabel);
pwdField = new JPasswordField("");
pwdField.setPreferredSize(new Dimension(130, 30));
content.add(pwdField);
}
{// 按钮
JButton playBtn = new JButton("播放");
playBtn.setPreferredSize(new Dimension(150, 30));
playBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String m_sDeviceIP = ipField.getText();
if (m_sDeviceIP == null || "".equals(m_sDeviceIP)) {
JOptionPane.showMessageDialog(Player.this, "IP不能为空");
return;
}
String username = usernameField.getText();
if (username == null || "".equals(username)) {
JOptionPane.showMessageDialog(Player.this, "用户名不能为空");
return;
}
String password = pwdField.getText();
if (password == null || "".equals(password)) {
JOptionPane.showMessageDialog(Player.this, "密码不能为空");
return;
}
if (playerPanel.isIsplay()) {// 先停止
playerPanel.stop();
}
playerPanel.start(m_sDeviceIP, username, password, 8000, 1L);
new Thread(new Runnable() {
@Override
public void run() {
playerPanel.play();
}
}).start();
}
});
content.add(playBtn);
JButton imgBtn = new JButton("截图");
imgBtn.setPreferredSize(new Dimension(150, 30));
imgBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!playerPanel.isIsplay()) {// 先停止
JOptionPane.showMessageDialog(Player.this, "请先预览!");
return;
}
JFileChooser filechooser = new JFileChooser();// 创建文件选择器
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = filechooser.showOpenDialog(Player.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String file = filechooser.getSelectedFile().getAbsolutePath() + "\\" + new Date().getTime()
+ ".jpg";
playerPanel.snapshot(file);
}
}
});
content.add(imgBtn);
JButton saveBtn = new JButton("存储");
saveBtn.setPreferredSize(new Dimension(150, 30));
saveBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String m_sDeviceIP = ipField.getText();
if (m_sDeviceIP == null || "".equals(m_sDeviceIP)) {
JOptionPane.showMessageDialog(Player.this, "IP不能为空");
return;
}
String username = usernameField.getText();
if (username == null || "".equals(username)) {
JOptionPane.showMessageDialog(Player.this, "用户名不能为空");
return;
}
String password = pwdField.getText();
if (password == null || "".equals(password)) {
JOptionPane.showMessageDialog(Player.this, "密码不能为空");
return;
}
if (!playerPanel.isIsplay()) {// 先停止
JOptionPane.showMessageDialog(Player.this, "请先预览!");
return;
}
JFileChooser filechooser = new JFileChooser();// 创建文件选择器
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = filechooser.showOpenDialog(Player.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String file = filechooser.getSelectedFile().getAbsolutePath();
if (process == null) {
process = new HCSaveProcess(m_sDeviceIP, username, password, 8000, 1L);
}
process.start(file);
}
}
});
content.add(saveBtn);
}
content.setPreferredSize(new Dimension(200, 600));
getContentPane().add(content, BorderLayout.WEST);
}
@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) {
}
}
HCSaveProcess.java
package com.kx.hcws;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import com.kx.hcws.sdk.HCNetSDK;
import com.kx.hcws.sdk.HCNetSDK.FExceptionCallBack;
import com.kx.hcws.sdk.HCNetSDKManger;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.NativeLongByReference;
/**
* 存储处理器
*
* @author kxy
*
*/
public class HCSaveProcess {
// 日志
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(HCSaveProcess.class);
// 是否正在存储
protected boolean issaveing = false;
// 设备指针
protected NativeLongByReference devicePoint;
// 最大异常次数
private static int ERROR_MAX_TRY_COUNT = 3;
// 用户参数
private HCNetSDK.NET_DVR_CLIENTINFO m_strClientInfo;
// 预览指针
private NativeLong lRealHandle = new NativeLong(-1);
// 异常次数
private int errorcout = 0;
// 是否正在处理中
private boolean isloadding = false;
// 存储文件名称
private String filename;
private Long deviceId;
private String m_sDeviceIP;
private String username;
private String password;
private int port;
private long iChannelNum;
/**
* 异常回调
*/
private static 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() + ",device=" + deviceid);
// 0x8000 32768 用户交互时异常(注册心跳超时,心跳间隔为2分钟)
// 0x8001 32769 语音对讲异常
// 0x8002 32770 报警异常
// 0x8003 32771 网络预览异常
// 0x8004 32772 透明通道异常
// 0x8005 32773 预览时重连
// 0x8006 32774 报警时重连
// 0x8007 32775 透明通道重连
// 0x8008 32776 透明通道重连成功
// 0x8010 32784 回放异常
// 0x8011 32785 硬盘格式化
// 0x8012 32786 被动解码异常
// 0x8013 32787 邮件测试异常
// 0x8014 32788 备份异常
// 0x8015 32789 预览时重连成功
// 0x8016 32790 报警时重连成功
// 0x8017 32791 用户交互恢复
// 0x8018 32792 网络流量检测异常
// 0x8019 32793 图片预览重连
// 0x8020 32800 图片预览重连成功
// 0x8021 32801 图片预览异常
// 0x8022 32802 报警信息缓存已达上限
// 0x8023 32803 报警丢失
// 0x8024 32804 被动转码重连
// 0x8025 32805 被动转码重连成功
// 0x8026 32806 被动转码异常
// 0x8040 32832 用户重登陆
// 0x8041 32833 用户重登陆成功
// 0x8042 32834 被动解码重连
// 0x8043 32835 集群报警异常
// 0x8044 32836 重登陆失败,停止重登陆
// 0x8045 32837 关闭预览重连功能
// 0x8046 32838 关闭报警重连功能
// 0x8047 32839 关闭透明通道重连功能
// 0x8048 32840 关闭回显重连功能
// 0x8049 32841 关闭被动解码重连功能
// 0x804a 32842 关闭被动转码重连功能
// 32769 //语音对讲异常 || 32770 //报警异常|| 32772 //透明通道异常|| 32773 预览时重连||
// 32774 报警时重连|| 32775 透明通道重连
// || 32776 透明通道重连成功|| 32784 回放异常|| 32785 硬盘格式化|| 32786 被动解码异常||
// 32787 邮件测试异常|| 32788 备份异常
// || 32789 预览时重连成功|| 32790 报警时重连成功|| 32791 用户交互恢复|| 32792
// 网络流量检测异常|| 32793 图片预览重连|| 32800 图片预览重连成功||
// || 32801 图片预览异常|| 32802 报警信息缓存已达上限|| 32804 被动转码重连|| 32805
// 被动转码重连成功|| 32806 被动转码异常|| 32832 用户重登陆
// || 32833 用户重登陆成功|| 32834 被动解码重连|| 32835 集群报警异常|| 32836
// 重登陆失败,停止重登陆|| 32837 关闭预览重连功能|| 32838 关闭报警重连功能
// || 32839 关闭透明通道重连功能|| 32840 关闭回显重连功能|| 32841 关闭被动解码重连功能|| 32842
// 关闭被动转码重连功能
if (dwType == 32769// 语音对讲异常
|| dwType == 32770// 报警异常
|| dwType == 32772// 透明通道异常
|| dwType == 32773// 预览时重连
|| dwType == 32774// 报警时重连
|| dwType == 32775// 透明通道重连
|| dwType == 32776// 透明通道重连成功
|| dwType == 32784// 回放异常
|| dwType == 32785// 硬盘格式化
|| dwType == 32786// 被动解码异常
|| dwType == 32787// 邮件测试异常
|| dwType == 32788// 备份异常
|| dwType == 32789// 预览时重连成功
|| dwType == 32790// 报警时重连成功
|| dwType == 32791// 用户交互恢复
|| dwType == 32792// 网络流量检测异常
|| dwType == 32793// 图片预览重连
|| dwType == 32800// 图片预览重连成功
|| dwType == 32801// 图片预览异常
|| dwType == 32802// 报警信息缓存已达上限
|| dwType == 32804// 被动转码重连
|| dwType == 32805// 被动转码重连成功
|| dwType == 32806// 被动转码异常
|| dwType == 32832// 用户重登陆
|| dwType == 32833// 用户重登陆成功
|| dwType == 32834// 被动解码重连
|| dwType == 32835// 集群报警异常
|| dwType == 32837// 关闭预览重连功能
|| dwType == 32838// 关闭报警重连功能
|| dwType == 32839// 关闭透明通道重连功能
|| dwType == 32840// 关闭回显重连功能
|| dwType == 32841// 关闭被动解码重连功能
|| dwType == 32842// 关闭被动转码重连功能
|| dwType == 32971//
) {
return;
}
}
};
public HCSaveProcess(String m_sDeviceIP, String username, String password, int port, Long deviceId) {
this.deviceId = deviceId;
this.m_sDeviceIP = m_sDeviceIP;
this.username = username;
this.password = password;
this.port = port;
devicePoint = new NativeLongByReference(new NativeLong(deviceId));
// 注册接收异常、重连等消息的窗口句柄或回调函数。
HCNetSDKManger.hCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, exceptionCallBack, devicePoint.getPointer());
}
public void start(String basepath) {
if (isloadding) {
return;
}
issaveing = isloadding = true;
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(null, "注册失败");
return;
} else {
// CreateDeviceTree();
}
if (lUserID == null || lUserID.intValue() == -1) {
logger.info("注册失败");
return;
}
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 {
System.out.println("通道号获取失败");
return;
}
}
if (iChannelNum == -1) {
System.out.println("请选择要预览的通道");
return;
}
m_strClientInfo = new HCNetSDK.NET_DVR_CLIENTINFO();
m_strClientInfo.lChannel = new NativeLong(iChannelNum);
logger.info("设备" + deviceId + "启动拉流。");
errorcout = 0;
lRealHandle = HCNetSDKManger.hCNetSDK.NET_DVR_RealPlay_V30(lUserID, m_strClientInfo, null, null, false);
// 预览失败,如果是用户登陆失效,则重新注册
logger.info("设备" + deviceId + "无法拉流:" + HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError());
if (lRealHandle == null || lRealHandle.longValue() == -1) {
isloadding = issaveing = false;
System.out.println("设备" + deviceId + "无法拉流:" + HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError());
return;
}
// 打开声音,
if (!HCNetSDKManger.hCNetSDK.NET_DVR_OpenSound(lRealHandle)) {
System.out.println("设备" + deviceId + "--存储打开声音失败:" + HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError());
} else {
System.out.println("设备" + deviceId + "--存储打开声音成功!");
}
File file = new File(basepath + "\\");
if (!file.exists()) {
file.mkdirs();
}
filename = basepath + "\\test.mp4";
System.out.println("设备" + deviceId + "--存储目录:" + filename);
boolean result = false;
while (!result && errorcout < ERROR_MAX_TRY_COUNT && issaveing) {
result = HCNetSDKManger.hCNetSDK.NET_DVR_SaveRealData(lRealHandle, filename);
System.out.println("设备" + deviceId + "--存储结果:" + HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError() + "--" + result);
errorcout++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!result) {
isloadding = issaveing = false;
System.out.println("设备" + deviceId + "存储失败。");
System.out.println("设备" + deviceId + "关闭存储播放:" + HCNetSDKManger.hCNetSDK.NET_DVR_StopRealPlay(lRealHandle));
lRealHandle = new NativeLong(-1);
return;
}
logger.info("设备" + deviceId + "开始存储……");
isloadding = false;
while (result && issaveing) {
try {
Thread.sleep(5 * 60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
logger.info("设备" + deviceId + "存储结束。");
}
public void stop() {
logger.info("设备" + deviceId + "结束存储。");
if (lRealHandle != null && lRealHandle.longValue() > -1) {
boolean result = HCNetSDKManger.hCNetSDK.NET_DVR_StopSaveRealData(lRealHandle);
logger.info("设备" + deviceId + "停止存储结果:" + HCNetSDKManger.hCNetSDK.NET_DVR_GetLastError() + "-" + result);
}
if (lRealHandle != null && lRealHandle.longValue() != -1) {
HCNetSDKManger.hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
}
issaveing = false;
System.gc();
}
public boolean isIssaveing() {
// 检测文件名
if (isloadding) {
return true;
}
if (filename == null || "".equals(filename)) {
return false;
}
File file = new File(filename);
if (!file.exists()) {
return false;
}
Date lastUpdateTime = new Date(file.lastModified());
if ((new Date().getTime() - lastUpdateTime.getTime()) > 5 * 60 * 60 * 1000) {// 超过5分钟存储的文件还未更新
return false;
}
return true;
}
}
预览:
源码:源码下载