海康SDK集成,PTZ控制

 @ApiOperation(value = "设置海康摄像头PTZ参数", notes = "设置海康摄像头PTZ参数")
    @Log(action = "设置海康摄像头PTZ参数", tag = "/setHKCameraPTZValue")
    @WebMapping("/setHKCameraPTZValue")
    public JSONObject setHKCameraPTZvalue(String ip,String username,String password,int iPort,Integer wTiltPos,Integer wPanPos,Integer wZoomPos,Integer direction){
     
        NativeLong lUserID  = new  NativeLong(-1);;//用户句柄
        boolean initSuc = hCNetSDK.NET_DVR_Init();//设备初始化
        lUserID = hCNetSDK.NET_DVR_Login_V30(ip, (short) iPort,username,password, null);//登陆
        HCNetSDK.NET_DVR_PTZPOS m_ptzPosCurrent = new  HCNetSDK.NET_DVR_PTZPOS();
        IntByReference ibrBytesReturned = new IntByReference(0);
        Pointer pioint  = m_ptzPosCurrent.getPointer();
        hCNetSDK.NET_DVR_GetDVRConfig(lUserID, HCNetSDK.NET_DVR_GET_PTZPOS,new NativeLong(0),pioint,m_ptzPosCurrent.size(),ibrBytesReturned);
        m_ptzPosCurrent.read();

        System.out.println("PTZ垂直参数为: "+m_ptzPosCurrent.wTiltPos);
        System.out.println("PTZ水平参数为: "+m_ptzPosCurrent.wPanPos);
        System.out.println("PTZ变倍参数为: "+m_ptzPosCurrent.wZoomPos);

        HCNetSDK.NET_DVR_PTZSCOPE poe = new  HCNetSDK.NET_DVR_PTZSCOPE();
        IntByReference ibrBytesReturned1 = new IntByReference(0);
        hCNetSDK.NET_DVR_GetDVRConfig(lUserID, HCNetSDK.NET_DVR_GET_PTZSCOPE,new NativeLong(0),poe.getPointer(),poe.size(),ibrBytesReturned1);
        poe.read();

        if(direction == direction_up){
     //向上
           int wTiltPosNew =  Integer.valueOf(Integer.toHexString(m_ptzPosCurrent.wTiltPos)) - (wTiltPos);
            wTiltPosNew =  Integer.parseInt((String.valueOf(wTiltPosNew)),16);
           if(wTiltPosNew < 0){
     
               m_ptzPosCurrent.wTiltPos = 0;
           }else{
     
               m_ptzPosCurrent.wTiltPos = (short)wTiltPosNew;
           }

        }else if(direction == direction_down){
     //向下
            int wTiltPosNew =  Integer.valueOf(Integer.toHexString(m_ptzPosCurrent.wTiltPos)) + (wTiltPos);
            wTiltPosNew =  Integer.parseInt((String.valueOf(wTiltPosNew)),16);
            if(wTiltPosNew > poe.wTiltPosMin){
     
                m_ptzPosCurrent.wTiltPos = poe.wTiltPosMin;
            }else{
     
                m_ptzPosCurrent.wTiltPos = (short)wTiltPosNew;
            }
        }else if(direction == direction_left){
     //向左

            int wPanPosNew =  Integer.valueOf(Integer.toHexString(m_ptzPosCurrent.wPanPos)) - (wPanPos);
            wPanPosNew =  Integer.parseInt((String.valueOf(wPanPosNew)),16);

            if(wPanPosNew < poe.wPanPosMin){
     
                m_ptzPosCurrent.wPanPos = poe.wPanPosMin;
            }else{
     
                m_ptzPosCurrent.wPanPos=(short)wPanPosNew;
            }
        }else if(direction == direction_right){
     //向右
            int wPanPosNew =  Integer.valueOf(Integer.toHexString(m_ptzPosCurrent.wPanPos)) + (wPanPos);
            wPanPosNew =  Integer.parseInt((String.valueOf(wPanPosNew)),16);
            if(wPanPosNew > poe.wPanPosMax){
     
                m_ptzPosCurrent.wPanPos = poe.wPanPosMax;
            }else{
     
                m_ptzPosCurrent.wPanPos=(short)wPanPosNew;
            }

        }else if(direction == direction_enlarge){
     //放大

            int wZoomPosNew =  Integer.valueOf(Integer.toHexString(m_ptzPosCurrent.wZoomPos)) + (wZoomPos);
            wZoomPosNew =  Integer.parseInt((String.valueOf(wZoomPosNew)),16);

            if(wZoomPosNew > poe.wZoomPosMax){
     
                m_ptzPosCurrent.wZoomPos=poe.wZoomPosMax;
            }else{
     
                m_ptzPosCurrent.wZoomPos=(short)wZoomPosNew;
            }

        }else if(direction == direction_narrow){
     //缩小
            int wZoomPosNew =  Integer.valueOf(Integer.toHexString(m_ptzPosCurrent.wZoomPos)) - (wZoomPos);
            wZoomPosNew =  Integer.parseInt((String.valueOf(wZoomPosNew)),16);

            if(wZoomPosNew < poe.wZoomPosMin){
     
                m_ptzPosCurrent.wZoomPos=poe.wZoomPosMin;
            }else{
     
                m_ptzPosCurrent.wZoomPos=(short)wZoomPosNew;
            }
        }
        m_ptzPosCurrent.write();
        JSONObject current = new JSONObject();
        current.put("wZoomPos",Integer.toHexString(m_ptzPosCurrent.wZoomPos));
        current.put("wPanPos",Integer.toHexString(m_ptzPosCurrent.wPanPos));
        current.put("wTiltPos",Integer.toHexString(m_ptzPosCurrent.wTiltPos));
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetDVRConfig(lUserID, HCNetSDK.NET_DVR_SET_PTZPOS, new NativeLong(0), m_ptzPosCurrent.getPointer(),m_ptzPosCurrent.size());
        hCNetSDK.NET_DVR_GetLastError();
        JSONObject result  = new JSONObject();;
        if(hCNetSDK.NET_DVR_GetLastError() == 0){
     
            result.put("result","设置成功");
            result.put("data",current);
        }else{
     
            result.put("result","设置失败");
            result.put("data","");
        }

        hCNetSDK.NET_DVR_Cleanup();
        return result;
    }

实际显示的PTZ值是获取到的十六进制值的十分之一,如获取的水平参数P的值是0x1750,实际显示的P值为175度;获取到的垂直参数T的值是0x0789,实际显示的T值为78.9度;获取到的变倍参数Z的值是0x1100,实际显示的Z值为110倍。

你可能感兴趣的:(sdk)