这是我测试用的main方法
把代码复制走, 只需要修改一下录像机的ip账号密码就可以了
我测试的时候是吧录像机下的所有摄像头都进行了拍照, 你可以根据你的需求进行修改
海康的SDK自行去官网下载
(如需要卡口手动抓拍代码,请留言!)
(如果拍照失败出现29: 客服解答是抓拍太频繁,换一种抓拍方式,但是另外一种太麻烦了、
我就直接调取摄像机拍照就行了, 不通过录像机抓取)
package com.qymj.utils.paizhao;
import java.io.File;
import com.qymj.utils.paizhao.HCNetSDK.NET_DVR_DEVICEINFO;
import com.qymj.utils.paizhao.HCNetSDK.NET_DVR_IPPARACFG;
import com.qymj.utils.paizhao.HCNetSDK.NET_DVR_JPEGPARA;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
public class paizhao {
/**
* @param args
*
*/
private static String sDVRIP="127.0.0.1";
private static short sDVRPort=8000;
private static String sUserName="admin";
private static String sPassword="admin123";
private static String fileRoot="d:/paizhao";
private static int start=0;
private static int end=0;
private static short wPicSize=0xff;///* 0=CIF, 1=QCIF, 2=D1 3=UXGA(1600x1200), 4=SVGA(800x600), 5=HD720p(1280x720),6=VGA*/
private static short wPicQuality=0;// 图片质量系数 0-最好 1-较好 2-一般
public static void main(String[] args) {
File f=new File(fileRoot);
if(!f.exists()){
f.mkdirs();
}
NET_DVR_DEVICEINFO lpDeviceInfo=new NET_DVR_DEVICEINFO();
//加载属性文件
//loadProperties();
//创建海康SDK实例
HCNetSDK hk = HCNetSDK.INSTANCE ;
//SDK初始化
boolean init=hk.NET_DVR_Init();
if(init){//true表示SDK初始化成功
}
//DVR登录
NativeLong userId=hk.NET_DVR_Login(sDVRIP, sDVRPort, sUserName, sPassword, lpDeviceInfo);
NET_DVR_IPPARACFG ipparacfg=new NET_DVR_IPPARACFG();
ipparacfg.write();
Pointer lpIpParaConfig = ipparacfg.getPointer();
IntByReference lpBytesReturned = new IntByReference(0);//获取IP接入配置参数
boolean configIsOk=hk.NET_DVR_GetDVRConfig(userId, hk.NET_DVR_GET_IPPARACFG, new NativeLong(0), lpIpParaConfig, ipparacfg.size(), lpBytesReturned);
ipparacfg.read();
if (!configIsOk){//设备不支持,则表示没有IP通道
for (int iChannum = 0; iChannum < lpDeviceInfo.byChanNum; iChannum++) {
System.out.println(("Camera" + (iChannum +" not support "+ lpDeviceInfo.byStartChan)));
}
}else{//设备支持IP通道
for (int iChannum = 0; iChannum < lpDeviceInfo.byChanNum; iChannum++){
if(ipparacfg.byAnalogChanEnable[iChannum] == 1){
System.out.println(("Camera" + (iChannum +" 支持 "+ lpDeviceInfo.byStartChan)));
}
}
for(int iChannum =0; iChannum < HCNetSDK.MAX_IP_CHANNEL; iChannum++){
if (ipparacfg.struIPChanInfo[iChannum].byEnable == 1)
{
System.out.println("IPCamera" + (iChannum + lpDeviceInfo.byStartChan)+" ip:"+new String(ipparacfg.struIPDevInfo[iChannum].struIP.sIpV4).trim()+
" byChannel="+ipparacfg.struIPChanInfo[iChannum].byChannel+" byEnable="+ipparacfg.struIPChanInfo[iChannum].byEnable+
" byIPID="+ipparacfg.struIPChanInfo[iChannum].byIPID);
}
}
}
System.out.println("登录的userId="+userId);
if(end>0 && start<end){
for(int iChannum =start; iChannum <=end; iChannum++){
NativeLong tdh=new NativeLong(iChannum);
NET_DVR_JPEGPARA cs=new NET_DVR_JPEGPARA();
cs.wPicSize=wPicSize;
cs.wPicQuality=wPicQuality;
String fileName=fileRoot+"/"+sDVRIP+"_"+tdh+".jpeg";
boolean pz=hk.NET_DVR_CaptureJPEGPicture(userId, tdh, cs, fileName);
System.out.println("拍照通道号"+tdh+":"+(pz?"拍照成功照片在"+fileName:"拍照失败!!失败代码:"+hk.NET_DVR_GetLastError()));
}
}else{
for(int iChannum =0; iChannum < HCNetSDK.MAX_IP_CHANNEL; iChannum++){
NativeLong tdh=new NativeLong(ipparacfg.struIPChanInfo[iChannum].byIPID+32);
String ip=new String(ipparacfg.struIPDevInfo[iChannum].struIP.sIpV4).trim();
if (ipparacfg.struIPChanInfo[iChannum].byEnable == 1){
NET_DVR_JPEGPARA cs=new NET_DVR_JPEGPARA();
cs.wPicSize=wPicSize;
cs.wPicQuality=wPicQuality;
String fileName=fileRoot+"/"+ip+"_"+tdh+".jpeg";
boolean pz=hk.NET_DVR_CaptureJPEGPicture(userId, tdh, cs, fileName);
System.out.println("拍照通道号"+tdh+":"+(pz?"拍照成功照片在"+fileName:"拍照失败!!失败代码:"+hk.NET_DVR_GetLastError()));
}else{
System.out.println("通道号"+tdh+"没有启用!IP="+ip);
}
}
}
hk.NET_DVR_Logout(userId);
hk.NET_DVR_Cleanup();
}
}