实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.xmobileapp.cammonitor; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import com.xmobileapp.cammonitor.config.CamMonitorParameter; import com.xmobileapp.cammonitor.core.CameraSource; import com.xmobileapp.cammonitor.core.SocketCamera; import com.xmobileapp.cammonitor.util.ActivtyUtil; public class CamMonitorView extends SurfaceView implements SurfaceHolder.Callback { /** The thread that actually draws the image stream */ private CamMonitorThread thread; public static final String TAG = "CamMonitorView"; private CamMonitorParameter cmPara; /** * The constructor called from the main ServerAct activity * * @param context * @param attrs */ public CamMonitorView(Context context, AttributeSet attrs) { super(context, attrs); // register our interest in hearing about changes to our surface SurfaceHolder holder = getHolder(); holder.addCallback(this); thread = new CamMonitorThread(holder); Log.d(TAG, "@@@ done creating view!"); } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { thread.setSurfaceSize(width, height); } public void surfaceCreated(SurfaceHolder holder) { // start the thread here so that we don't busy-wait in run() // waiting for the surface to be created thread.setRunning(true); thread.start(); } public void surfaceDestroyed(SurfaceHolder holder) { thread.closeCameraSource(); boolean retry = true; while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { } } } class CamMonitorThread extends Thread{ /** Handle to the surface manager object we interact with */ private SurfaceHolder mSurfaceHolder; /** * Current height of the surface/canvas. * * @see #setSurfaceSize */ private int mCanvasHeight = 1; /** * Current width of the surface/canvas. * * @see #setSurfaceSize */ private int mCanvasWidth = 1; /** Indicate whether the surface has been created & is ready to draw */ private boolean mRun = false; private CameraSource cs; private Canvas c = null; public CamMonitorThread(SurfaceHolder surfaceHolder) { super(); mSurfaceHolder = surfaceHolder; } /** * Used to signal the thread whether it should be running or not. * Passing true allows the thread to run; passing false will shut it * down if it's already running. Calling start() after this was most * recently called with false will result in an immediate shutdown. * * @param b true to run, false to shut down */ public void setRunning(boolean b) { mRun = b; if (mRun == false) { } } /** * the heart of the worker bee */ public void run() { // while running do stuff in this loop...bzzz! while (mRun) { try { c = mSurfaceHolder.lockCanvas(null); // synchronized (mSurfaceHolder) { // captureImage(cmPara.getIp(), cmPara.getPort()); captureImage(cmPara.getIp(), cmPara.getPort(), mCanvasWidth, mCanvasHeight); // } } finally { // do this in a finally so that if an exception is thrown // during the above, we don't leave the Surface in an // inconsistent state if (c != null) { mSurfaceHolder.unlockCanvasAndPost(c); c = null; } }// end finally block } } public void setSurfaceSize(int width, int height) { // synchronized to make sure these all change atomically synchronized (mSurfaceHolder) { mCanvasWidth = width; mCanvasHeight = height; } } // private boolean captureImage(String ip, int port){ // // cs = new SocketCamera(ip, port, c.getWidth(), c.getHeight(), true); // if (!cs.open()) { /* deal with failure to obtain camera */ // ActivtyUtil.showAlert(CamMonitorView.this.getContext(), "Error", "不能连接远端服务器!", "确定"); // return false; // } // cs.capture(c); //capture the frame onto the canvas // // return true; // } private boolean captureImage(String ip, int port, int width, int height){ cs = new SocketCamera(ip, port, width, height, true); if (!cs.open()) { /* deal with failure to obtain camera */ ActivtyUtil.showAlert(CamMonitorView.this.getContext(), "Error", "不能连接远端服务器!", "确定"); return false; } cs.capture(c); //capture the frame onto the canvas return true; } public boolean saveImage(){ String now = String.valueOf(System.currentTimeMillis()); if(cs == null){ return false; } cs.saveImage(cmPara.getLocal_dir(), now ".JPEG"); return true; } public void closeCameraSource(){ cs.close(); } public CameraSource getCameraSource() { return cs; } } public CamMonitorParameter getCmPara() { return cmPara; } public void setCmPara(CamMonitorParameter cmPara) { this.cmPara = cmPara; } public void setRunning(boolean b) { this.thread.setRunning(b); } public CamMonitorThread getThread() { return thread; } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论