天天看點

海康攝像頭拍照(java版,拿走即用)

這是我測試用的main方法

把代碼複制走, 隻需要修改一下錄像機的ip賬号密碼就可以了

我測試的時候是吧錄像機下的所有攝像頭都進行了拍照, 你可以根據你的需求進行修改

海康的SDK自行去官網下載下傳

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();
	}
}