海康摄像头调用程序

启动Sdk的工具类 海康摄像头调用程序
package czc.superzig.modular.utils.camera;

import java.io.File;

import com.sun.jna.Callback;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.examples.win32.W32API.DWORD;
import com.sun.jna.examples.win32.W32API.HWND;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;


public interface HCNetSDK extends Library {
    public static final int NET_DVR_DEV_ADDRESS_MAX_LEN = 129;
    public static final int NET_DVR_LOGIN_USERNAME_MAX_LEN = 64;
    public static final int NET_DVR_LOGIN_PASSWD_MAX_LEN = 64;
    public static final int SERIALNO_LEN = 48; // 序列号长度
    public static final int NAME_LEN = 32; // 用户名长度
    public static final int NET_DVR_SYSHEAD = 1;//系统头数据
    public static final int NET_DVR_STREAMDATA = 2;//视频流数据(包括复合流和音视频分开的视频流数据)\
    String LINUX_PATH=File.separator + "home" + File.separator + "czc_" + File.separator+"java"+File.separator+"lib" +File.separator+ "libhcnetsdk.so";
    String WINDOWS_PATH="hcnetsdk";
    HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary(WINDOWS_PATH, HCNetSDK.class);

    boolean NET_DVR_Init();

    boolean NET_DVR_Cleanup();

    boolean NET_DVR_SaveRealData(NativeLong lRealHandle, String sFileName);

    boolean NET_DVR_StopSaveRealData(NativeLong lRealHandle);
    //获取解码和显示的播放库句柄
    int  NET_DVR_GetRealPlayerIndex(NativeLong lRealHandle);
    boolean NET_DVR_StopRealPlay(NativeLong lRealHandle);
    
    boolean NET_DVR_SetRealDataCallBack(NativeLong lRealHandle, FRowDataCallBack fRealDataCallBack, int dwUser);

    boolean NET_DVR_SetStandardDataCallBack(NativeLong lRealHandle, FStdDataCallBack fStdDataCallBack, int dwUser);

    NativeLong NET_DVR_RealPlay_V30(NativeLong lUserID, NET_DVR_CLIENTINFO lpClientInfo,
            FRealDataCallBack_V30 fRealDataCallBack_V30, Pointer pUser, boolean bBlocked);

    boolean NET_DVR_SetLogToFile(boolean bLogEnable, String strLogDir, boolean bAutoDel);

    NativeLong NET_DVR_Login_V40(Pointer pLoginInfo, Pointer lpDeviceInfo);

    boolean NET_DVR_Logout_V30(NativeLong lUserID);
    
    boolean  NET_DVR_CapturePicture(NativeLong lRealHandle,String sPicFileName);
    
    //boolean NET_DVR_SetCapturePictureMode(DWORD dwCaptureMode);
    boolean NET_DVR_SetCapturePictureMode(int dwCaptureMode);
    public static class NET_DVR_CLIENTINFO extends Structure {
        public NativeLong lChannel;
        public NativeLong lLinkMode;
        public HWND hPlayWnd;
        public String sMultiCastIP;
    }
    


    /*** API函数声明,详细说明见API手册 ***/
    public static interface FRealDataCallBack_V30 extends StdCallCallback {
        public void invoke(NativeLong lRealHandle, int dwDataType, ByteByReference pBuffer, int dwBufSize,
                Pointer pUser);
    }

    public static interface FStdDataCallBack extends StdCallCallback {
        public void invoke(NativeLong lRealHandle, int dwDataType, ByteByReference pBuffer, int dwBufSize, int dwUser);
    }

    public static interface FRowDataCallBack extends StdCallCallback {
        public void invoke(NativeLong lUserID, String sIPAddr, NativeLong lRowAmout, String pRecvDataBuffer,
                int dwBufSize, int dwUser);
    }

    public static class NET_DVR_LOCAL_SDK_PATH extends Structure {
        public byte[] sPath = new byte[256];
        public byte[] byRes = new byte[128];
    }

    boolean NET_DVR_SetSDKInitCfg(int enumType, Pointer lpInBuff);

    int NET_DVR_GetLastError();

    boolean NET_DVR_CaptureJPEGPicture(NativeLong lUserID, NativeLong lChannel, NET_DVR_JPEGPARA lpJpegPara,
            String sPicFileName);

    boolean NET_DVR_GetDVRConfig(NativeLong lUserID, int dwCommand, NativeLong lChannel, Pointer lpOutBuffer,
            int dwOutBufferSize, IntByReference lpBytesReturned);

    // 图片质量
    public static class NET_DVR_JPEGPARA extends Structure {
        /*
         * 注意:当图像压缩分辨率为VGA时,支持0=CIF, 1=QCIF, 2=D1抓图, 当分辨率为3=UXGA(1600x1200),
         * 4=SVGA(800x600), 5=HD720p(1280x720),6=VGA,7=XVGA, 8=HD900p 仅支持当前分辨率的抓图
         */
        public short wPicSize; /*
                                 * 0=CIF, 1=QCIF, 2=D1 3=UXGA(1600x1200), 4=SVGA(800x600),
                                 * 5=HD720p(1280x720),6=VGA
                                 */
        public short wPicQuality; /* 图片质量系数 0-最好 1-较好 2-一般 */
    }
    boolean  NET_DVR_CaptureJPEGPicture_NEW(NativeLong lUserID, NativeLong lChannel, NET_DVR_JPEGPARA lpJpegPara, String sJpegPicBuffer, int dwPicSize,  IntByReference lpSizeReturned);
    public static class NET_DVR_USER_LOGIN_INFO extends Structure {
        public byte[] sDeviceAddress = new byte[NET_DVR_DEV_ADDRESS_MAX_LEN];
        public byte byUseTransport;
        public short wPort;
//        public  fLoginResultCallBack cbLoginResult;
        public byte[] sUserName = new byte[NET_DVR_LOGIN_USERNAME_MAX_LEN];
        public byte[] sPassword = new byte[NET_DVR_LOGIN_PASSWD_MAX_LEN];
        public FLoginResultCallBack cbLoginResult;
        Pointer pUser;
        public int bUseAsynLogin;
        public byte[] byRes2 = new byte[128];
    }

    // NET_DVR_Login_V40()参数
    public static class NET_DVR_DEVICEINFO_V40 extends Structure {
        public NET_DVR_DEVICEINFO_V30 struDeviceV30 = new NET_DVR_DEVICEINFO_V30();
        public byte bySupportLock;
        public byte byRetryLoginTime;
        public byte byPasswordLevel;
        public byte byRes1;
        public int dwSurplusLockTime;
        public byte[] byRes2 = new byte[256];
    }

    // NET_DVR_Login_V30()参数结构
    public static class NET_DVR_DEVICEINFO_V30 extends Structure {
        public byte[] sSerialNumber = new byte[SERIALNO_LEN]; // 序列号
        public byte byAlarmInPortNum; // 报警输入个数
        public byte byAlarmOutPortNum; // 报警输出个数
        public byte byDiskNum; // 硬盘个数
        public byte byDVRType; // 设备类型, 1:DVR 2:ATM DVR 3:DVS ......
        public byte byChanNum; // 模拟通道个数
        public byte byStartChan; // 起始通道号,例如DVS-1,DVR - 1
        public byte byAudioChanNum; // 语音通道数
        public byte byIPChanNum; // 最大数字通道个数
        public byte[] byRes1 = new byte[24]; // 保留
    }

    public static interface FLoginResultCallBack extends Callback {
        public int invoke(NativeLong lUserID, int dwResult, Pointer lpDeviceinfo, Pointer pUser);
    }
}
View Code

调用摄像头生成图片和截取视频的方法

海康摄像头调用程序
package czc.superzig.modular.utils.camera;

import com.ibm.icu.math.BigDecimal;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.examples.win32.W32API.HWND;

import czc.superzig.common.operatingtable.base.entity.Result;
import czc.superzig.modular.utils.Contants;
import czc.superzig.modular.utils.LocalCache;
import groovy.mock.interceptor.Demand;

import java.awt.Canvas;
import java.awt.Frame;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JWindow;



public class DemoCapture {

    
    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    private NativeLong  lRealHandle;
    private NativeLong  userId;
    private  HCNetSDK.NET_DVR_CLIENTINFO m_strClientInfo;// 用户参数

    //拍照
    public String capture(String path) throws Exception {
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs(); 
        }
        String fileName=path+System.currentTimeMillis()+".jpg";
        HCNetSDK.NET_DVR_JPEGPARA picPara = new HCNetSDK.NET_DVR_JPEGPARA();
        System.out.print("******" + hCNetSDK.NET_DVR_GetLastError() + "*********\n");
        picPara.wPicSize = 0;
        picPara.wPicQuality = 0;
        NativeLong chan = new NativeLong();
        hCNetSDK.NET_DVR_GetDVRConfig(this.userId, 0, chan, null, 0, null);
        System.out.print("the chan is :" + chan + "\n");
        NativeLong channel = new NativeLong(1);
        System.out.print("the channel is :" + channel + "\n");
        String picPath = fileName;
        boolean ff = hCNetSDK.NET_DVR_CaptureJPEGPicture(this.userId, channel, picPara, picPath);
        System.out.print("******" + ff + hCNetSDK.NET_DVR_GetLastError() + "*********\n");
        if(!ff){
            throw new Exception();
        }
        return fileName;
    }



    public  void initDataVideo() {
        String choose = "r";
        //String strPathCom = "/home/pual/workspace/lib";
        String strPathCom = "/usr/local/tomcat/lib";
        HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
        System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
        struComPath.write();
        boolean loginFlag = hCNetSDK.NET_DVR_Init();
        System.out.print("initSDK is " + loginFlag + "\n");
        System.out.print("error code :" + hCNetSDK.NET_DVR_GetLastError() + "\n");
        hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
        hCNetSDK.NET_DVR_SetLogToFile(true, null, false);
        lRealHandle = new NativeLong(-1);
        this.m_strClientInfo = new HCNetSDK.NET_DVR_CLIENTINFO();
        this.m_strClientInfo.lChannel = new NativeLong(1);
        this.m_strClientInfo.lLinkMode= new NativeLong(0);
        this.m_strClientInfo.hPlayWnd = null;
    }

    //摄像头登录
    public  void myLogin(String ip) {
        long starttime = System.currentTimeMillis();
        HCNetSDK.NET_DVR_USER_LOGIN_INFO struLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
        HCNetSDK.NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
        struLoginInfo.bUseAsynLogin = 0;

        struLoginInfo.wPort = 8000;
        String address = ip;
        String userName = "admin";
        String passWord = "czc123456";
        for (int i = 0; i < address.length(); i++) {
            struLoginInfo.sDeviceAddress[i] = (byte) address.charAt(i);
        }
        for (int i = 0; i < userName.length(); i++) {
            struLoginInfo.sUserName[i] = (byte) userName.charAt(i);
        }
        for (int i = 0; i < passWord.length(); i++) {
            struLoginInfo.sPassword[i] = (byte) passWord.charAt(i);
        }
        struLoginInfo.write();
        userId = hCNetSDK.NET_DVR_Login_V40(struLoginInfo.getPointer(), struDeviceInfoV40.getPointer());
        System.out.print("the userId is :" + userId + "\n");
        System.out.print("**********"+ip + hCNetSDK.NET_DVR_GetLastError() +this.m_strClientInfo.lChannel+ "*****************\n");
        long stoptime = System.currentTimeMillis();
        System.out.print("Login time is :" + (stoptime - starttime) + "\n");
    }
    //开始录像
    public Result startRecord(String ip,String path) {
        //destroySDK();
        Result result=new Result();
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        String fileName=path+System.currentTimeMillis()+".mp4";
        initDataVideo();
        myLogin(ip);
        long starttime = System.currentTimeMillis();
        lRealHandle = hCNetSDK.NET_DVR_RealPlay_V30(this.userId, this.m_strClientInfo, null, null, true);
        if(lRealHandle.longValue()==-1){
            initDataVideo();
            myLogin(ip);
            lRealHandle = hCNetSDK.NET_DVR_RealPlay_V30(this.userId, this.m_strClientInfo, null, null, true);
            System.err.println("---------"+hCNetSDK.NET_DVR_GetLastError());
        }
        System.err.println("---------"+hCNetSDK.NET_DVR_GetLastError());
        boolean mark = hCNetSDK.NET_DVR_SaveRealData(lRealHandle, fileName);
        result.setCode(fileName);
        result.setData(lRealHandle);
        return result;
    }
    //停止录像
    public void stopRecord(NativeLong lRealHandle) {
        hCNetSDK.NET_DVR_StopSaveRealData(lRealHandle);
        destroySDK();
        System.err.println(hCNetSDK.NET_DVR_GetLastError());
        System.out.print("*************stoptime************:" + System.currentTimeMillis() + "\n");
    }

    public void destroySDK() {
        hCNetSDK.NET_DVR_StopRealPlay(this.lRealHandle);
        hCNetSDK.NET_DVR_Logout_V30(this.userId);
        hCNetSDK.NET_DVR_Cleanup();
    }

    /**
     * 预览抓图、目前获取图片最快的方法
     * @throws Exception 
     */
    public String  takePicture(String path) throws Exception {
        long starttime = System.currentTimeMillis();
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
            
        }
        String fileName=path+System.currentTimeMillis()+".jpg";
        JFrame wnd = new JFrame();
        wnd.setSize(100, 100);
        wnd.setVisible(true);
        wnd.setLocation(-100, -100);
        
        final HWND hwnd = new HWND(Native.getComponentPointer(wnd));
        m_strClientInfo.hPlayWnd = hwnd;
        
        this.lRealHandle = hCNetSDK.NET_DVR_RealPlay_V30(this.userId, this.m_strClientInfo, null, null, true);
        System.out.print("**********"+this.m_strClientInfo.lChannel+" :"+ hCNetSDK.NET_DVR_GetLastError() + "*****************\n");
        hCNetSDK.NET_DVR_SetCapturePictureMode(1);
        boolean flag = hCNetSDK.NET_DVR_CapturePicture(this.lRealHandle, fileName);
        System.out.print("the result :" + flag + " and use time " + hCNetSDK.NET_DVR_GetLastError() + "  "
                + (System.currentTimeMillis() - starttime) + "\n");
        wnd.dispose();
        if(!flag){
            throw new Exception();
        }
        return fileName;
    }
    
    public NativeLong getlRealHandle() {
        return lRealHandle;
    }
    public void setlRealHandle(NativeLong lRealHandle) {
        this.lRealHandle = lRealHandle;
    }
    
    
    
    public  NativeLong getUserId() {
        return userId;
    }

    public  void setUserId(NativeLong userId) {
        this.userId = userId;
    }
    
    public static void main(String[] args) throws Exception {
        DemoCapture capture=new DemoCapture();
        capture.initDataVideo();
        capture.myLogin("10.0.0.223");
        capture.capture("d://");
        System.err.println();
    }

}
View Code

 

上一篇:海康摄像头SDK在Linux、windows下的兼容问题(二)已解决


下一篇:[C#] 折腾海康威视的人体测温 模组