更新一个抓图功能代码见最底部 目前海康威视官网上并没有提供Android的API和SKD所以下面的SDK是反编译后得到的。 效果图“现在”截不了,不是我不给图。公司上面有人在调试硬件设备 1.先了解下大概的 部署图 都属于(直连的性质): 自己随便画的有点丑。。。别喷 2.也可以部署某个服务器到互联网上、使用手持设备(android)通过联网直接远程控制摄像头等等是可行的、这就看亲们自己的想想力了。 3.此文用于交流不涉及商业信息、如有雷同不胜荣幸。 4.现在Demo已经实现的功能: 4.1.简单的八个方向控制 5.正在完善的功能: 5.1.抓图、录像、远程关机重启、多屏展示等等后续有完善的功能继续更新 下面咱们开始看代码: 1、MonitorCameraInfo.java(监控点信息类) 2、SjrsSurfaceView.java(继承自SurfaceView,用来播放视频并显示) 3、VideoShowActivity(主activity) 4、清单文件中一定要加上网络访问的权限 android.permission.INTERNET 5、获取视频展示有两种,一种是文件模式,一种是流模式(我用的是流模式) 6、下面是工程结构图,需要完整的可以下Demo研究,咱们可以互相交流
按照官网提供的调用顺序如下:
配置好登陆需要的相关信息:
- public class VideoShowActivity extends Activity {
- /** SurfaceView对象,用来显示视频 */
- private SjrsSurfaceView nowSjrsSurfaceView;
- /** 视频向上 */
- private Button btUp;
- /** 视频向下 */
- private Button btDown;
- /** 视频向左 */
- private Button btLeft;
- /** 视频向右 */
- private Button btRigth;
- /** 视频上左 */
- private Button btUpLeft;
- /** 视频上右 */
- private Button btUpRigth;
- /** 视频下左 */
- private Button btDownLeft;
- /** 视频下右 */
- private Button btDownRigth;
- /** button点击事件*/
- private ButtonListener btnListener;
- /** 实例化网络库SDK*/
- private SjrsSurfaceView mSurface;
- /** 监控点信息类 */
- private MonitorCameraInfo cameraInfo;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findView();
- setListener();
- init();
- }
- /**
- * 组件配置
- */
- private void findView() {
- // TODO Auto-generated method stub
- nowSjrsSurfaceView = (SjrsSurfaceView) findViewById(R.id.video);
- btUp = (Button) findViewById(R.id.bt_up);
- btDown = (Button) findViewById(R.id.bt_down);
- btLeft = (Button) findViewById(R.id.bt_left);
- btRigth = (Button) findViewById(R.id.bt_rigth);
- btUpLeft = (Button) findViewById(R.id.bt_up_left);
- btUpRigth = (Button) findViewById(R.id.bt_up_rigth);
- btDownLeft = (Button) findViewById(R.id.bt_down_left);
- btDownRigth = (Button) findViewById(R.id.bt_down_rigth);
- btnListener = new ButtonListener();
- }
- /**
- * 监听设置
- */
- private void setListener() {
- // TODO Auto-generated method stub
- btUp.setOnClickListener(btnListener);
- btDown.setOnClickListener(btnListener);
- btLeft.setOnClickListener(btnListener);
- btRigth.setOnClickListener(btnListener);
- btUp.setOnTouchListener(btnListener);
- btDown.setOnTouchListener(btnListener);
- btLeft.setOnTouchListener(btnListener);
- btRigth.setOnTouchListener(btnListener);
- btUpLeft.setOnClickListener(btnListener);
- btUpRigth.setOnClickListener(btnListener);
- btDownLeft.setOnClickListener(btnListener);
- btDownRigth.setOnClickListener(btnListener);
- btUpLeft.setOnTouchListener(btnListener);
- btUpRigth.setOnTouchListener(btnListener);
- btDownLeft.setOnTouchListener(btnListener);
- btDownRigth.setOnTouchListener(btnListener);
- }
- /**
- * 页面初始化
- */
- private void init() {
- // TODO Auto-generated method stub
- mSurface = new SjrsSurfaceView(VideoShowActivity.this);
- }
- /**
- * 显示
- */
- protected void onResume() {
- super.onResume();
- // 如果没有在播放的话
- if (!nowSjrsSurfaceView.playFlag) {
- // 监控点信息类
- cameraInfo = new MonitorCameraInfo();
- //224.186.114.116
- cameraInfo.serverip = "192.168.1.206";
- cameraInfo.serverport = 8000;
- cameraInfo.username = "admin";
- cameraInfo.userpwd = "12345";
- cameraInfo.channel = 2;
- cameraInfo.describe = "测试点";
- nowSjrsSurfaceView.setMonitorInfo(cameraInfo);
- // 开始实时预览
- nowSjrsSurfaceView.startPlay();
- }
- }
- /**
- * 暂停
- */
- protected void onPause() {
- super.onPause();
- if (nowSjrsSurfaceView.playFlag) {
- nowSjrsSurfaceView.stopPlay(); // 停止实时预览
- }
- }
- /**
- * 方向按键监听
- * 注意:此处的通道号参数 实质为:2 但必须指定为:1(主通道)才可以做控制
- */
- public class ButtonListener implements OnTouchListener,OnClickListener {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- switch (v.getId()) {
- case R.id.bt_up:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,21,0);
- System.out.println("向上");
- break;
- case R.id.bt_down:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,22,0);
- System.out.println("向下");
- break;
- case R.id.bt_left:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,23,0);
- System.out.println("向左");
- break;
- case R.id.bt_rigth:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,24,0);
- System.out.println("向右");
- break;
- case R.id.bt_up_left:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,25,0);
- System.out.println("上左");
- break;
- case R.id.bt_up_rigth:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,26,0);
- System.out.println("上右");
- break;
- case R.id.bt_down_left:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,27,0);
- System.out.println("下左");
- break;
- case R.id.bt_down_rigth:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,28,0);
- System.out.println("下右");
- break;
- /*case R.id.bt_amplification:
- boolean iss = mSurface.SjrsSurface().NET_DVR_PTZControlWithSpeed(cameraInfo.playNum,15,0,3);
- System.out.println("异常:"+mSurface.SjrsSurface().NET_DVR_GetLastError());
- System.out.println("焦距放大"+iss);
- break;
- case R.id.bt_shrink:
- boolean is = mSurface.SjrsSurface().NET_DVR_PTZControlWithSpeed(cameraInfo.playNum,16,0,3);
- System.out.println("焦距缩小"+is);
- break;*/
- default:
- break;
- }
- return false;
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.bt_up:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,21,1);
- System.out.println("结束向上移动");
- break;
- case R.id.bt_down:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,22,1);
- System.out.println("结束向下移动");
- break;
- case R.id.bt_left:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,23,1);
- System.out.println("结束向左移动");
- break;
- case R.id.bt_rigth:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,24,1);
- System.out.println("结束向右移动");
- break;
- case R.id.bt_up_left:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,25,1);
- System.out.println("结束上左移动");
- break;
- case R.id.bt_up_rigth:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,26,1);
- System.out.println("结束上右移动");
- break;
- case R.id.bt_down_left:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,27,1);
- System.out.println("结束下左移动");
- break;
- case R.id.bt_down_rigth:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(cameraInfo.userId,1,28,1);
- System.out.println("结束下右移动");
- break;
- /*case R.id.bt_amplification:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(0,1,13,1);
- System.out.println("结束焦距放大");
- break;
- case R.id.bt_shrink:
- mSurface.SjrsSurface().NET_DVR_PTZControl_Other(0,1,14,1);
- System.out.println("结束焦距缩小");
- break;*/
- default:
- break;
- }
- }
- }
- }
- public class MonitorCameraInfo {
- public String serverip = "";
- public int serverport = 0;
- public String username = "";
- public String userpwd = "";
- public int channel = 0;
- public String describe = "";
- /**登录帐号id */
- public int userId = 0;
- /**播放返回值 */
- public int playNum = 0;
- /**抓图存放路劲 */
- public String filepath = getSDRootPath()+"/HCNetSDK";
- public MonitorCameraInfo() {}
初始化SDK登陆&&播放画面:
package com.HCNetSDK.Player;
- /**
- * SurfaceView,用来播放视频并显示
- * 在要显示视频的SurfaceView对象创建完成后(即surfaceCreated()方法被触发)再连接服务器
- * 进行实时预览,否则在实时预览时可能会出现SurfaceView尚未完全加载成功,导致调调数据显示异常
- */
- public class SjrsSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
- private HCNetSDK videoCtr; //网络库sdk
- private Player myPlayer = null; //播放库sdk
- public int playPort = -1; //播放端口
- public boolean playFlag = false; //播放标志
- public MonitorCameraInfo cameraInfo = null; //监控点信息
- private SurfaceHolder holder = null;
- public SjrsSurfaceView(Context paramContext) {
- super(paramContext);
- initSurfaceView();
- }
- public SjrsSurfaceView(Context paramContext,
- AttributeSet paramAttributeSet) {
- super(paramContext, paramAttributeSet);
- initSurfaceView();
- }
- public SjrsSurfaceView(Context paramContext,
- AttributeSet paramAttributeSet, int paramInt) {
- super(paramContext, paramAttributeSet);
- initSurfaceView();
- }
- private void initSurfaceView() {
- getHolder().addCallback(this);
- }
- public HCNetSDK SjrsSurface(){
- //实例化海康威视android sdk
- if(videoCtr == null){
- videoCtr = new HCNetSDK();
- }
- return videoCtr;
- }
- public boolean onDown(MotionEvent paramMotionEvent) {
- return false;
- }
- public boolean onFling(MotionEvent paramMotionEvent1,
- MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
- return false;
- }
- public void onLongPress(MotionEvent paramMotionEvent) {
- }
- public boolean onScroll(MotionEvent paramMotionEvent1,
- MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
- return false;
- }
- public void onShowPress(MotionEvent paramMotionEvent) {
- }
- public boolean onSingleTapUp(MotionEvent paramMotionEvent) {
- return false;
- }
- public void surfaceChanged(SurfaceHolder paramSurfaceHolder, int paramInt1,
- int paramInt2, int paramInt3) {
- }
- public void surfaceCreated(SurfaceHolder paramSurfaceHolder) {
- holder = this.getHolder();
- }
- public void surfaceDestroyed(SurfaceHolder paramSurfaceHolder) {}
- public void setMonitorInfo(MonitorCameraInfo setMonitorInfo) {
- this.cameraInfo = setMonitorInfo;
- }
- /**
- * flag 1/暂停 0/恢复
- */
- public void pausePaly(int flag) {
- myPlayer.pause(playPort, flag);
- }
- /**
- * 停止播放&释放资源
- */
- public void stopPlay() {
- try {
- playFlag = false;
- videoCtr.NET_DVR_StopRealPlay(playPort);
- videoCtr.NET_DVR_Logout_V30(cameraInfo.userId);
- cameraInfo.userId = -1;
- videoCtr.NET_DVR_Cleanup();
- //停止播放后释放资源
- if (myPlayer != null) {
- myPlayer.stop(playPort);
- myPlayer.closeStream(playPort);
- myPlayer.freePort(playPort);
- playPort = -1;
- //使用绘图缓存后释放资源
- destroyDrawingCache();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- //暂不做任何操作
- }
- }
- /**
- * 开始实时预览
- **/
- public void startPlay() {
- try {
- //实例化播放API
- myPlayer = Player.getInstance();
- SjrsSurface();
- //初始化海康威视android sdk
- videoCtr.NET_DVR_Init();
- //设置错误回掉函数
- videoCtr.NET_DVR_SetExceptionCallBack(mExceptionCallBack);
- //设置连接超时时长
- videoCtr.NET_DVR_SetConnectTime(60000);
- //获取空闲播放端口
- playPort = myPlayer.getPort();
- NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
- //登录服务器
- cameraInfo.userId = videoCtr.NET_DVR_Login_V30(cameraInfo.serverip,
- cameraInfo.serverport, cameraInfo.username,
- cameraInfo.userpwd, deviceInfo);
- System.out.println("下面是设备信息************************");
- System.out.println("userId=" + cameraInfo.userId);
- System.out.println("通道开始=" + deviceInfo.byStartChan);
- System.out.println("通道个数=" + deviceInfo.byChanNum);
- System.out.println("设备类型=" + deviceInfo.byDVRType);
- System.out.println("ip通道个数=" + deviceInfo.byIPChanNum);
- byte[] sbbyte = deviceInfo.sSerialNumber;
- String sNo = "";
- for (int i = 0; i < sbbyte.length; i++) {
- sNo += String.valueOf(sbbyte);
- }
- System.out.println("设备序列号=" + sNo);
- System.out.println("************************");
- // 参数结构体
- NET_DVR_CLIENTINFO clientInfo = new NET_DVR_CLIENTINFO();
- // 浏览通道号
- clientInfo.lChannel = cameraInfo.channel;
- // 子码流(保证图像连续性)tcp连接方式,如果要保证图像清晰度,可选用主码流
- clientInfo.lLinkMode = 0x80000000;
- // 多播地址 需要多播时配置
- clientInfo.sMultiCastIP = null;
- // 启动实时预览 mRealDataCallback即为数据回传回掉函数
- cameraInfo.playNum = videoCtr.NET_DVR_RealPlay_V30(cameraInfo.userId, clientInfo,mRealPlayCallBack, false);
- System.out.println("playFlags=" + cameraInfo.playNum);
- System.out.println("GetLastError="+ videoCtr.NET_DVR_GetLastError());
- } catch (Exception e) {
- e.printStackTrace();
- // 释放资源
- stopPlay();
- }
- }
- /**
- * 异常回掉函数
- */
- private ExceptionCallBack mExceptionCallBack = new ExceptionCallBack() {
- public void fExceptionCallBack(int arg0, int arg1, int arg2) {
- // TODO Auto-generated method stub
- System.out.println("异常回掉函数运行!");
- }
- };
- private RealPlayCallBack mRealPlayCallBack = new RealPlayCallBack() {
- @Override
- public void fRealDataCallBack(int lRealHandle, int dataType,
- byte[] paramArrayOfByte, int byteLen) {
- //端口连接状态返回码
- if (playPort == -1)
- return;
- // dataType视频连接数量 1个或者4个
- switch (dataType) {
- case 1:
- // 打开流连接
- if (myPlayer.openStream(playPort, paramArrayOfByte, byteLen,1024 * 1024)) {
- // 1.连接状态 2.视频模式:1为实时
- if (myPlayer.setStreamOpenMode(playPort, 1)) {
- // 放入要播放的控件中
- if (myPlayer.play(playPort, holder)) {
- playFlag = true;
- } else {
- playError(3);
- }
- } else {
- playError(2);
- }
- } else {
- playError(1);
- }
- break;
- case 4:
- if (playFlag && myPlayer.inputData(playPort, paramArrayOfByte,byteLen)) {
- playFlag = true;
- } else {
- playError(4);
- playFlag = false;
- }
- }
- }
- };
- /**
- * 打印出相对应的异常
- * @param step
- */
- private void playError(int step) {
- switch (step) {
- case 1:
- System.out.println("openStream error,step=" + step);
- break;
- case 2:
- System.out.println("setStreamOpenMode error,step=" + step);
- break;
- case 3:
- System.out.println("play error,step=" + step);
- break;
- case 4:
- System.out.println("inputData error,step=" + step);
- break;
- }
- stopPlay();
- }
- }
最近有些忙就弄了一个“抓图”功能,勿喷勿喷
这个代码 你可以自己弄个按钮来触发,不多说了
NET_DVR_JPEGPARA jpeg = new NET_DVR_JPEGPARA();
INT_PTR a = new INT_PTR();
System.out.println("返回长度:" + a);
/* a.iValue = 1024; */
byte[] num = new byte[1024 * 1024];
// 设置图片的分辨率
jpeg.wPicSize = 2;
// 设置图片质量
jpeg.wPicQuality = 2;
// 创建文件目录
File file = new File(cameraInfo.filepath + "/a.jpg");
/* file.mkdirs(); */
// System.out.println("文件路劲:"+cameraInfo.filepath);
/** 1.userId 返回值 2.通道号 3.图像参数 4.路劲 */
boolean is = mSurface.SjrsSurface()
.NET_DVR_CaptureJPEGPicture_NEW(cameraInfo.userId,
cameraInfo.channel, jpeg, num, 1024 * 1024, a);
System.out.println(is + " "
+ mSurface.SjrsSurface().NET_DVR_GetLastError());
// 存储本地
BufferedOutputStream outputStream = null;
try {
outputStream = new BufferedOutputStream(
new FileOutputStream(file));
outputStream.write(num);
outputStream.flush();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Toast.makeText(VideoShowActivity.this, "截取成功,保存在SDK的" + cameraInfo.filepath + "下", 1).show();
注意加权限:android.permission.INTERNET 果断上源码Demo~~~亲你邪恶了。。 /** * 其他功能 抓图、录像、远程关机重启、多屏展示等等(功能很多很多。。。)后续有完善的功能继续更新 */
|