【海康摄像头二次开发】移植海康摄像头Android SDK并实现多屏显示

文章目录

  • 前言
  • 一、看海康的例程
  • 二、重要的函数分析
    • HCNetSDK
    • PlayerSDK
  • 三、移植
    • 1.引入库
    • 3.写一个设备信息管理的类
    • 4.设备登录的类
    • 5.视频显示的控件
    • 6、通用的工具类
    • 7、使用
  • 效果展示

【海康摄像头二次开发】移植海康摄像头Android SDK并实现多屏显示_第1张图片


前言

最近接到了一个让人窒息的活儿。简单来说需要做一个机器人的遥控器。需求就是用6818 Android系统做一个通过实体按键控制机器人同时要将机器人身上几个摄像头的数据显示到屏幕上的功能。这就有点窒息了。再此之前还没怎么深入接触过Android,于是又花了将近一个月的时间去学了一下子简单的Android开发。然后就风风火火的搞起来了。好像海康的文档真心不太多,搞起来还是有点麻烦的。

一、看海康的例程

移植海康的SDK的第一步当然是看他给出的事例了。

/**
 * 

DemoActivity Class

* @author zhuzhenlei 2014-7-17 * @version V1.0 * @modificationHistory * @modify by user: * @modify by reason: */
package com.test.demo; import org.json.JSONArray; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import org.MediaPlayer.PlayM4.Player; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.PixelFormat; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; import android.view.Surface; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import com.hcnetsdk.jna.HCNetSDKJNAInstance; import com.hikvision.netsdk.ExceptionCallBack; import com.hikvision.netsdk.HCNetSDK; import com.hikvision.netsdk.INT_PTR; import com.hikvision.netsdk.NET_DVR_COMPRESSIONCFG_V30; import com.hikvision.netsdk.NET_DVR_DEVICEINFO_V30; import com.hikvision.netsdk.NET_DVR_PLAYBACK_INFO; import com.hikvision.netsdk.NET_DVR_PLAYCOND; import com.hikvision.netsdk.NET_DVR_PREVIEWINFO; import com.hikvision.netsdk.NET_DVR_TIME; import com.hikvision.netsdk.NET_DVR_VOD_PARA; import com.hikvision.netsdk.PTZCommand; import com.hikvision.netsdk.PlaybackCallBack; import com.hikvision.netsdk.PlaybackControlCommand; import com.hikvision.netsdk.StdDataCallBack; import com.hikvision.netsdk.VoiceDataCallBack; import com.hikvision.netsdk.RealPlayCallBack; import com.hikvision.netsdk.RealDataCallBack; /** *
 *  ClassName  DemoActivity Class
 * 
* * @author zhuzhenlei * @version V1.0 * @modificationHistory */
public class DemoActivity extends Activity implements Callback{ private Button m_oLoginBtn = null; private Button m_oPreviewBtn = null; private Button m_oPlaybackBtn = null; private Button m_oParamCfgBtn = null; private Button m_oCaptureBtn = null; private Button m_oRecordBtn = null; private Button m_oTalkBtn = null; private Button m_oPTZBtn = null; private Button m_oOtherBtn = null; private EditText m_oIPAddr = null; private EditText m_oPort = null; private EditText m_oUser = null; private EditText m_oPsd = null; private SurfaceView m_osurfaceView = null; private final static int REQUEST_CODE = 1; // private final static int RESULT_OK = 0; private NET_DVR_DEVICEINFO_V30 m_oNetDvrDeviceInfoV30 = null; private StdDataCallBack cbf = null; private RealDataCallBack rdf = null; private int m_iLogID = -1; // return by NET_DVR_Login_v30 private int m_iPlayID = -1; // return by NET_DVR_RealPlay_V30 private int m_iPlaybackID = -1; // return by NET_DVR_PlayBackByTime private int m_iPort = -1; // play port private int m_iStartChan = 0; // start channel no private int m_iChanNum = 0; // channel number private static PlaySurfaceView[] playView = new PlaySurfaceView[4]; private final String TAG = "DemoActivity"; private boolean m_bTalkOn = false; private boolean m_bPTZL = false; private boolean m_bMultiPlay = false; private boolean m_bInsideDecode = true; private boolean m_bSaveRealData = false; private boolean m_bStopPlayback = false; private String m_retUrl = ""; public static String accessToken = ""; public static String areaDomain = ""; public static String appkey = ""; // fill in with appkey public static String appSecret = ""; // fill in with appSecret public DemoActivity() { } public DemoActivity Demo; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); CrashUtil crashUtil = CrashUtil.getInstance(); crashUtil.init(this); setContentView(R.layout.main); if (!initeSdk()) { this.finish(); return; } if (!initeActivity()) { this.finish(); return; } // m_oIPAddr.setText("10.17.132.49"); m_oIPAddr.setText("172.10.21.31"); m_oPort.setText("8000"); m_oUser.setText("admin"); m_oPsd.setText("a1111111"); } // @Override public void surfaceCreated(SurfaceHolder holder) { m_osurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT); Log.i(TAG, "surface is created" + m_iPort); if (-1 == m_iPort) { return; } Surface surface = holder.getSurface(); if (true == surface.isValid()) { if (false == Player.getInstance() .setVideoWindow(m_iPort, 0, holder)) { Log.e(TAG, "Player setVideoWindow failed!"); } } } // @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } // @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.i(TAG, "Player setVideoWindow release!" + m_iPort); if (-1 == m_iPort) { return; } if (true == holder.getSurface().isValid()) { if (false == Player.getInstance().setVideoWindow(m_iPort, 0, null)) { Log.e(TAG, "Player setVideoWindow failed!"); } } } @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("m_iPort", m_iPort); super.onSaveInstanceState(outState); Log.i(TAG, "onSaveInstanceState"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { m_iPort = savedInstanceState.getInt("m_iPort"); super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, "onRestoreInstanceState"); } /** * @fn initeSdk * @author zhuzhenlei * @brief SDK init * @param NULL * [in] * @param NULL * [out] * @return true - success;false - fail */ 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); return true; } // GUI init private boolean initeActivity() { findViews(); m_osurfaceView.getHolder().addCallback(this); setListeners(); return true; } private void ChangeSingleSurFace(boolean bSingle) { DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); for (int i = 0; i < 4; i++) { if (playView[i] == null) { playView[i] = new PlaySurfaceView(this); playView[i].setParam(metric.widthPixels); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); params.bottomMargin = playView[i].getM_iHeight() - (i / 2) * playView[i].getM_iHeight(); params.leftMargin = (i % 2) * playView[i].getM_iWidth(); params.gravity = Gravity.BOTTOM | Gravity.LEFT; addContentView(playView[i], params); playView[i].setVisibility(View.INVISIBLE); } } if (bSingle) { // ��·ֻ��ʾ����1 for (int i = 0; i < 4; ++i) { playView[i].setVisibility(View.INVISIBLE); } playView[0].setParam(metric.widthPixels * 2); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); params.bottomMargin = playView[3].getM_iHeight() - (3 / 2) * playView[3].getM_iHeight(); // params.bottomMargin = 0; params.leftMargin = 0; // params. params.gravity = Gravity.BOTTOM | Gravity.LEFT; playView[0].setLayoutParams(params); playView[0].setVisibility(View.VISIBLE); } else { for (int i = 0; i < 4; ++i) { playView[i].setVisibility(View.VISIBLE); } playView[0].setParam(metric.widthPixels); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); params.bottomMargin = playView[0].getM_iHeight() - (0 / 2) * playView[0].getM_iHeight(); params.leftMargin = (0 % 2) * playView[0].getM_iWidth(); params.gravity = Gravity.BOTTOM | Gravity.LEFT; playView[0].setLayoutParams(params); } } // get controller instance private void findViews() { m_oLoginBtn = (Button) findViewById(R.id.btn_Login); m_oPreviewBtn = (Button) findViewById(R.id.btn_Preview); m_oPlaybackBtn = (Button) findViewById(R.id.btn_Playback); m_oParamCfgBtn = (Button) findViewById(R.id.btn_ParamCfg); m_oCaptureBtn = (Button) findViewById(R.id.btn_Capture); m_oRecordBtn = (Button) findViewById(R.id.btn_Record); m_oTalkBtn = (Button) findViewById(R.id.btn_Talk); m_oPTZBtn = (Button) findViewById(R.id.btn_PTZ); m_oOtherBtn = (Button) findViewById(R.id.btn_OTHER); m_oIPAddr = (EditText) findViewById(R.id.EDT_IPAddr); m_oPort = (EditText) findViewById(R.id.EDT_Port); m_oUser = (EditText) findViewById(R.id.EDT_User); m_oPsd = (EditText) findViewById(R.id.EDT_Psd); m_osurfaceView = (SurfaceView) findViewById(R.id.Sur_Player); } // listen private void setListeners() { m_oLoginBtn.setOnClickListener(Login_Listener); m_oPreviewBtn.setOnClickListener(Preview_Listener); m_oPlaybackBtn.setOnClickListener(Playback_Listener); m_oParamCfgBtn.setOnClickListener(ParamCfg_Listener); m_oCaptureBtn.setOnClickListener(Capture_Listener); m_oRecordBtn.setOnClickListener(Record_Listener); m_oTalkBtn.setOnClickListener(Talk_Listener); m_oOtherBtn.setOnClickListener(OtherFunc_Listener); m_oPTZBtn.setOnTouchListener(PTZ_Listener); } // ptz listener private Button.OnTouchListener PTZ_Listener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { try { if (m_iLogID < 0) { Log.e(TAG, "please login on a device first"); return false; } if (event.getAction() == MotionEvent.ACTION_DOWN) { if (m_bPTZL == false) { if (!HCNetSDK.getInstance().NET_DVR_PTZControl_Other( m_iLogID, m_iStartChan, PTZCommand.PAN_LEFT, 0)) { Log.e(TAG, "start PAN_LEFT failed with error code: " + HCNetSDK.getInstance() .NET_DVR_GetLastError()); } else { Log.i(TAG, "start PAN_LEFT succ"); } } else { if (!HCNetSDK.getInstance() .NET_DVR_PTZControl_Other(m_iLogID, m_iStartChan, PTZCommand.PAN_RIGHT, 0)) { Log.e(TAG, "start PAN_RIGHT failed with error code: " + HCNetSDK.getInstance() .NET_DVR_GetLastError()); } else { Log.i(TAG, "start PAN_RIGHT succ"); } } } else if (event.getAction() == MotionEvent.ACTION_UP) { if (m_bPTZL == false) { if (!HCNetSDK.getInstance().NET_DVR_PTZControl_Other( m_iLogID, m_iStartChan, PTZCommand.PAN_LEFT, 1)) { Log.e(TAG, "stop PAN_LEFT failed with error code: " + HCNetSDK.getInstance() .NET_DVR_GetLastError()); } else { Log.i(TAG, "stop PAN_LEFT succ"); } m_bPTZL = true; m_oPTZBtn.setText("PTZ(R)"); } else { if (!HCNetSDK.getInstance() .NET_DVR_PTZControl_Other(m_iLogID, m_iStartChan, PTZCommand.PAN_RIGHT, 1)) { Log.e(TAG, "stop PAN_RIGHT failed with error code: " + HCNetSDK.getInstance() .NET_DVR_GetLastError()); } else { Log.i(TAG, "stop PAN_RIGHT succ"); } m_bPTZL = false; m_oPTZBtn.setText("PTZ(L)"); } } return true; } catch (Exception err) { Log.e(TAG, "error: " + err.toString()); return false; } } }; // preset listener private Button.OnClickListener OtherFunc_Listener = new OnClickListener() { public void onClick(View v) { // PTZTest.TEST_PTZ(m_iPlayID, m_iLogID, m_iStartChan); // ConfigTest.Test_ScreenConfig(m_iLogID, m_iStartChan); // PTZTest.TEST_PTZ(m_iPlayID, m_iLogID, m_iStartChan); /* * try { //PictureTest.PicUpload(m_iLogID); } catch * (InterruptedException e) { // TODO Auto-generated catch block * e.printStackTrace(); } */ //PictureTest.BaseMap(m_iLogID); // DecodeTest.PicPreview(m_iLogID); // ManageTest.TEST_Manage(m_iLogID); // AlarmTest.Test_SetupAlarm(m_iLogID); // OtherFunction.TEST_OtherFunc(m_iPlayID, m_iLogID, m_iStartChan); JNATest.TEST_Config(m_iPlayID, m_iLogID, m_iStartChan); // JNATest.TEST_EzvizConfig(m_iPlayID, m_iLogID, m_iStartChan); // ConfigTest.TEST_Config(m_iPlayID, m_iLogID, m_iStartChan); // HttpTest.Test_HTTP(); // ScreenTest.TEST_Screen(m_iLogID); // get_ddns_Info(appkey, appSecret); //get ddns info by using appkey and app secret // get_ddns_Info_HC(); } }; // Test Activity result public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode== REQUEST_CODE) { if (resultCode== 1 && data != null) { m_retUrl = data.getStringExtra("Info"); Log.e(TAG, "m_retUrl: " + m_retUrl); accessToken = m_retUrl.substring(m_retUrl.indexOf("access_token")+13, m_retUrl.indexOf("access_token")+77); Log.e(TAG, "accessToken: " + accessToken); areaDomain = m_retUrl.substring(m_retUrl.indexOf("areaDomain")+11); Log.e(TAG, "areaDomain: " + areaDomain); } else { Log.e(TAG, "resultCode!= 1"); } Demo = new DemoActivity(); new Thread(new Runnable() { //inner class - new thread to get device list @Override public void run() { Demo.get_device_ip(); } }).start(); } } // Talk listener private Button.OnClickListener Talk_Listener = new Button.OnClickListener() { public void onClick(View v) { try { if (m_bTalkOn == false) { if (VoiceTalk.startVoiceTalk(m_iLogID) >= 0) { m_bTalkOn = true; m_oTalkBtn.setText("Stop"); } } else { if (VoiceTalk.stopVoiceTalk()) { m_bTalkOn = false; m_oTalkBtn.setText("Talk"); } } } catch (Exception err) { Log.e(TAG, "error: " + err.toString()); } } }; // record listener private Button.OnClickListener Record_Listener = new Button.OnClickListener() { public void onClick(View v) { if (!m_bSaveRealData) { if (!HCNetSDKJNAInstance.getInstance().NET_DVR_SaveRealData_V30(m_iPlayID, 0x2, "/sdcard/test.mp4")) { System.out.println("NET_DVR_SaveRealData_V30 failed! error: " + HCNetSDK.getInstance().NET_DVR_GetLastError()); return; } else { System.out.println("NET_DVR_SaveRealData_V30 succ!"); } m_bSaveRealData = true; } else { if (!HCNetSDK.getInstance().NET_DVR_StopSaveRealData(m_iPlayID)) { System.out .println("NET_DVR_StopSaveRealData failed! error: " + HCNetSDK.getInstance() .NET_DVR_GetLastError()); } else { System.out.println("NET_DVR_StopSaveRealData succ!"); } m_bSaveRealData = false; } } }; // capture listener private Button.OnClickListener Capture_Listener = new Button.OnClickListener() { public void onClick(View v) { try { if(m_iPlayID < 0){ Log.e(TAG, "please start preview first"); return; }else{ // HCNetSDKJNAInstance.getInstance().NET_DVR_SetCapturePictureMode(0x1); if(HCNetSDKJNAInstance.getInstance().NET_DVR_CapturePictureBlock(m_iPlayID, "/sdcard/capblock.jpg", 0)){ Log.e(TAG, "NET_DVR_CapturePictureBlock Succ!"); } else { Log.e(TAG, "NET_DVR_CapturePictureBlock fail! Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); } } // if(m_iPlaybackID < 0){ // Log.e(TAG, "please start preview first"); // return; // } // else{ // if(HCNetSDKJNAInstance.getInstance().NET_DVR_PlayBackCaptureFile(m_iPlaybackID, "/sdcard/capfile.bmp")){ // Log.e(TAG, "NET_DVR_PlayBackCaptureFile succ"); // }else{ // Log.e(TAG, "NET_DVR_PlayBackCaptureFile fail " + HCNetSDK.getInstance().NET_DVR_GetLastError()); // } // } // if (m_iPort < 0) { // Log.e(TAG, "please start preview first"); // return; // } // Player.MPInteger stWidth = new Player.MPInteger(); // Player.MPInteger stHeight = new Player.MPInteger(); // if (!Player.getInstance().getPictureSize(m_iPort, stWidth, // stHeight)) { // Log.e(TAG, "getPictureSize failed with error code:" // + Player.getInstance().getLastError(m_iPort)); // return; // } // int nSize = 5 * stWidth.value * stHeight.value; // byte[] picBuf = new byte[nSize]; // Player.MPInteger stSize = new Player.MPInteger(); // if (!Player.getInstance() // .getBMP(m_iPort, picBuf, nSize, stSize)) { // Log.e(TAG, "getBMP failed with error code:" // + Player.getInstance().getLastError(m_iPort)); // return; // } // // SimpleDateFormat sDateFormat = new SimpleDateFormat( // "yyyy-MM-dd-hh:mm:ss"); // String date = sDateFormat.format(new java.util.Date()); // FileOutputStream file = new FileOutputStream("/mnt/sdcard/" // + date + ".bmp"); // file.write(picBuf, 0, stSize.value); // file.close(); } catch (Exception err) { Log.e(TAG, "error: " + err.toString()); } } }; private PlaybackCallBack getPlayerbackPlayerCbf() { PlaybackCallBack cbf = new PlaybackCallBack() { @Override public void fPlayDataCallBack(int iPlaybackHandle, int iDataType, byte[] pDataBuffer, int iDataSize) { // player channel 1 // PprocessRealData(1, iDataType, pDataBuffer, iDataSize, Player.STREAM_FILE); DemoActivity.this.processRealData(1, iDataType, pDataBuffer, iDataSize, Player.STREAM_REALTIME); } }; return cbf; } // playback listener private Button.OnClickListener Playback_Listener = new Button.OnClickListener() { public void onClick(View v) { try { if (m_iLogID < 0) { Log.e(TAG, "please login on a device first"); return; } if (m_iPlaybackID < 0) { if (m_iPlayID >= 0) { Log.i(TAG, "Please stop preview first"); return; } ChangeSingleSurFace(true); NET_DVR_TIME timeStart = new NET_DVR_TIME(); NET_DVR_TIME timeStop = new NET_DVR_TIME(); timeStart.dwYear = 2017; timeStart.dwMonth = 9; timeStart.dwDay = 24; timeStop.dwYear = 2017; timeStop.dwMonth = 11; timeStop.dwDay = 25; NET_DVR_VOD_PARA vodParma = new NET_DVR_VOD_PARA(); vodParma.struBeginTime = timeStart; vodParma.struEndTime = timeStop; vodParma.byStreamType = 0; vodParma.struIDInfo.dwChannel = m_iStartChan; vodParma.hWnd = playView[0].getHolder().getSurface(); // NET_DVR_PLAYCOND playcond = new NET_DVR_PLAYCOND(); // playcond.dwChannel = m_iStartChan; // playcond.struStartTime = timeStart; // playcond.struStopTime = timeStop; // playcond.byDrawFrame = 0; // playcond.byStreamType = 1; // m_iPlaybackID = HCNetSDK.getInstance().NET_DVR_PlayBackReverseByTime_V40(m_iLogID, playView[0].getHolder().getSurface(), playcond); m_iPlaybackID = HCNetSDK.getInstance().NET_DVR_PlayBackByTime_V40(m_iLogID, vodParma); // PlaybackCallBack fPlaybackDataCallBack = getPlayerbackPlayerCbf(); // if (fPlaybackDataCallBack == null) // { // Log.e(TAG, "fPlaybackDataCallBack object is failed!"); // return ; // } // // if(!HCNetSDK.getInstance().NET_DVR_SetPlayDataCallBack(m_iPlaybackID, fPlaybackDataCallBack)) // { // Log.e(TAG, "Set playback callback failed!"); // return ; // } if (m_iPlaybackID >= 0) { NET_DVR_PLAYBACK_INFO struPlaybackInfo = null; if (!HCNetSDK.getInstance().NET_DVR_PlayBackControl_V40(m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYSTART, null, 0, null)) { Log.e(TAG, "net sdk playback start failed!"); return; } m_bStopPlayback = false; m_oPlaybackBtn.setText("Stop"); // if(HCNetSDK.getInstance().NET_DVR_PlayBackControl_V40(m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYPAUSE, null, 0, struPlaybackInfo)) // { // Log.e(TAG, "NET_DVR_PlayBackControl_V40 pause succ"); // } // // if(HCNetSDK.getInstance().NET_DVR_PlayBackControl_V40(m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYRESTART, null, 0, struPlaybackInfo)) // { // Log.e(TAG, "NET_DVR_PLAYRESTART restart succ"); // } // // if(HCNetSDK.getInstance().NET_DVR_PlayBackControl_V40(m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYFAST, null, 0, struPlaybackInfo)) // { // Log.e(TAG, "NET_DVR_PLAYFAST succ"); // } // if(HCNetSDK.getInstance().NET_DVR_PlayBackControl_V40(m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYSLOW, null, 0, struPlaybackInfo)) // { // Log.e(TAG, "NET_DVR_PLAYSLOW succ"); // } // // if(HCNetSDK.getInstance().NET_DVR_PlayBackControl_V40(m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYSTARTAUDIO, null, 0, struPlaybackInfo)) // { // Log.e(TAG, "NET_DVR_PLAYSTARTAUDIO succ"); // } Thread thread = new Thread() { public void run() { int nProgress = -1; while (true) { nProgress = HCNetSDK.getInstance().NET_DVR_GetPlayBackPos(m_iPlaybackID); System.out.println("NET_DVR_GetPlayBackPos:" + nProgress); if (nProgress < 0 || nProgress >= 100) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; thread.start(); } else { Log.i(TAG,"NET_DVR_PlayBackByTime failed, error code: "+ HCNetSDK.getInstance().NET_DVR_GetLastError()); } } else { m_bStopPlayback = true; if (!HCNetSDK.getInstance().NET_DVR_StopPlayBack(m_iPlaybackID)) { Log.e(TAG, "net sdk stop playback failed"); } m_oPlaybackBtn.setText("Playback"); m_iPlaybackID = -1; ChangeSingleSurFace(false); } } catch (Exception err) { Log.e(TAG, "error: " + err.toString()); } } }; /* private Button.OnClickListener Playback_Listener = new Button.OnClickListener() { public void onClick(View v) { try { if (m_iLogID < 0) { Log.e(TAG, "please login on a device first"); return; } if (m_iPlaybackID < 0) { if (m_iPlayID >= 0) { Log.i(TAG, "Please stop preview first"); return; } ChangeSingleSurFace(true); m_iPlaybackID = HCNetSDK.getInstance() .NET_DVR_PlayBackByName(m_iLogID, new String("ch0001_00000000300000000"), playView[0].getHolder().getSurface()); if (m_iPlaybackID >= 0) { NET_DVR_PLAYBACK_INFO struPlaybackInfo = null; if (!HCNetSDK .getInstance() .NET_DVR_PlayBackControl_V40( m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYSTART, null, 0, struPlaybackInfo)) { Log.e(TAG, "net sdk playback start failed!"); return; } // HCNetSDK.getInstance().NET_DVR_PlayBackControl_V40(m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYPAUSE, null, 0, struPlaybackInfo); // Thread thread = new Thread() // { // public void run() // { // try // { // sleep(10*1000); // } catch (InterruptedException e) // { // e.printStackTrace(); // } // HCNetSDK.getInstance().NET_DVR_PlayBackControl_V40(m_iPlaybackID, PlaybackControlCommand.NET_DVR_PLAYRESTART, null, 0, null); // } // }; // thread.start(); // // m_bStopPlayback = false; // m_oPlaybackBtn.setText("Stop"); // // int iPlaybackIndex = HCNetSDKJNAInstance.getInstance().NET_DVR_GetPlayBackPlayerIndex(m_iPlaybackID); // if(iPlaybackIndex != -1){ // Log.e(TAG, "NET_DVR_GetPlayBackPlayerIndex Succ! iPlaybackIndex = %d" + iPlaybackIndex); // } // // if(HCNetSDKJNAInstance.getInstance().NET_DVR_PlayBackCaptureFile(m_iPlaybackID, "/mnt/sdcard/capture_02.dmp")){ // Log.e(TAG, "NET_DVR_PlayBackCaptureFile succ"); // } Thread thread11 = new Thread() { public void run() { int nProgress = -1; while (true) { nProgress = HCNetSDK.getInstance() .NET_DVR_GetPlayBackPos( m_iPlaybackID); System.out .println("NET_DVR_GetPlayBackPos:" + nProgress); if (nProgress < 0 || nProgress >= 100) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO // Auto-generated // catch // block e.printStackTrace(); } } } }; thread11.start(); } else { Log.i(TAG, "NET_DVR_PlayBackByName failed, error code: " + HCNetSDK.getInstance() .NET_DVR_GetLastError()); } } else { m_bStopPlayback = true; if (!HCNetSDK.getInstance().NET_DVR_StopPlayBack( m_iPlaybackID)) { Log.e(TAG, "net sdk stop playback failed"); } // player stop play m_oPlaybackBtn.setText("Playback"); m_iPlaybackID = -1; ChangeSingleSurFace(false); } } catch (Exception err) { Log.e(TAG, "error: " + err.toString()); } } }; */ // login listener private Button.OnClickListener Login_Listener = new Button.OnClickListener() { public void onClick(View v) { try { if (m_iLogID < 0) { // login on the device m_iLogID = loginDevice(); if (m_iLogID < 0) { Log.e(TAG, "This device logins failed!"); return; } else { System.out.println("m_iLogID=" + m_iLogID); } // get instance of exception callback and set ExceptionCallBack oexceptionCbf = getExceptiongCbf(); if (oexceptionCbf == null) { Log.e(TAG, "ExceptionCallBack object is failed!"); return; } if (!HCNetSDK.getInstance().NET_DVR_SetExceptionCallBack( oexceptionCbf)) { Log.e(TAG, "NET_DVR_SetExceptionCallBack is failed!"); return; } m_oLoginBtn.setText("Logout"); Log.i(TAG, "Login sucess ****************************1***************************"); } else { // whether we have logout if (!HCNetSDK.getInstance().NET_DVR_Logout_V30(m_iLogID)) { Log.e(TAG, " NET_DVR_Logout is failed!"); //if (!HCNetSDKJNAInstance.getInstance().NET_DVR_DeleteOpenEzvizUser(m_iLogID)) { // Log.e(TAG, " NET_DVR_DeleteOpenEzvizUser is failed!"); return; } m_oLoginBtn.setText("Login"); m_iLogID = -1; } }catch (Exception err) { Log.e(TAG, "error: " + err.toString()); } } }; private RealPlayCallBack getRealPlayerCbf() { RealPlayCallBack cbf = new RealPlayCallBack() { public void fRealDataCallBack(int iRealHandle, int iDataType, byte[] pDataBuffer, int iDataSize) { try{ FileOutputStream file = new FileOutputStream("/sdcard/RealPlay.mp4", true); file.write(pDataBuffer, 0, iDataSize); file.close(); }catch(Exception e) { e.printStackTrace(); } } }; return cbf; } // Preview listener private Button.OnClickListener Preview_Listener = new Button.OnClickListener() { public void onClick(View v) { try { ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(DemoActivity.this .getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); if (m_iLogID < 0) { Log.e(TAG, "please login on device first"); return; } if (m_iPlaybackID >= 0) { Log.i(TAG, "Please stop palyback first"); return; } if (m_bInsideDecode) { if (m_iChanNum > 1)// preview more than a channel { if (!m_bMultiPlay) { startMultiPreview(); // startMultiPreview(); m_bMultiPlay = true; m_oPreviewBtn.setText("Stop"); } else { stopMultiPreview(); m_bMultiPlay = false; m_oPreviewBtn.setText("Preview"); } } else // preivew a channel { if (m_iPlayID < 0) { startSinglePreview(); } else { stopSinglePreview(); m_oPreviewBtn.setText("Preview"); } } } else { if (m_iPlayID < 0) { if (m_iPlaybackID >= 0) { Log.i(TAG, "Please stop palyback first"); return; } / //NET_DVR_RealPlay_V40 callback // 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 = 1; // substream previewInfo.bBlocked = 1; m_iPlayID = HCNetSDK.getInstance().NET_DVR_RealPlay_V40(m_iLogID, previewInfo, null); if (m_iPlayID < 0) { Log.e(TAG, "NET_DVR_RealPlay is failed!Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); return; } Log.i(TAG, "NetSdk Play sucess ***********************3***************************"); m_oPreviewBtn.setText("Stop"); /// // real data call back // if(rdf == null) // { // rdf = new RealDataCallBack() // { // public void fRealDataCallBack(int iRealHandle, int iDataType, byte[] pDataBuffer, int iDataSize) // { // DemoActivity.this.processRealData(1, iDataType, pDataBuffer, iDataSize, Player.STREAM_REALTIME); // } // }; // } // // if(!HCNetSDK.getInstance().NET_DVR_SetRealDataCallBack(m_iPlayID, rdf)){ // Log.e(TAG, "NET_DVR_SetRealDataCallBack is failed!Err:" // + HCNetSDK.getInstance().NET_DVR_GetLastError()); // } // Log.i(TAG, // "NET_DVR_SetRealDataCallBack sucess ***************************************************"); / //std data call back if(cbf == null) { cbf = new StdDataCallBack() { public void fStdDataCallback(int iRealHandle, int iDataType, byte[] pDataBuffer, int iDataSize) { DemoActivity.this.processRealData(1, iDataType, pDataBuffer, iDataSize, Player.STREAM_REALTIME); } }; } if(!HCNetSDK.getInstance().NET_DVR_SetStandardDataCallBack(m_iPlayID, cbf)){ Log.e(TAG, "NET_DVR_SetStandardDataCallBack is failed!Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); } Log.i(TAG, "NET_DVR_SetStandardDataCallBack sucess ***************************************************"); /// }else{ stopSinglePreview(); m_oPreviewBtn.setText("Preview"); } } } catch (Exception err) { Log.e(TAG, "error: " + err.toString()); } } }; /** * @fn getStdDataPlayerCbf * @author * @brief get realplay callback instance * @param NULL * [in] * @param NULL * [out] * @return callback instance */ // private static StdDataCallBack cbf = null; // private StdDataCallBack getStdDataPlayerCbf() { // // StdDataCallBack cbf = new StdDataCallBack(){ // public void fStdDataCallback(int iRealHandle, int iDataType, // byte[] pDataBuffer, int iDataSize) { // DemoActivity.this.processRealData(1, iDataType, pDataBuffer, // iDataSize, Player.STREAM_REALTIME); // } // }; // return cbf; // }; /** * @fn processRealData * @author zhuzhenlei * @brief process real data * @param iPlayViewNo * - player channel [in] * @param iDataType * - data type [in] * @param pDataBuffer * - data buffer [in] * @param iDataSize * - data size [in] * @param iStreamMode * - stream mode [in] * @param NULL * [out] * @return NULL */ public void processRealData(int iPlayViewNo, int iDataType, byte[] pDataBuffer, int iDataSize, int iStreamMode) { if (HCNetSDK.NET_DVR_SYSHEAD == iDataType) { if (m_iPort >= 0) { return; } m_iPort = Player.getInstance().getPort(); if (m_iPort == -1) { Log.e(TAG, "getPort is failed with: " + Player.getInstance().getLastError(m_iPort)); return; } Log.i(TAG, "getPort succ with: " + m_iPort); if (iDataSize > 0) { if (!Player.getInstance().setStreamOpenMode(m_iPort, iStreamMode)) // set stream mode { Log.e(TAG, "setStreamOpenMode failed"); return; } if (!Player.getInstance().openStream(m_iPort, pDataBuffer, iDataSize, 2 * 1024 * 1024)) // open stream { Log.e(TAG, "openStream failed"); return; } if (!Player.getInstance().play(m_iPort, m_osurfaceView.getHolder())) { Log.e(TAG, "play failed"); return; } if (!Player.getInstance().playSound(m_iPort)) { Log.e(TAG, "playSound failed with error code:" + Player.getInstance().getLastError(m_iPort)); return; } } } else { try{ FileOutputStream file = new FileOutputStream("/sdcard/StdPlayData.mp4", true); file.write(pDataBuffer, 0, iDataSize); file.close(); }catch(Exception e) { e.printStackTrace(); } // if (!Player.getInstance().inputData(m_iPort, pDataBuffer, iDataSize)) // { // for (int i = 0; i < 4000 && m_iPlaybackID >= 0 && !m_bStopPlayback; i++) // { // if (Player.getInstance().inputData(m_iPort, pDataBuffer, iDataSize)) // { // break; // } // if (i % 100 == 0) { Log.e(TAG, "inputData failed with: " + Player.getInstance().getLastError(m_iPort) + ", i:" + i); } // // try { // Thread.sleep(10); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // // } // } // } } //} } // configuration listener private Button.OnClickListener ParamCfg_Listener = new Button.OnClickListener() { public void onClick(View v) { try { paramCfg(m_iLogID); } catch (Exception err) { Log.e(TAG, "error: " + err.toString()); } } }; private void startSinglePreview() { if (m_iPlaybackID >= 0) { Log.i(TAG, "Please stop palyback first"); 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; previewInfo.hHwnd = playView[0].getHolder(); m_iPlayID = HCNetSDK.getInstance().NET_DVR_RealPlay_V40(m_iLogID, previewInfo, null); if (m_iPlayID < 0) { Log.e(TAG, "NET_DVR_RealPlay is failed!Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); return; } // m_iPort = HCNetSDKJNAInstance.getInstance().NET_DVR_GetRealPlayerIndex(m_iPlayID); // if(m_iPort != -1) // { // Log.e(TAG, "NET_DVR_GetRealPlayerIndex Succ! iPlayIndex = " + m_iPort); // } // // Player.getInstance().getLastError(m_iPort); boolean bRet = HCNetSDKJNAInstance.getInstance().NET_DVR_OpenSound(m_iPlayID); if(bRet){ Log.e(TAG, "NET_DVR_OpenSound Succ!"); } // // if(HCNetSDKJNAInstance.getInstance().NET_DVR_CapturePicture(m_iPlayID, "/mnt/sdcard/capture_01.dmp")){ // Log.e(TAG, "NET_DVR_CapturePicture Succ!"); // } // else // { // Log.e(TAG, "NET_DVR_CapturePicture fail! Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); // } // // // short volume = 55; // if(HCNetSDKJNAInstance.getInstance().NET_DVR_Volume(m_iPlayID, volume)){ // Log.e(TAG, "NET_DVR_Volume Succ!"); // } Log.i(TAG, "NetSdk Play sucess ***********************3***************************"); m_oPreviewBtn.setText("Stop"); } private void startMultiPreview() { for (int i = 0; i < 4; i++) { playView[i].startPreview(m_iLogID, m_iStartChan + i); } // new Thread(new Runnable() { // // @Override // public void run() { // // TODO Auto-generated method stub // for (int i = 0; i < 4; i++) { // while (!playView[i].bCreate) { // try { // Thread.sleep(100); // Log.i(TAG, "wait for surface create"); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // // NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO(); // previewInfo.lChannel = m_iStartChan + i; // previewInfo.dwStreamType = 0; // substream // previewInfo.bBlocked = 1; // previewInfo.hHwnd = playView[i].getHolder(); // // playView[i].m_iPreviewHandle = // HCNetSDK.getInstance().NET_DVR_RealPlay_V40( // m_iLogID, previewInfo, null); // if (playView[i].m_iPreviewHandle < 0) { // Log.e(TAG, "NET_DVR_RealPlay is failed!Err:" // + HCNetSDK.getInstance().NET_DVR_GetLastError()); // } // } // } // }).start(); m_iPlayID = playView[0].m_iPreviewHandle; } private void stopMultiPreview() { int i = 0; for (i = 0; i < 4; i++) { playView[i].stopPreview(); } m_iPlayID = -1; } /** * @fn stopSinglePreview * @author zhuzhenlei * @brief stop preview * @param NULL * [in] * @param NULL * [out] * @return NULL */ private void stopSinglePreview() { if (m_iPlayID < 0) { Log.e(TAG, "m_iPlayID < 0"); return; } if(HCNetSDKJNAInstance.getInstance().NET_DVR_CloseSound()){ Log.e(TAG, "NET_DVR_CloseSound Succ!"); } // net sdk stop preview if (!HCNetSDK.getInstance().NET_DVR_StopRealPlay(m_iPlayID)) { Log.e(TAG, "StopRealPlay is failed!Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); return; } Log.i(TAG, "NET_DVR_StopRealPlay succ"); m_iPlayID = -1; } /** * @fn loginNormalDevice * @author zhuzhenlei * @brief login on device * @param NULL * [in] * @param NULL * [out] * @return login ID */ 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; } String strIP = m_oIPAddr.getText().toString(); int nPort = Integer.parseInt(m_oPort.getText().toString()); String strUser = m_oUser.getText().toString(); String strPsd = m_oPsd.getText().toString(); // call NET_DVR_Login_v30 to login on, port 8000 as default int iLogID = HCNetSDK.getInstance().NET_DVR_Login_V30(strIP, nPort, strUser, strPsd, 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; } if (m_iChanNum > 1) { ChangeSingleSurFace(false); } else { ChangeSingleSurFace(true); } Log.i(TAG, "NET_DVR_Login is Successful!"); return iLogID; } public static void Test_XMLAbility(int iUserID) { byte[] arrayOutBuf = new byte[64 * 1024]; INT_PTR intPtr = new INT_PTR(); String strInput = new String( ""); byte[] arrayInBuf = new byte[8 * 1024]; arrayInBuf = strInput.getBytes(); if (!HCNetSDK.getInstance().NET_DVR_GetXMLAbility(iUserID, HCNetSDK.DEVICE_ABILITY_INFO, arrayInBuf, strInput.length(), arrayOutBuf, 64 * 1024, intPtr)) { System.out.println("get DEVICE_ABILITY_INFO faild!" + " err: " + HCNetSDK.getInstance().NET_DVR_GetLastError()); } else { System.out.println("get DEVICE_ABILITY_INFO succ!"); } } /** * @fn loginEzvizDevice * @author liuyu6 * @brief login on ezviz device * @param NULL * [in] * @param NULL * [out] * @return login ID */ private int loginEzvizDevice() { return -1; /* * NET_DVR_OPEN_EZVIZ_USER_LOGIN_INFO struLoginInfo = new * NET_DVR_OPEN_EZVIZ_USER_LOGIN_INFO(); NET_DVR_DEVICEINFO_V30 * struDeviceInfo = new NET_DVR_DEVICEINFO_V30(); * * //String strInput = new String("pbsgp.p2papi.ezviz7.com"); String * strInput = new String("open.ys7.com"); //String strInput = new * String("pbdev.ys7.com"); //String strInput = new * String("183.136.184.67"); byte[] byInput = strInput.getBytes(); * System.arraycopy(byInput, 0, struLoginInfo.sEzvizServerAddress, 0, * byInput.length); * * struLoginInfo.wPort = 443; * * strInput = new * String("at.43anfq0q9k8zt06vd0ppalfhc4bj177p-3k4ovrh4vu-105zgp6-jgt8edqst" * ); byInput = strInput.getBytes(); System.arraycopy(byInput, 0, * struLoginInfo.sAccessToken, 0, byInput.length); * * //strInput = new String("67a7daedd4654dc5be329f2289914859"); * //byInput = strInput.getBytes(); //System.arraycopy(byInput, 0, * struLoginInfo.sSessionID, 0, byInput.length); * * //strInput = new String("ae1b9af9dcac4caeb88da6dbbf2dd8d5"); strInput * = new String("com.hik.visualintercom"); byInput = * strInput.getBytes(); System.arraycopy(byInput, 0, * struLoginInfo.sAppID, 0, byInput.length); * * //strInput = new String("78313dadecd92bd11623638d57aa5128"); strInput * = new String("226f102a99ad0e078504d380b9ddf760"); byInput = * strInput.getBytes(); System.arraycopy(byInput, 0, * struLoginInfo.sFeatureCode, 0, byInput.length); * * //strInput = new * String("https://pbopen.ys7.com:443/api/device/transmission"); * strInput = new String("/api/device/transmission"); byInput = * strInput.getBytes(); System.arraycopy(byInput, 0, struLoginInfo.sUrl, * 0, byInput.length); * * strInput = new String("520247131"); byInput = strInput.getBytes(); * System.arraycopy(byInput, 0, struLoginInfo.sDeviceID, 0, * byInput.length); * * strInput = new String("2"); byInput = strInput.getBytes(); * System.arraycopy(byInput, 0, struLoginInfo.sClientType, 0, * byInput.length); * * strInput = new String("UNKNOWN"); byInput = strInput.getBytes(); * System.arraycopy(byInput, 0, struLoginInfo.sNetType, 0, * byInput.length); * * strInput = new String("5.0.1"); byInput = strInput.getBytes(); * System.arraycopy(byInput, 0, struLoginInfo.sOsVersion, 0, * byInput.length); * * strInput = new String("v.5.1.5.30"); byInput = strInput.getBytes(); * System.arraycopy(byInput, 0, struLoginInfo.sSdkVersion, 0, * byInput.length); * * int iUserID = -1; * * iUserID = * HCNetSDK.getInstance().NET_DVR_CreateOpenEzvizUser(struLoginInfo, * struDeviceInfo); * * if (-1 == iUserID) { System.out.println("NET_DVR_CreateOpenEzvizUser" * + " err: " + HCNetSDK.getInstance().NET_DVR_GetLastError()); return * -1; } else { * System.out.println("NET_DVR_CreateOpenEzvizUser success"); } * * Test_XMLAbility(iUserID); Test_XMLAbility(iUserID); * Test_XMLAbility(iUserID); * * return iUserID; */ } /** * @fn loginDevice * @author zhangqing * @brief login on device * @param NULL * [in] * @param NULL * [out] * @return login ID */ private int loginDevice() { int iLogID = -1; iLogID = loginNormalDevice(); // iLogID = JNATest.TEST_EzvizLogin(); // iLogID = loginEzvizDevice(); return iLogID; } /** * @fn paramCfg * @author zhuzhenlei * @brief configuration * @param iUserID * - login ID [in] * @param NULL * [out] * @return NULL */ private void paramCfg(final int iUserID) { // whether have logined on if (iUserID < 0) { Log.e(TAG, "iUserID < 0"); return; } NET_DVR_COMPRESSIONCFG_V30 struCompress = new NET_DVR_COMPRESSIONCFG_V30(); if (!HCNetSDK.getInstance().NET_DVR_GetDVRConfig(iUserID, HCNetSDK.NET_DVR_GET_COMPRESSCFG_V30, m_iStartChan, struCompress)) { Log.e(TAG, "NET_DVR_GET_COMPRESSCFG_V30 failed with error code:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); } else { Log.i(TAG, "NET_DVR_GET_COMPRESSCFG_V30 succ"); } // set substream resolution to cif struCompress.struNetPara.byResolution = 1; if (!HCNetSDK.getInstance().NET_DVR_SetDVRConfig(iUserID, HCNetSDK.NET_DVR_SET_COMPRESSCFG_V30, m_iStartChan, struCompress)) { Log.e(TAG, "NET_DVR_SET_COMPRESSCFG_V30 failed with error code:" + HCNetSDK.getInstance().NET_DVR_GetLastError()); } else { Log.i(TAG, "NET_DVR_SET_COMPRESSCFG_V30 succ"); } } /** * @fn getExceptiongCbf * @author zhuzhenlei * @brief process exception * @param NULL * [in] * @param NULL * [out] * @return exception instance */ private ExceptionCallBack getExceptiongCbf() { ExceptionCallBack oExceptionCbf = new ExceptionCallBack() { public void fExceptionCallBack(int iType, int iUserID, int iHandle) { System.out.println("recv exception, type:" + iType); } }; return oExceptionCbf; } /** * @fn Cleanup * @author zhuzhenlei * @brief cleanup * @param NULL * [in] * @param NULL * [out] * @return NULL */ public void Cleanup() { // release net SDK resource HCNetSDK.getInstance().NET_DVR_Cleanup(); } public void get_access_token(String appKey,String appSecret) { Log.e(TAG, "get_access_token in" ); if(appKey == "" || appSecret == "") { Log.e(TAG, "appKey or appSecret is null"); return; } try { String url = "https://open.ezvizlife.com/api/lapp/token/get"; URL getDeviceUrl = new URL(url); /*Set Http Request Header*/ HttpURLConnection connection = (HttpURLConnection)getDeviceUrl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); connection.setRequestProperty("Host","isgpopen.ezvizlife.com"); connection.setDoInput(true); connection.setDoOutput(true); PrintWriter PostParam = new PrintWriter(connection.getOutputStream()); String sendParam = "appKey=" + appKey + "&appSecret=" + appSecret; PostParam.print(sendParam); PostParam.flush(); BufferedReader inBuf = new BufferedReader(new InputStreamReader(connection.getInputStream())); JSONObject RetValue = new JSONObject(new String(inBuf.readLine().getBytes(),"utf-8")); int RetCode = Integer.parseInt(RetValue.getString("code")); if(RetCode != 200) { Log.e(TAG, "Get DDNS Info fail! Err code: " + RetCode); return; }else{ JSONObject DetailInfo = RetValue.getJSONObject("data"); accessToken = DetailInfo.getString("accessToken"); Log.e(TAG, "accessToken: " + accessToken); areaDomain = DetailInfo.getString("areaDomain"); Log.e(TAG, "areaDomain: " + areaDomain); } }catch (Exception e) { e.printStackTrace(); } } public String getKey() { return appkey; } public String getSecret() { return appSecret; } void get_device_ip() { String deviceSerial = "711563208" /*m_oIPAddr.getText().toString()*/; //IP text instead of deviceSerial if(deviceSerial == null) { Log.e(TAG, "deviceSerial is null "); return; } try { String url = areaDomain + "/api/lapp/ddns/get"; URL getDeviceUrl = new URL(url); /*Set Http Request Header*/ HttpURLConnection connection = (HttpURLConnection)getDeviceUrl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); connection.setRequestProperty("Host","isgpopen.ezvizlife.com"); connection.setDoInput(true); connection.setDoOutput(true); PrintWriter PostParam = new PrintWriter(connection.getOutputStream()); String sendParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial; // String sendParam = "accessToken=" + accessToken + "&domain=" + areaDomain; System.out.println(sendParam); PostParam.print(sendParam); PostParam.flush(); BufferedReader inBuf = new BufferedReader(new InputStreamReader(connection.getInputStream())); JSONObject RetValue = new JSONObject(new String(inBuf.readLine().getBytes(),"utf-8")); Log.e(TAG, "RetValue = " + RetValue); return; }catch (Exception e) { e.printStackTrace(); } } public JSONObject get_ddns_Info(String appkey, String appSecret) { try{ if(m_retUrl != "") { Log.e(TAG, "m_retUrl != null "); accessToken = m_retUrl.substring(m_retUrl.indexOf("access_token")+13, m_retUrl.indexOf("access_token")+77); Log.e(TAG, "accessToken: " + accessToken); areaDomain = m_retUrl.substring(m_retUrl.indexOf("areaDomain")+11); Log.e(TAG, "areaDomain: " + areaDomain); }else{ Demo = new DemoActivity(); new Thread(new Runnable() { //inner class - new thread to get device list @Override public void run() { Demo.get_access_token(Demo.getKey(), Demo.getSecret()); Demo.get_device_ip(); } }).start(); } }catch (Exception e) { e.printStackTrace(); } return null; } public void get_ddns_Info_HC() { Intent intent = new Intent(DemoActivity.this, TestActivity.class); //skip to HC page startActivityForResult(intent, REQUEST_CODE); //get ddns info by using HC } }

这个真的看得是有点头大啊。仔细分析的话我们可以发现有两条重要的线索。

(1)网络设备线
onCreate->CameraLogin->loginDevice->loginNormalDevice->NET_DVR_Login_V30
(2)设备播放线(这里采用流模式)
previewButton.setOnClickListener->startSinglePreview->getRealPlayerCbf->(回调部分,会被内部的库反复调用)processRealData->

Player.getInstance().setStreamOpenMode(playPort,iStreamMode)
Player.getInstance().openStream(playPort, pDataBuffer,iDataSize, 2 * 1024 * 1024)
play(playPort,surface.getHolder()))//播放部分的核心,将播放端口与surface控件,联系了起来
Player.getInstance().playSound(playPort)
Player.getInstance().playSound(playPort)

【海康摄像头二次开发】移植海康摄像头Android SDK并实现多屏显示_第2张图片
那么这两条线又是靠什么串起来的呢。也就是说播放库,怎么知道要播放的设备是啥呢,这就涉及到下面这个核心函数,将播放与网络设备初始化串联了起来

this.m_iPreviewHandle = HCNetSDK.getInstance().NET_DVR_RealPlay_V40(iLogId, netDVRPreviewInfo, realPlayCallBack);

m_iPreviewHandle:是播放部分的线索变量
iLogId: 是初始化部分的线索变量

这就是主要的部分了

二、重要的函数分析

HCNetSDK

#5.24 NET_DVR_DEVICEINFO_V30: 设备信息
In class com.hikvision.netsdk.HCNetSDK
public class NET_DVR_DEVICEINFO_V30
{
public byte[] sSerialNumber = new byte[SERIALNO_LEN];
public byte byAlarmInPortNum;
public byte byAlarmOutPortNum;
public byte byDiskNum;
public byte byDVRType;
public byte byChanNum;
public byte byStartChan;
public byte byAudioChanNum;
public byte byIPChanNum;
public byte byZeroChanNum;
public short  wDevType;
public byte byStartDChan
public byte byHighDChanNum
}
#3.3.4  用户注册设备 NET_DVR_Login_V30
函 数:  public int NET_DVR_Login_V30(String sDvrIp, int iDvrPort, ava.lang.String sUserName, String
sPassword, NET_DVR_DEVICEINFO_V30 DeviceInfo)
参 数:  [in] sDvrIp
[in] iDvrPort
[in] sUserName
[in] sPassword
[out] DeviceInfo
设备 IP 地址或静态域名
设备端口号
登录的用户名
用户密码
设备信息,详见:NET_DVR_DEVICEINFO_V30
返回值:  -1 表示失败,其他值表示返回的用户 ID 值。该用户 ID 具有唯一性,后续对设备的操作都需要通
过此 ID 实现。接口返回失败请调用 NET_DVR_GetLastError 获取错误码,通过错误码判断出错原
因。
说 明:  In class com.hikvision.netsdk.HCNetSDK ,JNI  接口。SDK 注册设备新增支持静态域名的方式,即
可设置 sDVRIP="test.vicp.net"
#3.3.5  用户注销 NET_DVR_Logout_V30
函 数:  public boolean NET_DVR_Logout_V30 (int lUserID)
参 数:  [in]lUserID  用户 ID 号,NET_DVR_Login_V30 的返回值
返回值:  TRUE 表示成功,FALSE 表示失败。接口返回失败请调用 NET_DVR_GetLastError 获取错误码,通
过错误码判断出错原因。
说 明:  In class com.hikvision.netsdk.HCNetSDK ,JNI  接口。

PlayerSDK

5.56 NET_DVR_PREVIEWINFO: 预览参数
In class com.hikvision.netsdk.HCNetSDK
public class NET_DVR_PREVIEWINFO
{
public int lChannel;
public int dwStreamType;
public int dwLinkMode;
public int bBlocked;
public int bPassbackRecord;
public byte byPreviewMode;
public byte byProtoType;
public SurfaceHolder hHwnd;
}
Members
lChannel
通道号,目前设备模拟通道号从 1 开始,数字通道的起始通道号一般从 33 开始,具体取值在登录接口
返回
dwStreamType
码流类型:0-主码流,1-子码流,2-码流 33-虚拟码流,以此类推
dwLinkMode
连接方式:0- TCP 方式,1- UDP 方式,2- 多播方式,3- RTP 方式,4-RTP/RTSP,5-RSTP/HTTP
bBlocked
0- 非阻塞取流,1- 阻塞取流
bPassbackRecord
0-不启用录像回传,1-启用录像回传。ANR 断网补录功能,客户端和设备之间网络异常恢复之后自动将
前端数据同步过来,需要设备支持。
byPreviewMode
预览模式:0- 正常预览,1- 延迟预览
byProtoType
应用层取流协议:0- 私有协议,1- RTSP 协议
hHwnd
播放窗口的句柄,为 NULL 表示不解码显示
实时预览 NET_DVR_RealPlay_V40
函 数:  public int NET_DVR_RealPlay_V40(int lUserID, NET_DVR_PREVIEWINFO previewInfo, RealPlayCallBack
CallBack)
参 数:  [in] lUserID
[in] previewInfo
[in] CallBack
NET_DVR_Login_V30 的返回值
预览参数,包括码流类型、取流协议、通道号等,详见:
NET_DVR_PREVIEWINFO
码流数据回调函数
public interface RealPlayCallBack {
public void fRealDataCallBack(int iRealHandle, int iDataType, byte[] pDataBuffer, int iDataSize);
}
[out] iRealHandle
[out] iDataType
[out] pDataBuffer
[out] iDataSize
当前的预览句柄
数据类型
存放数据的缓冲区指针
缓冲区大小
表 3.4 码流数据类型
dwDataType  宏定义  宏定义值  含义
NET_DVR_SYSHEAD  1  系统头数据
NET_DVR_STREAMDATA  2  流数据(包括复合流或音视频分开的视频流数据)
NET_DVR_AUDIOSTREAMDATA  3  音频数据
返回值:  -1 表示失败,其他值作为 NET_DVR_StopRealPlay 等函数的句柄参数。接口返回失败请调用
NET_DVR_GetLastError 获取错误码,通过错误码判断出错原因。
说 明:  In class com.hikvision.netsdk.HCNetSDK ,JNI  接口。通过该接口设置实时流回调函数获取实时流
音视频数据,然后可以通过播放库进行解码显示。
5.3.1  设置流播放模式 setStreamOpenMode
函 数:
boolean setStreamOpenMode(int nPort, int nMode)
参 数: Int nPort
unsigned int nMode
STREAME_REALTIME
STREAME_FILE
播放通道号
流播放模式:STREAME_REALTIME、STREAME_FILE
此模式(默认)下, 会尽量保正实时性, 防止数据阻塞,而且数据
检查严格。适合播放网络实时数据,立刻解码
文件模式,适合用户将文件以流方式输入,按时间戳播放
返回值: 成功返回 true;失败返回 false
说 明:
若码流为 RTP 封装的,则需要注意正确送入 RTP 的文件头,以及数据包大小和
内容。
设置流播放模式,其中若设置为 STREAM_REALTIME 模式则表示尽量保证实时
性,防止数据阻塞;且数据检查严格;
设置为 STREAM_FILE 则表示按时间戳播放。
注 意:
STREAME_REALTIME 模式下 fast、slow 调用无效且不返回错误码。


函 数: boolean play(int nPort, SurfaceHolder holder)
参 数: int nPort
SurfaceHolder holder
播放通道号
显示窗口
返回值: 成功返回 true;失败返回 false
说 明:
播放开始,播放视频画面大小将根据 holder 窗口调整,要全屏显示,只要把 holder
窗口放大到全屏。在 play 之前需要 holder 创建成功。
如果已经播放,重置当前播放速度为正常速度。
注 意:
I 帧大于 2MB 时可能无法解析

三、移植

1.引入库

SDK中涉及的库主要有两部分,JNI的.so库和Jar库

【海康摄像头二次开发】移植海康摄像头Android SDK并实现多屏显示_第3张图片拷贝到项目中

3.写一个设备信息管理的类

package com.example.robotremote.HIKIVideo;

/**
 * @author yueyang
 * @mail [email protected]
 * @date 2020/10/16 下午3:08
 */
public class Videoinfo{

    public Videoinfo(String ip ,int port , String userName ,String password)
    {
        this.ip=ip;
        this.password=password;
        this.port=port;
        this.userName=userName;
    }

    public Videoinfo()
    {
    }

    private String ip = "192.168.1.10";

    private int port=8081;

    private String userName = "admin";

    private String password = "iris2020";

    private String channel = "";

    private String cameraName = "";

    private String desc = "";

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getCameraName() {
        return cameraName;
    }

    public void setCameraName(String cameraName) {
        this.cameraName = cameraName;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "VideoInfo{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", channel='" + channel + '\'' +
                ", cameraName='" + cameraName + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }

}

4.设备登录的类

package com.example.robotremote.HIKIVideo;


import android.util.Log;

import com.hikvision.netsdk.ExceptionCallBack;
import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.NET_DVR_DEVICEINFO_V30;

public class HIKILogin {

    /**
     * TAG OF THIS ACTIVITY
     */
    private final String TAG = "HIKILOGIN";
    /**
     * MESSAGE OF IP CAMERA
     */
    private int startChannel = 0; // start channel no
    private int channelNumber = 0; // channel number
    private Videoinfo videoinfo;
    private NET_DVR_DEVICEINFO_V30 m_oNetDvrDeviceInfoV30 = null;
    private int loginID = -1; // return by NET_DVR_Login_v30


    public int getStartChannel()
    {
        return startChannel;
    }
    public int getLoginID()
    {
        return  loginID;
    }
    /**
     * @param
     */
    public void setVideoinfo(Videoinfo vid) {
        videoinfo=vid;
    }

    /*********************DEVICE TO LOGIN*************************/
    /**onCreate->CameraLogin->loginDevice->loginNormalDevice->NET_DVR_Login_V30
     /**
     * @return
     */
    public boolean CameraLogin(Videoinfo vid) {
        try {
            if (loginID < 0) {
                setVideoinfo( vid);
                loginID = loginDevice();
                if (loginID < 0) {
                    Log.e(TAG, "This device logins failed!");
                    return false;
                } else {
                    System.out.println("loginID=" + loginID);
                }
                // get instance of exception callback and set
                ExceptionCallBack oexceptionCbf = MethodUtils.getInstance().getExceptiongCbf();
                if (oexceptionCbf == null) {
                    Log.e(TAG, "ExceptionCallBack object is failed!");
                    return false;
                }

                if (!HCNetSDK.getInstance().NET_DVR_SetExceptionCallBack(
                        oexceptionCbf)) {
                    Log.e(TAG, "NET_DVR_SetExceptionCallBack is failed!");
                    return false;
                }
                Log.i(TAG, "Login sucess");
            } else {
                // whether we have logout
                if (!HCNetSDK.getInstance().NET_DVR_Logout_V30(loginID)) {
                    Log.e(TAG, " NET_DVR_Logout is failed!");
                    return false;
                }
                loginID = -1;
            }
        } catch (
                Exception err) {
            Log.e(TAG, "error: " + err.toString());
        }
        return  true;
    }
    /**
     * @author yueyang
     * @mail [email protected]
     * @date 2020/10/16 下午3:08
     */
    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;
        }
        String strIP = videoinfo.getIp();
        int nPort = videoinfo.getPort();
        String strUser = videoinfo.getUserName();
        String strPsd = videoinfo.getPassword();
        Log.d(TAG,strIP);
        Log.d(TAG,Integer.toString(nPort));
        Log.d(TAG,strUser);
        Log.d(TAG,strPsd);

        int iLogID = HCNetSDK.getInstance().NET_DVR_Login_V30(strIP, nPort,
                strUser, strPsd, 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) {
            startChannel = m_oNetDvrDeviceInfoV30.byStartChan;
            channelNumber = m_oNetDvrDeviceInfoV30.byChanNum;
        } else if (m_oNetDvrDeviceInfoV30.byIPChanNum > 0) {
            startChannel = m_oNetDvrDeviceInfoV30.byStartDChan;
            channelNumber = m_oNetDvrDeviceInfoV30.byIPChanNum
                    + m_oNetDvrDeviceInfoV30.byHighDChanNum * 256;
        }
        Log.i(TAG, "NET_DVR_Login is Successful!");

        return iLogID;
    }



    private int loginDevice() {
        int iLogID = -1;

        iLogID = loginNormalDevice();

        return iLogID;
    }

}

5.视频显示的控件

这里将所有与播放有关的内容单独写成一个类

package com.example.robotremote.HIKIVideo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.NET_DVR_PREVIEWINFO;
import com.hikvision.netsdk.RealPlayCallBack;

import org.MediaPlayer.PlayM4.Player;

public class PlaySurfaceview  extends SurfaceView implements SurfaceHolder.Callback {

    private final String TAG = "PlaySurfaceView";
    private boolean m_bSurfaceCreated = false;
    private int m_iHeight = 0;
    private int m_iPort = -1;
    public int m_iPreviewHandle = -1;
    private int m_iWidth = 0;
    private int SrceenW,SrceenH;
    private SurfaceHolder sfh;
    private Paint paint;
    private Canvas canvas;

    public PlaySurfaceview(Context context, AttributeSet attrs)
    {
        super(context,attrs);
        sfh =this.getHolder();
        sfh.addCallback(this);
        paint =new Paint();
        paint.setColor(Color.BLACK);

    }


    public void setParam(int screenWidth, int screenHeight) {
        this.m_iWidth = (screenWidth / 2);
        this.m_iHeight = (3 * this.m_iWidth / 4);
    }

    public void setViewSize(int width, int height) {
        this.m_iWidth = width;
        this.m_iHeight = height;
    }

    public int getCurHeight() {
        return this.m_iHeight;
    }

    public int getCurWidth() {
        return this.m_iWidth;
    }


    /**
     * 获取实时流音视频数据
     * @return  码流数据回调函数
     */
    private RealPlayCallBack getRealPlayerCbf() {
        return new RealPlayCallBack() {
            public void fRealDataCallBack(int paramAnonymousInt1, int paramAnonymousInt2, byte[] paramAnonymousArrayOfByte, int paramAnonymousInt3) {
                PlaySurfaceview.this.processRealData(1, paramAnonymousInt2, paramAnonymousArrayOfByte, paramAnonymousInt3, 0);
            }
        };
    }
    /**
     * 通过播放库解码显示到SurfaceView
     *
     * @param iPlayViewNo   当前的预览句柄
     * @param iDataType     数据类型
     * @param pDataBuffer   存放数据的缓冲区指针
     * @param iDataSize     缓冲区大小
     * @param iStreamMode   实时流模式
     */
    private void processRealData(int iPlayViewNo, int iDataType, byte[] pDataBuffer, int iDataSize, int iStreamMode) {
        if (HCNetSDK.NET_DVR_SYSHEAD == iDataType) {
            if (m_iPort >= 0) {
                return;
            }
            m_iPort = Player.getInstance().getPort();
            if (m_iPort == -1) {
                Log.e(TAG, "getPort is failed with: " + Player.getInstance().getLastError(m_iPort));
                return;
            }
            Log.i(TAG, "getPort succ with: " + m_iPort);
            if (iDataSize > 0) {
                if (!Player.getInstance().setStreamOpenMode(m_iPort, iStreamMode)) //set stream mode
                {
                    Log.e(TAG, "setStreamOpenMode failed");
                    return;
                }
                if (!Player.getInstance().openStream(m_iPort, pDataBuffer, iDataSize, 2 * 1024 * 1024)) //open stream
                {
                    Log.e(TAG, "openStream failed");
                    return;
                }
                while (!m_bSurfaceCreated) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Log.i(TAG, "wait 100 for surface, handle:" + iPlayViewNo);
                }

                if (!Player.getInstance().play(m_iPort, getHolder())) {
                    Log.e(TAG, "play failed,error:" + Player.getInstance().getLastError(m_iPort));
                    return;
                }
                if (!Player.getInstance().playSound(m_iPort)) {
                    Log.e(TAG, "playSound failed with error code:" + Player.getInstance().getLastError(m_iPort));
                    return;
                }
            }
        } else {
            if (!Player.getInstance().inputData(m_iPort, pDataBuffer, iDataSize)) {
                Log.e(TAG, "inputData failed with: " + Player.getInstance().getLastError(m_iPort));
            }
        }
    }

    private void stopPlayer() {
        Player.getInstance().stopSound();
        if (!Player.getInstance().stop(this.m_iPort)) {
            Log.e(TAG, "stop is failed!");
            return;
        }
        if (!Player.getInstance().closeStream(this.m_iPort)) {
            Log.e(TAG, "closeStream is failed!");
            return;
        }
        if (Player.getInstance().freePort(this.m_iPort)) {
            this.m_iPort = -1;
            return;
        }
        Log.e(TAG, "freePort is failed!" + this.m_iPort);
    }

    public void startPreview(int iLogId, int chanNum) {
        RealPlayCallBack realPlayCallBack = getRealPlayerCbf();
        if (realPlayCallBack == null) {
            Log.e(TAG, "fRealDataCallBack object is failed!");
            return;
        }
        Log.i(TAG, "preview channel:" + chanNum);
        NET_DVR_PREVIEWINFO netDVRPreviewInfo = new NET_DVR_PREVIEWINFO();
        // 通道号,模拟通道号从1开始,数字通道号从33开始,具体取值在登录接口返回
        netDVRPreviewInfo.lChannel = chanNum;
        // 码流类型
        netDVRPreviewInfo.dwStreamType = 1;
        // 连接方式,0-TCP方式,1-UDP方式,2-多播方式,3-RTP方式,4-RTP/RTSP,5-RSTP/HTTP
        // previewInfo.dwLinkMode = 5;
        // 0-非阻塞取流,1-阻塞取流
        netDVRPreviewInfo.bBlocked = 1;
        // 实时预览,返回值-1表示失败
        this.m_iPreviewHandle = HCNetSDK.getInstance().NET_DVR_RealPlay_V40(iLogId, netDVRPreviewInfo, realPlayCallBack);

        if (m_iPreviewHandle < 0) {
            Log.e(TAG, "NET_DVR_RealPlay is failed!Err:" + HCNetSDK.getInstance().NET_DVR_GetLastError());
        }
    }

    public void stopPreview() {
        HCNetSDK.getInstance().NET_DVR_StopRealPlay(this.m_iPreviewHandle);
        stopPlayer();
    }

    //注意:线程的初始化和启动需要放在同一个函数中,否则当退出后再次start这个时候就会抛出异常
    public void surfaceCreated(SurfaceHolder holder)
    {
        this.m_bSurfaceCreated = true;
        this.SrceenH=this.getHeight();
        this.SrceenW=this.getWidth();

        Surface surface = holder.getSurface();
        if (true == surface.isValid()) {
            if (false == Player.getInstance().setVideoWindow(m_iPort, 0, holder)) {
                Log.e(TAG, "Player setVideoWindow failed!");
            }
        }

    }
    public void surfaceChanged(SurfaceHolder holder,int format,int width,int height)
    {
    }
    public  void surfaceDestroyed(SurfaceHolder holder)
    {
        // TODO Auto-generated method stub
        m_bSurfaceCreated = false;
        if (-1 == m_iPort) {
            return;
        }
        if (true == holder.getSurface().isValid()) {
            if (false == Player.getInstance().setVideoWindow(m_iPort, 0, null)) {
                Log.e(TAG, "Player setVideoWindow failed!");
            }
        }
    }


}



6、通用的工具类

package com.example.robotremote.HIKIVideo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.hikvision.netsdk.ExceptionCallBack;
import com.hikvision.netsdk.HCNetSDK;

public class MethodUtils {

    private Context context = null;

    private static MethodUtils mInstance = null;

    public MethodUtils(){

    }

    public static synchronized MethodUtils getInstance(){

        if (mInstance == null){
            mInstance = new MethodUtils();
        }

        return mInstance;
    }

    /**
     * 初始化海康视频SDK
     *
     * @return
     */
    public boolean initHCNetSDK() {
        if (!HCNetSDK.getInstance().NET_DVR_Init()) {
            return false;
        }
        HCNetSDK.getInstance().NET_DVR_SetLogToFile(3, "/mnt/sdcard/sdklog/", true);

        return true;
    }


    /**
     * 退出Activity,设置返回值
     *
     * @param activity  待退出activity
     * @param resultCode    返回码
     * @param msg   返回信息
     */
    public void quitActivity(Activity activity,int resultCode, String msg){
        Intent intent = new Intent();
        intent.putExtra("result",msg);
        activity.setResult(resultCode,intent);
        activity.finish();
    }

    /**
     * 获取异常回调
     *
     * @return  异常回调
     */
    public ExceptionCallBack getExceptiongCbf() {
        return new ExceptionCallBack() {
            public void fExceptionCallBack(int paramAnonymousInt1, int paramAnonymousInt2, int paramAnonymousInt3) {
                Log.e("ExceptionCallBack", "recv exception, type:" + paramAnonymousInt1);
            }
        };
    }

    /**
     * 获取连接错误信息
     * 当前只例举了常见错误码对应错误信息,更多错误码信息详见《设备网络编程指南(Android)》第4章
     *
     * @param errorCode 错误码
     * @return
     */
    public String getNETDVRErrorMsg(int errorCode){
        String errorMsg = "";
        switch (errorCode){
            case 1:
                errorMsg = "用户名或密码错误";
                break;
            case 2:
                errorMsg = "无当前设备操作权限";
                break;
            case 3:
                errorMsg = "SDK未初始化";
                break;
            case 4:
                errorMsg = "通道号错误";
                break;
            case 5:
                errorMsg = "连接到设备的用户数超过最大";
                break;
            case 7:
                errorMsg = "连接设备失败";
                break;
            case 11:
                errorMsg = "传送的数据有误";
                break;
            case 13:
                errorMsg = "无此权限";
                break;
            default:
                errorMsg = "错误码" + errorCode;
                break;
        }
        return errorMsg;
    }

}

7、使用

package com.example.robotremote;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import com.example.robotremote.HIKIVideo.HIKILogin;
import com.example.robotremote.HIKIVideo.MethodUtils;
import com.example.robotremote.HIKIVideo.PlaySurfaceview;
import com.example.robotremote.HIKIVideo.Videoinfo;
import com.example.robotremote.Key.KeyCol;
import com.example.robotremote.Led.LedCol;
import com.example.robotremote.Serial.SerialCol;
import com.hikvision.netsdk.ExceptionCallBack;
import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.NET_DVR_DEVICEINFO_V30;


/**
 * @author yueyang
 * @version V1.0
 * @modificationHistory
 */
public class MainActivity extends Activity {

    /**
     * TAG OF THIS ACTIVITY
     */
    private final String TAG = "DemoActivity";
    /**
     * HARD AND HARD PROCESS THREAD
     */
    private SerialCol serialCol;
    private Thread serialth;
    private LedCol ledCol;
    private Thread ledth;
    private KeyCol keyCol;
    private Thread keyth;

    /**
     * VIEW OF WINDOW
     */
    private PlaySurfaceview surface1 = null;
    private PlaySurfaceview surface2 = null;
    private GestureDetector mDetector1;
    private GestureDetector mDetector2;

    /**
     * FLAG TO JUDGE WHICH SURFACE
     */
    private boolean isSurface1;
    private boolean isSurface2;
    private HIKILogin login[]=new HIKILogin[2];

    private Videoinfo[] vid=new Videoinfo[2];

    /**
     * NET PART
     */
    private int playID = -1; // return by NET_DVR_RealPlay_V30
    private int playPort = -1; // play port


    /**
     * PLAYER PART
     */
    private int playbackID = -1; // return by NET_DVR_PlayBackByTime


    public void setStyle() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置为横屏
    }

    public void HardInit() {

        serialCol = new SerialCol("/dev/ttySAC0", 115200);
        serialth = new Thread(serialCol);
        serialth.start();
        ledCol = new LedCol();
        ledth = new Thread(ledCol);
        ledth.start();
//        keyCol=new KeyCol();
//        keyth=new Thread(keyCol);
//        keyth.start();
    }
    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);
        return true;
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle();
        HardInit();
        setContentView(R.layout.activity_main);

        if (!initeSdk()) {

        }

        mDetector1 = new GestureDetector(this, new MyGestureListener());
        mDetector2 = new GestureDetector(this, new MyGestureListener());


        surface1 = (PlaySurfaceview) findViewById(R.id.surface1);
        surface2 = (PlaySurfaceview) findViewById(R.id.surface2);


        /*Set VideoInfo*/
        vid[0]=new Videoinfo("192.168.1.64",8000,"admin","iris2020");
        vid[1]=new Videoinfo("192.168.1.65",8000,"admin","root1234");
        login[0]=new HIKILogin();
        login[1]=new HIKILogin();
        login[0].CameraLogin(vid[0]);
        login[1].CameraLogin(vid[1]);

        surface1.startPreview(login[0].getLoginID(),login[0].getStartChannel());
        surface2.startPreview(login[1].getLoginID(),login[1].getStartChannel());

        //SurfaceView1实现双击与滑动效果
        surface1.setLongClickable(true);
        surface1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isSurface1 = true;
                mDetector1.onTouchEvent(event);
                isSurface1 = false;
                return true;
            }
        });

        //SurfaceView2实现双击与滑动效果
        surface2.setLongClickable(true);
        surface2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isSurface2 = true;
                mDetector2.onTouchEvent(event);
                isSurface2 = false;
                return true;
            }
        });
    }


    //手势处理监听器
    private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            //移动速度大于1000时判定为滑动
            if (velocityX < -1000 && isSurface1) {
                surface2.setVisibility(View.VISIBLE);
            } else if (velocityX > 1000 && isSurface2) {
                surface1.setVisibility(View.VISIBLE);
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            //实现surfaceview的单独显示
            if (isSurface1) {
                surface2.setVisibility(View.GONE);
            } else if (isSurface2) {
                surface1.setVisibility(View.GONE);
            }
            return super.onDoubleTap(e);
        }
    }

}

效果展示

双击全屏显示
【海康摄像头二次开发】移植海康摄像头Android SDK并实现多屏显示_第4张图片
滑动还原
【海康摄像头二次开发】移植海康摄像头Android SDK并实现多屏显示_第5张图片

源码下载:
海康摄像头二次开发

你可能感兴趣的:(Android开发,java,android,海康摄像头)