android写的远程监控调用JNI的停止视频问题,如果连接远程摄像头成功后可以安全退出当前activity,如果连接摄像头不成功调用JNI中的stop方法卡死
问题:
在连接成功后可以安全退出视频查看,当正在连接时点击后退按钮,应用会出现卡死想象,请大家指教,这个问题困扰好几天了。
activity部分代码:
@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setIcon(android.R.drawable.ic_dialog_alert);builder.setTitle("温馨提示");builder.setMessage("确定退出视频监控吗?");builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Overridepublic void onClick(DialogInterface dialog, int which) {onBackPressed();}});builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});builder.create();builder.show();return false;}
public void onBackPressed() {
try {if (this.videoView.getStatus() == 1){videoView.logoutDevice();this.videoView.stopVideo();if(videoThread != null){videoThread.interrupt();videoThread = null;}}finish();} catch (Exception e) {e.printStackTrace();}
}
class VideoView extends View implements Runnable {ByteBuffer buffer;final int h = 288;ImageButton ibutStop;ImageButton ibutdown;ImageButton ibutzoomin;ImageButton ibutleft;ImageButton ibutpic;ImageButton ibutzoomout;ImageButton ibutright;ImageButton ibutup;private int initial = 0;private Bitmap mBitmap;byte[] pixel = new byte[202752];Bitmap resizedBitmap;private int step = 0;private boolean takepic = false;Bitmap temp;private Video video;Bitmap videoBit = null;final int w = 352;
static {System.loadLibrary("Kernel");}
public VideoView(Context paramContext, AttributeSet paramAttributeSet) {super(paramContext, paramAttributeSet);}
public native int status();
public native int connection(String paramString, int paramInt);
public native int loginDevice(String paramString1, String paramString2);
public native int liveVideo(int paramInt, Object paramObject);
public native int InitDecoder();
public native int UninitDecoder();
public native int changeChannel(int paramInt);
public native int connectionStatus();
public native int sendAutoPTZ(int paramInt1, int paramInt2);
public native int sendPTZCmd(int paramInt1, int paramInt2);
public native int stopVideo();
public native int logoutDevice();
public native int nextChannel(int paramInt);
public native int previousChannel(int paramInt);
public int getStatus() {return status();}
protected void onDraw(Canvas paramCanvas) {try {super.onDraw(paramCanvas);if ((this.initial != 0) && (status() != 0)) {videoBit = Bitmap.createBitmap(352, 288, Bitmap.Config.RGB_565);buffer = ByteBuffer.wrap(this.pixel);this.videoBit.copyPixelsFromBuffer(this.buffer);if (this.takepic) {saveBitmap2file(this.videoBit);Toast.makeText(getContext(), "截图保存成功", 3000).show();this.takepic = false;}Rect localRect1 = new Rect(0, 0, 352, 288);Rect localRect2 = new Rect(0, 0, VideoActivity.viewWidth,VideoActivity.viewHeight);paramCanvas.drawBitmap(this.videoBit, localRect1, localRect2,null);}int i = -10 + VideoActivity.viewHeight;if (this.step == 0) {Paint localPaint1 = new Paint();localPaint1.setColor(Color.BLUE);paramCanvas.drawText("正在连接前端设备, 请稍等..... ", 10, i, localPaint1);}
if (this.step == 1) {Paint localPaint2 = new Paint();localPaint2.setColor(Color.GREEN);paramCanvas.drawText("成功连接到前端设备 ", 10, i, localPaint2);}if (this.step == 2) {Paint localPaint3 = new Paint();localPaint3.setColor(Color.RED);paramCanvas.drawText("连接前端设备失败 ", 10, i, localPaint3);}if (this.step == 3) {Paint localPaint4 = new Paint();localPaint4.setColor(Color.GREEN);paramCanvas.drawText("登录前端设备成功 ", 10, i, localPaint4);}if (this.step == 4) {Paint localPaint5 = new Paint();localPaint5.setColor(Color.RED);paramCanvas.drawText("传输实施视频失败 ", 10, i, localPaint5);}if (this.step == 5) {Paint localPaint6 = new Paint();localPaint6.setColor(Color.GREEN);paramCanvas.drawText("正在传输实时视频", 10, i, localPaint6);}
} catch (Exception e) {// TODO: handle exception}}
public void run() {try {int result = -1; result = connection(this.video.getIp(),Integer.valueOf(this.video.getTp()));System.out.println("connection ............" + this.video.getIp()+ " " + this.video.getTp());// result = connection("192.168.1.100",6036);
System.out.println("connection ............" + result);if (result == 0) {this.step = 2;updateView();return;}System.out.println(this.video.getUn() + "!!!!!!!!!!!!!!!!!!!!!!!!!"+ this.video.getPw());result = loginDevice(this.video.getUn(), this.video.getPw());
if (result == 1) {step = 3;updateView();result = liveVideo(this.video.getCh(), pixel);if (result != 1) {step = 4;updateView();stopVideo();return;} else {step = 5;updateView();}
} else {return;}
} catch (Exception e) {e.printStackTrace();} finally {buffer = null;videoBit = null;resizedBitmap = null;temp = null;System.gc();}}
boolean saveBitmap2file(Bitmap paramBitmap) {Bitmap.CompressFormat localCompressFormat = Bitmap.CompressFormat.JPEG;//.../storage/emulated/0/hanrun/String str1 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "hanrun" + File.separator;
File localFile1 = new File(str1);if (!localFile1.exists()) {localFile1.mkdir();}
Date localDate = new Date();String str2 = new SimpleDateFormat("MMddkkmmss").format(localDate);File localFile2 = new File(str1 + this.video.getTp() + str2 + ".jpg");FileOutputStream localFileOutputStream;try {localFileOutputStream = new FileOutputStream(localFile2);boolean bool2 = paramBitmap.compress(localCompressFormat, 100,localFileOutputStream);return bool2;} catch (IOException localIOException2) {localIOException2.printStackTrace();return false;}}
public void setTakepic(boolean paramBoolean) {this.takepic = paramBoolean;}
public void setVideo(Video paramVideo) {this.video = paramVideo;}
public void updateView() {postInvalidate();this.initial = 1;}
}
JNI文件如下,只列出连接和退出摄像头的代码:
JNIEXPORT jint JNICALL Java_com_handlerun_view_VideoView_connection(JNIEnv *env, jobject thiz, jstring ip,jint port) { status = 1;m_DeviceAddr.sin_family = AF_INET;m_DeviceAddr.sin_port = htons(port);char *m_ip = (char*)env->GetStringUTFChars(ip, NULL);m_DeviceAddr.sin_addr.s_addr = inet_addr(m_ip); sk.socket = socket(AF_INET,SOCK_STREAM,0);if(sk.socket == SOCKET_ERROR){status =0;return 0;} //设置为非阻塞模式unsigned long ul = 1;result = ioctl(sk.socket, FIONBIO, &ul);if(result == SOCKET_ERROR){__android_log_print(ANDROID_LOG_ERROR,"Tag", " ioctl FIONBIO error :%d \n", result );status = 0;return 0;} //连接到服务器int error;result = connect(sk.socket,(struct sockaddr*)&m_DeviceAddr,sizeof(struct sockaddr)); if(result != -1 ){__android_log_print(ANDROID_LOG_ERROR,"Tag", "socket connect error :%d \n", result );status = 0;return 0;} //检测连接状态if(errno != EINPROGRESS){__android_log_print(ANDROID_LOG_ERROR,"Tag", "socket earn EINPROGRESS :%d \n", errno );close(sk.socket);status = 0;return 0;}struct timeval tv;fd_set writefds;tv.tv_sec = 5;tv.tv_usec = 0;FD_ZERO(&writefds);FD_SET(sk.socket, &writefds);result = select(sk.socket+1,NULL,&writefds,NULL,&tv);// 连接超时if(result==0){__android_log_print(ANDROID_LOG_ERROR,"Tag", " select error :%d \n", result );close(sk.socket);status = 0;return 0;} //穿透防火墙int len = sizeof(int);result = getsockopt(sk.socket, SOL_SOCKET, SO_ERROR, &error, &len);if(result!=0){__android_log_print(ANDROID_LOG_ERROR,"Tag", " getsockopt error :%d \n", errno );close(sk.socket);status = 0;return 0;} memset(buf,0,200);result = ReceiveData(sk.socket,m_DeviceTypeInfo,64,20); if(result != 64){close(sk.socket);status = 0;return 0;}netprotrocolver = *((unsigned long*)m_DeviceTypeInfo+3); __android_log_write(ANDROID_LOG_ERROR,"Tag","stopVideo ----hrsst stop over ------ ............... "); return 1; } JNIEXPORT jint JNICALL Java_com_handlerun_view_VideoView_stopVideo(JNIEnv *env, jobject thiz) {memset(&dataHead,0,dataHead_Size);memset(&packCMD,0,packCmd_Size); dataHead.HeadFlag = '1111';dataHead.lLen = packCmd_Size; packCMD.cmdType = CMD_REQUEST_LOGOUT;packCMD.dataLen = 0; result = SendData(sk.socket,(char*)&dataHead,dataHead_Size,10);result = SendData(sk.socket,(char*)&packCMD,packCmd_Size,10);result = shutdown(sk.socket,SHUT_RDWR); close(sk.socket); __android_log_write(ANDROID_LOG_ERROR,"Tag","stopVideo stop over ............... "); status = 0 ;return 1; }
android写的远程监控调用JNI的停止视频问题,如果连接远程摄像头成功后可以安全退出当前activity,如果连接摄像头不成功调用JNI中的stop方法卡死相关推荐
- 用java实现一个简单远程监控软件_利用java实现一个简单的远程监控程序
一般的远程监控软件都是用c或者c++等语言开发的,而使用java如何来实现相同的功能呢. 首先我们先介绍一下一个简单的远程监控程序的实现原理. 功能一,远程屏幕监视 (1) 必须要有监控端与被监控端, ...
- Android开发宇视监控播放,宇视科技视频监控(EZPlayer)
EZPlayer是宇视科技推出的监控设备套件.用于对本地录像进行播放.支持.ts..mp4及SD卡视频格式播放.录像同步播放.水印检测.抓拍及剪辑.有需要的小伙伴欢迎来西西下载体验. 软件简介: EZ ...
- 全球与中国远程监控(M&C)系统市场深度研究分析报告
本文研究全球及中国市场远程监控(M&C)系统现状及未来发展趋势,侧重分析全球及中国市场的主要企业,同时对比北美.欧洲.中国.日本.东南亚和印度等地区的现状及未来发展趋势. 2021年全球远程监 ...
- 疫情期间如何做到远程监控,实现设备的“智慧把守”?
一.前 言 工业4.0引入了执行旧任务的新方法.工业4.0提供的工业解决方案能够创建一个无人工厂.在该工厂中,当各个机器做出分散决策时,人工劳动被降低至最小化.第四次工业革命通过状态监控和远程监控来优 ...
- 组态基于DTU实现机床远程监控系统
1 概 述 随着物联网各种技术快速发展,各物联网远程监测应用场景也应用而生,以空压机为例:空压机是一种空气压缩和气体输送设备,广泛运用于矿山.机械.电子.医疗等各行业.空压机常规都是需要人在现场监测和 ...
- mini2440基于motion的移动图像监控系统和基于SDL的远程监控
这段时间在考虑做基于mini2440移动图像监控系统设计和远程监控设计:这个项目刚开始被我分割成几部分: 1.实现采集开发板上摄像头数据在开发板的LCD显示 2.实现网络远程监控把开发板上摄像头数据发 ...
- PLC远程监控助力设备售后运维
PLC远程监控助力设备售后运维 PLC远程控制 PLC远程监控 PLC远程调试 PLC远程下载 PLC远程诊断 目前大多数PLC设备的故障诊断,固件升级是由人工去完成.而很多设备的故障只是因为操作人员 ...
- 全球与中国远程监控软件市场现状及未来发展趋势
本文研究全球及中国市场远程监控软件现状及未来发展趋势,侧重分析全球及中国市场的主要企业,同时对比北美.欧洲.日本.中国.东南亚.印度等地区的现状及未来发展趋势. 2019年全球远程监控软件市场规模达到 ...
- 魅影远程监控(企业版)
魅影远程监控(企业版) 无论你的团队多么的优秀,无论你多么严格地以身作则,总有成员会使你头痛.他们总在工作时间做无关的事情.上无关的网站.将大量的时间花在工作以外.你知道这样的情况随时在发生, ...
最新文章
- 矩阵论习题:设A,B为投影矩阵,证明A+B仍为投影矩阵当且仅当AB=BA=0。
- python多项分时求和_python实现连续变量最优分箱详解--CART算法
- PHP array_combine
- 【C++】38.类成员变量声明为 对象与指针的区别
- linux系统下的mysqlgt;aborted_MySQL令人头疼的Aborted告警案例分析
- c++ Segmentation fault (core dumped) 的一个实例
- Kindeditor富文本编辑器使用
- CentOS 7.6 RPM方式安装Oracle19c的过程
- Spring-ClassPathBeanDefinitionScanner
- thymeleaf模板的应用
- vs项目中的筛选器(filter)
- win7显示时钟与服务器同步失败,win7时间同步出错_Win7电脑时间同步出错怎么办...
- 冯仑《企业领导最容易犯的十大错误》
- 开放平台-百度开放平台:百度开放平台
- DFS序——树链剖分前驱知识
- 快递查询 教你一个方法使用物流单号查询物流信息
- 删除网络共享计算机,如何把电脑上的共享文件删除
- android打地鼠设计报告,android开发中利用handler制作一个打地鼠小游戏
- 008-阈下意识|识别商场上的圈套
- python中的整型是什么意思_Python中整型的基本介绍(代码示例)