();
public HCNetDemo() {
init();
}
private void init() {
initUI();
initSDK();
login("192.168.32.31", (short) 8000, "admin", "12345");
}
private void login(String ip, short port, String username, String passwd) {
if (userid != null && userid.longValue() >= 0) {
HCNetSDK.NET_DVR_Logout(userid);
}
NET_DVR_DEVICEINFO_V30 lpDeviceInfo = new NET_DVR_DEVICEINFO_V30();
// 第二步
userid = HCNetSDK.NET_DVR_Login_V30(ip, port, username, passwd,
lpDeviceInfo);
if (HCNetSDK.NET_DVR_GetLastError() != 0) {
userid = new NativeLong(-1);
showMessage("登录失败:"
+ getErrorMessage(HCNetSDK.NET_DVR_GetLastError()));
return;
}
System.out.println("序列号:" + new String(lpDeviceInfo.sSerialNumber));
System.out.println("通道数量:" + (lpDeviceInfo.byChanNum));
System.out.println("硬盘数量:" + (lpDeviceInfo.byDiskNum));
}
private void initSDK() {
boolean initSuccess = HCNetSDK.NET_DVR_Init();
if (!initSuccess) {
showMessage("初始化失败HCNETSDK:"
+ getErrorMessage(INSTANCE.NET_DVR_GetLastError()));
return;
}
HCNetSDK.NET_DVR_SetConnectTime(2000, 1);
}
private String getErrorMessage(int errorCode) {
return GlobalError.valueOf(errorCode).message();
}
private void initUI() {
// 这里用Panel,别自作主张换成JPanel(视频的panel和其父panel 只能用awt的容器,)
Panel parent = new Panel();
parent.setPreferredSize(new Dimension(660, 530));
jf.add(parent);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
parent.setLayout(new FlowLayout());
final Panel panels[] = new Panel[4];
for (int i = 0; i < panels.length; i++) {
Panel cardPanel = new Panel();
cardPanel.setPreferredSize(new Dimension(320, 240));
panels[i] = cardPanel;
cardPanel.setBackground(new Color(0, 0, 0));
parent.add(cardPanel);
final int channel = i + 1;
cardPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
panelRealplayMousePressed(e, channel);
}
});
}
//按钮
final JButton start = new JButton("开始预览");
start.setPreferredSize(new Dimension(100, 25));
JButton capture = new JButton("截图");
capture.setPreferredSize(new Dimension(100, 25));
parent.add(start);
parent.add(capture);
jf.pack();
jf.setVisible(true);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (userid.longValue() < 0) {
showMessage("请先登录");
return;
}
if (handles.isEmpty()) {
for (int i = 0; i < panels.length; i++) {
Panel cardPanel = panels[i];
HWND hwnd = createHWNDByComponent(cardPanel);
NET_DVR_CLIENTINFO lpClientInfo = new NET_DVR_CLIENTINFO();
lpClientInfo.hPlayWnd = hwnd;
lpClientInfo.lChannel = new NativeLong(i + 1);
lpClientInfo.sMultiCastIP = "";
lpClientInfo.lLinkMode = new NativeLong(0);
// 第三步
NativeLong handle = HCNetSDK.NET_DVR_RealPlay_V30(
userid, lpClientInfo, null, null, true);
int net_DVR_GetLastError = HCNetSDK
.NET_DVR_GetLastError();
if (net_DVR_GetLastError != 0) {
System.out.println("error:"
+ getErrorMessage(net_DVR_GetLastError));
} else {
System.out.println("handle:" + handle.longValue());
handles.add(handle);
}
}
if (!handles.isEmpty()) {
start.setText("停止预览");
}
} else {
for (NativeLong handle : handles) {
HCNetSDK.NET_DVR_StopRealPlay(handle);
}
handles.clear();
start.setText("开始预览");
}
}
});
capture.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String folder = "d:\\pic";
new File(folder).mkdirs();
for (NativeLong handle : handles) {
HCNetSDK.NET_DVR_CapturePicture(handle, folder + "\\"
+ handle + ".jpg");
}
int error = HCNetSDK.NET_DVR_GetLastError();
boolean hasSuccess = false;
if (error != 0) {
System.out.println(getErrorMessage(error));
showMessage("抓图失败:" + getErrorMessage(error));
} else {
hasSuccess = true;
showMessage("抓图成功,位置在:" + folder);
}
if (hasSuccess) {
showMessage("抓图成功,位置在:" + folder);
}
}
});
}
private void showMessage(String msg) {
JOptionPane.showMessageDialog(jf, msg);
}
private void panelRealplayMousePressed(java.awt.event.MouseEvent evt,
int channel) {
// 鼠标单击事件为双击
if (evt.getClickCount() == 2) {
// 新建JWindow 全屏预览
final JWindow wnd = new JWindow();
// 获取屏幕尺寸
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
wnd.setSize(screenSize);
wnd.setVisible(true);
final HWND hwnd = new HWND(Native.getComponentPointer(wnd));
NET_DVR_CLIENTINFO lpClientInfo = new NET_DVR_CLIENTINFO();
lpClientInfo.hPlayWnd = hwnd;
lpClientInfo.lChannel = new NativeLong(channel);
lpClientInfo.sMultiCastIP = "";
lpClientInfo.lLinkMode = new NativeLong(0);
final NativeLong lRealHandle = HCNetSDK.NET_DVR_RealPlay_V30(
userid, lpClientInfo, null, null, true);
// JWindow增加双击响应函数,双击时停止预览,退出全屏
wnd.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 2) {
// 停止预览
HCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
wnd.dispose();
}
}
});
}
}
private HWND createHWNDByComponent(Component parent) {
return new HWND(Native.getComponentPointer(parent));
}
}基于海康威视SDK javaC/S
原文:http://www.cnblogs.com/sunflo/p/4276352.html