Android 集成海康威视监控 SDK,实现监控录像的查看
切换Android studio 到project目录下,分别将jar包与armeabi包复制进去lib文件夹下:
设置app bulid.gradle文件 主要是添加CPU限制 只支持32位 以及添加so包的路径、添加jar包依赖:
添加权限
在调用摄像头的Activity的oncreate()中初始化SDK
private boolean initeSdk() { // init net sdk if (!HCNetSDK.getInstance().NET_DVR_Init()) { Log.e(TAG, "HCNetSDK init is failed!"); return false; } HCNetSDK.getInstance().NET_DVR_SetLogToFile(3, "/mnt/sdcard/sdklog/",true);//log保存的路径 return true; }
登陆
private int loginNormalDevice() { // get instance m_oNetDvrDeviceInfoV30 = new NET_DVR_DEVICEINFO_V30(); if (null == m_oNetDvrDeviceInfoV30) { Log.e(TAG, "HKNetDvrDeviceInfoV30 new is failed!"); return -1; } // call NET_DVR_Login_v30 to login on, port 8000 as default int iLogID = HCNetSDK.getInstance().NET_DVR_Login_V30(ADDRESS, PORT, USER, PSD, m_oNetDvrDeviceInfoV30); if (iLogID < 0) { Log.e(TAG, "NET_DVR_Login is failed!Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); return -1; } if (m_oNetDvrDeviceInfoV30.byChanNum > 0) { m_iStartChan = m_oNetDvrDeviceInfoV30.byStartChan; m_iChanNum = m_oNetDvrDeviceInfoV30.byChanNum; } else if (m_oNetDvrDeviceInfoV30.byIPChanNum > 0) { m_iStartChan = m_oNetDvrDeviceInfoV30.byStartDChan; m_iChanNum = m_oNetDvrDeviceInfoV30.byIPChanNum + m_oNetDvrDeviceInfoV30.byHighDChanNum * 256; } Log.i(TAG, "NET_DVR_Login is Successful!"); return iLogID; }
相机预览
//预览 final NET_DVR_PREVIEWINFO ClientInfo = new NET_DVR_PREVIEWINFO(); ClientInfo.lChannel = 0; ClientInfo.dwStreamType = 0; // substream ClientInfo.bBlocked = 1; //设置默认点 thread = new Thread(new Runnable() { @Override public void run() { while (!Thread.currentThread().isInterrupted()) { SystemClock.sleep(1000); runOnUiThread(new Runnable() { @Override public void run() { if (isShow) startSinglePreview();//预览 } }); } } }); thread.start();
预览摄像头需要开启线程,监听预览成功后停止线程,不然界面会显示黑色。
startSinglePreview()代码:
private void startSinglePreview() { if (m_iPlaybackID >= 0) { Log.i(TAG, "Please stop palyback first"); return; } RealPlayCallBack fRealDataCallBack = getRealPlayerCbf(); if (fRealDataCallBack == null) { Log.e(TAG, "fRealDataCallBack object is failed!"); return; } Log.i(TAG, "m_iStartChan:" + m_iStartChan); NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO(); previewInfo.lChannel = m_iStartChan; previewInfo.dwStreamType = 0; // substream previewInfo.bBlocked = 1; m_iPlayID = HCNetSDK.getInstance().NET_DVR_RealPlay_V40(m_iLogID, previewInfo, fRealDataCallBack); if (m_iPlayID < 0) { Log.e(TAG, "NET_DVR_RealPlay is failed!Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); return; } isShow = false; if (NotNull.isNotNull(thread)) { thread.interrupt(); } h1 = new CameraManager(); h1.setLoginId(m_iLogID); Intent intent = getIntent(); if (NotNull.isNotNull(intent) && intent.getIntExtra("INDEX", -1) != -1) { int point = app.preferences.getInt("POINT", 0); boolean b = HCNetSDK.getInstance().NET_DVR_PTZPreset(m_iPlayID, PTZCommand.GOTO_PRESET, point); } }
控制台操作
@Override public boolean onTouch(final View v, final MotionEvent event) { if (!NotNull.isNotNull(h1)) return false; Log.d(TAG, "onTouch: "); new Thread() { @Override public void run() { switch (v.getId()) { case R.id.btn_Up: if (event.getAction() == MotionEvent.ACTION_DOWN) { h1.startMove(8, m_iLogID); } if (event.getAction() == MotionEvent.ACTION_UP) { h1.stopMove(8, m_iLogID); } break; case R.id.btn_Left: if (event.getAction() == MotionEvent.ACTION_DOWN) { h1.startMove(4, m_iLogID); } if (event.getAction() == MotionEvent.ACTION_UP) { h1.stopMove(4, m_iLogID); } break; case R.id.btn_Right: if (event.getAction() == MotionEvent.ACTION_DOWN) { h1.startMove(6, m_iLogID); } if (event.getAction() == MotionEvent.ACTION_UP) { h1.stopMove(6, m_iLogID); } break; case R.id.btn_Down: if (event.getAction() == MotionEvent.ACTION_DOWN) { h1.startMove(2, m_iLogID); } if (event.getAction() == MotionEvent.ACTION_UP) { h1.stopMove(2, m_iLogID); } break; case R.id.btn_ZoomIn: if (event.getAction() == MotionEvent.ACTION_DOWN) { h1.startZoom(1, m_iLogID); } if (event.getAction() == MotionEvent.ACTION_UP) { h1.stopZoom(1, m_iLogID); } break; case R.id.btn_ZoomOut: if (event.getAction() == MotionEvent.ACTION_DOWN) { h1.startZoom(-1, m_iLogID); } if (event.getAction() == MotionEvent.ACTION_UP) { h1.stopZoom(-1, m_iLogID); } break; default: break; } } }.start(); return false; }
/** * 停止移动 NET_DVR_PTZControl_Other参数:(播放标记, 通道, 指令码, 开始标记0或停止标记1) * * @param orientation 九宫格数字方向 */ public void stopMove(int orientation, int m_iLogID) { if (m_iLogID < 0) { return; } switch (orientation) { case 9: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.UP_RIGHT, 1); break; case 8: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.TILT_UP, 1); break; case 7: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.UP_LEFT, 1); break; case 6: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.PAN_RIGHT, 1); break; case 5: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.PAN_AUTO, 1); break; case 4: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.PAN_LEFT, 1); break; case 3: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.DOWN_RIGHT, 1); break; case 2: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.TILT_DOWN, 1); break; case 1: HCNetSDK.getInstance().NET_DVR_PTZControl_Other(m_iLogID, 1, PTZCommand.DOWN_LEFT, 1); break; default: break; } }
Github传送门
转自 wangguan_9527 博客地址 https://blog.csdn.net/wangguan_9527/article/details/78168717