基于android的远程视频监控系统(已开放源码)

基本过程是android作为socket客户端将采集到的每一帧图像数据发送出去,PC作为服务器接收并显示每一帧图像实现远程监控。图片如下(后来PC端加了个拍照功能)。。。

(PS。刚学android和java不久很多东西还不懂,高手若是知道哪些地方可以继续优化的话还请多多指点下啊)

系统代码如下:

一、android手机客户端

(1)AndroidManifest.xml文件。添加camera和socket权限,并设置了程序开始执行的activity、

<?xml
version=
"1.0" encoding="utf-8"?>

<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"

    package="org.wanghai.CameraTest"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk
android:minSdkVersion=
"15" />

    

    <!--
授予程序使用摄像头的权限 -->

        <uses-permission
android:name=
"android.permission.CAMERA" />

        <uses-feature
android:name=
"android.hardware.camera" />

        <uses-feature
android:name=
"android.hardware.camera.autofocus" />

        <uses-permission
android:name=
"android.permission.INTERNET"/>

    <uses-permission
android:name=
"android.permission.KILL_BACKGROUND_PROCESSES"/>

    <uses-permission
android:name=
"android.permission.RESTART_PACKAGES"/>

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" >

                

        <activity

            android:name=".GetIP"

            android:screenOrientation="landscape"

            android:label="@string/app_name" >

            <intent-filter>

                <action
android:name=
"android.intent.action.MAIN" />

                <category
android:name=
"android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity

            android:name=".CameraTest"

            android:screenOrientation="landscape"

            android:label="@string/app_name" >

        </activity>

        

    </application>

</manifest>

  (2)main.xml 设置surfaceview用于摄像头采集图像的预览

<?xml
version=
"1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

   <SurfaceView

        android:id="@+id/sView"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:scaleType="fitCenter"/>

</LinearLayout>

  (3)login.xml 登录界面,用于输入服务器IP

<?xml
version=
"1.0" encoding="utf-8"?>

<TableLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"

        android:id="@+id/loginForm"

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        >

<TableRow>           

<TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="IP:"

        android:textSize="10pt"

        />

<!--
输入用户名的文本框 -->

<EditText

    android:id="@+id/ipedittext"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:digits="0123456789."

        android:hint="请填写服务器IP"

        android:selectAllOnFocus="true"

        />

</TableRow>

</TableLayout>

  (4)GetIP.java 获得服务器IP后,通过Intent启动CameraTest的activity,ip信息通过Bundle传递

public class GetIP
extends Activity
{

        String
ipname =
null;

        @Override

    public void onCreate(Bundle
savedInstanceState) {

        super.onCreate(savedInstanceState);

        //
设置全屏

        requestWindowFeature(Window.FEATURE_NO_TITLE);

             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);       

      

              final Builder
builder =
new AlertDialog.Builder(this);  
//定义一个AlertDialog.Builder对象                                        

                builder.setTitle("登录服务器对话框");                         
//
设置对话框的标题

                

                //装载/res/layout/login.xml界面布局

                TableLayout
loginForm = (TableLayout)getLayoutInflater().inflate( R.layout.login,
null);               

                final EditText
iptext = (EditText)loginForm.findViewById(R.id.ipedittext);                               

                builder.setView(loginForm);                             
//
设置对话框显示的View对象

                //
为对话框设置一个“登录”按钮

                builder.setPositiveButton("登录"

                        //
为按钮设置监听器

                        ,
new OnClickListener()
{

                                @Override

                                public void onClick(DialogInterface
dialog,
int which)
{

                                        //此处可执行登录处理

                                        ipname
= iptext.getText().toString().trim();

                                        Bundle
data =
new Bundle();

                                        data.putString("ipname",ipname);                                       

                                        Intent
intent =
new Intent(GetIP.this,CameraTest.class);

                                        intent.putExtras(data);

                                        startActivity(intent);

                                }

                        });

                //
为对话框设置一个“取消”按钮

                builder.setNegativeButton("取消"

                        
new OnClickListener()

                        {

                                @Override

                                public void onClick(DialogInterface
dialog,
int which)

                                {

                                        //取消登录,不做任何事情。

                                        System.exit(1);

                                }

                        });

                //创建、并显示对话框

                builder.create().show();

        }

}

  (5)CameraTest.java 程序主体。设置PreviewCallback后,每当一帧图像数据采集完成后将调用PreviewCallback的onPreviewFrame函数。在这里我们将YUV格式数据转为jpg,再启用线程将数据通过socket发送出去。

public class CameraTest
extends Activity
{

        SurfaceView
sView;

        SurfaceHolder
surfaceHolder;

        int screenWidth,
screenHeight;       

        Camera
camera;                   
//
定义系统所用的照相机       

        boolean isPreview
=
false;       
//是否在浏览中

        private String
ipname;

        @SuppressWarnings("deprecation")

        @Override

    public void onCreate(Bundle
savedInstanceState) {

        super.onCreate(savedInstanceState);

        //
设置全屏

             requestWindowFeature(Window.FEATURE_NO_TITLE);

             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        

        //
获取IP地址

        Intent
intent = getIntent();

        Bundle
data = intent.getExtras();

        ipname
= data.getString(
"ipname");

                        

                screenWidth
=
640;

                screenHeight
=
480;               

                sView
= (SurfaceView) findViewById(R.id.sView);                 
//
获取界面中SurfaceView组件               

                surfaceHolder
= sView.getHolder();                              
//
获得SurfaceView的SurfaceHolder

                

                //
为surfaceHolder添加一个回调监听器

                surfaceHolder.addCallback(new Callback()
{

                        @Override

                        public void surfaceChanged(SurfaceHolder
holder,
int format,
int width,int height)
{                               

                        }

                        @Override

                        public void surfaceCreated(SurfaceHolder
holder) {                                                       

                                initCamera();                                           
//
打开摄像头

                        }

                        @Override

                        public void surfaceDestroyed(SurfaceHolder
holder) {

                                //
如果camera不为null ,释放摄像头

                                if (camera
!=
null)
{

                                        if (isPreview)

                                                camera.stopPreview();

                                        camera.release();

                                        camera
=
null;

                                }

                            System.exit(0);

                        }               

                });

                //
设置该SurfaceView自己不维护缓冲   

                surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

                

    }

    

    private void initCamera()
{

            if (!isPreview)
{

                        camera
= Camera.open();

                }

                if (camera
!=
null &&
!isPreview) {

                        try{

                                Camera.Parameters
parameters = camera.getParameters();                               

                                parameters.setPreviewSize(screenWidth,
screenHeight);   
//
设置预览照片的大小                               

                                parameters.setPreviewFpsRange(20,30);                   
//
每秒显示20~30帧                               

                                parameters.setPictureFormat(ImageFormat.NV21);          
//
设置图片格式                               

                                parameters.setPictureSize(screenWidth,
screenHeight);   
//
设置照片的大小

                                //camera.setParameters(parameters);                     
// android2.3.3以后不需要此行代码

                                camera.setPreviewDisplay(surfaceHolder);                
//
通过SurfaceView显示取景画面                               

                        camera.setPreviewCallback(new StreamIt(ipname));        
//
设置回调的类                               

                                camera.startPreview();                                  
//
开始预览                               

                                camera.autoFocus(null);                                 
//
自动对焦

                        }
catch (Exception
e) {

                                e.printStackTrace();

                        }

                        isPreview
=
true;

                }

    }

    

}

class StreamIt
implements Camera.PreviewCallback
{

        private String
ipname;

        public StreamIt(String
ipname){

                this.ipname
= ipname;

        }

        

    @Override

    public void onPreviewFrame(byte[]
data, Camera camera) {

        Size
size = camera.getParameters().getPreviewSize();         

        try{

                //调用image.compressToJpeg()将YUV格式图像数据data转为jpg格式

            YuvImage
image =
new YuvImage(data,
ImageFormat.NV21, size.width, size.height,
null); 

            if(image!=null){

                    ByteArrayOutputStream
outstream =
new ByteArrayOutputStream();

                image.compressToJpeg(new Rect(0,
0,
size.width, size.height),
80,
outstream);

                outstream.flush();

                //启用线程将图像数据发送出去

                Thread
th =
new MyThread(outstream,ipname);

                th.start();              

            

        }catch(Exception
ex){ 

            Log.e("Sys","Error:"+ex.getMessage()); 

        }       

    }

}

    

class MyThread
extends Thread{       

        private byte byteBuffer[]
=
new byte[1024];

        private OutputStream
outsocket;       

        private ByteArrayOutputStream
myoutputstream;

        private String
ipname;

        

        public MyThread(ByteArrayOutputStream
myoutputstream,String ipname){

                this.myoutputstream
= myoutputstream;

                this.ipname
= ipname;

        try {

                        myoutputstream.close();

                }
catch (IOException
e) {

                        e.printStackTrace();

                }

        }

        

    public void run()
{

        try{

                //将图像数据通过Socket发送出去

            Socket
tempSocket =
new Socket(ipname,
6000);

            outsocket
= tempSocket.getOutputStream();

            ByteArrayInputStream
inputstream =
new ByteArrayInputStream(myoutputstream.toByteArray());

            int amount;

            while ((amount
= inputstream.read(byteBuffer)) != -
1)
{

                outsocket.write(byteBuffer,
0,
amount);

            }

            myoutputstream.flush();

            myoutputstream.close();

            tempSocket.close();                  

        }
catch (IOException
e) {

            e.printStackTrace();

        }

    }

}

  二、PC服务器端

ImageServer.java 用于显示图像,并且可以拍照

public class ImageServer
{       

    public static ServerSocket
ss =
null;

    

    public static void main(String
args[])
throws IOException{   

            ss
=
new ServerSocket(6000);

        

        final ImageFrame
frame =
new ImageFrame(ss);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

       

        while(true){

                frame.panel.getimage();

            frame.repaint();

        }       

    }

       

}

/**

    A
frame with an image panel

*/

@SuppressWarnings("serial")

class ImageFrame
extends JFrame{

        public ImagePanel
panel;

        public JButton
jb;

   

    public ImageFrame(ServerSocket
ss){

               //
get screen dimensions             

               Toolkit
kit = Toolkit.getDefaultToolkit();

        Dimension
screenSize = kit.getScreenSize();

        int screenHeight
= screenSize.height;

        int screenWidth
= screenSize.width;

        //
center frame in screen

        setTitle("ImageTest");

        setLocation((screenWidth
- DEFAULT_WIDTH) /
2,
(screenHeight - DEFAULT_HEIGHT) /
2);

        setSize(DEFAULT_WIDTH,
DEFAULT_HEIGHT);

        //
add panel to frame

        this.getContentPane().setLayout(null);

        panel
=
new ImagePanel(ss);

        panel.setSize(640,480);

        panel.setLocation(0,
0);

        add(panel);

        jb
=
new JButton("拍照");

        jb.setBounds(0,480,640,50);

        add(jb);

        saveimage
saveaction =
new saveimage(ss);

        jb.addActionListener(saveaction);

    }

    public static final int DEFAULT_WIDTH
=
640;

    public static final int DEFAULT_HEIGHT
=
560

}

/**

   A
panel that displays a tiled image

*/

@SuppressWarnings("serial")

class ImagePanel
extends JPanel
{    

    private ServerSocket
ss;

    private Image
image;

    private InputStream
ins;

         

    public ImagePanel(ServerSocket
ss) { 

            this.ss
= ss;

    }

    

    public void getimage()
throws IOException{

            Socket
s =
this.ss.accept();

        System.out.println("连接成功!");

        this.ins
= s.getInputStream();

                this.image
= ImageIO.read(ins);

                this.ins.close();

    }

   

    public void paintComponent(Graphics
g){ 

        super.paintComponent(g);   

        if (image
==
null)
return;

        g.drawImage(image,
0,
0,
null);

    }

}

class saveimage
implements ActionListener
{

        RandomAccessFile
inFile =
null;

        byte byteBuffer[]
=
new byte[1024];

        InputStream
ins;

        private ServerSocket
ss;

        

        public saveimage(ServerSocket
ss){

                this.ss
= ss;

        }

        

        public void actionPerformed(ActionEvent
event){

        try {

                        Socket
s = ss.accept();

                        ins
= s.getInputStream();

                        

                        //
文件选择器以当前的目录打开

                JFileChooser
jfc =
new JFileChooser(".");

                jfc.showSaveDialog(new javax.swing.JFrame());

                //
获取当前的选择文件引用

                File
savedFile = jfc.getSelectedFile();

                

                //
已经选择了文件

                if (savedFile
!=
null)
{

                    //
读取文件的数据,可以每次以快的方式读取数据

                    try {

                                        inFile
=
new RandomAccessFile(savedFile,
"rw");

                                }
catch (FileNotFoundException
e) {

                                        e.printStackTrace();

                                }

                }

            int amount;

            while ((amount
= ins.read(byteBuffer)) != -
1)
{

                inFile.write(byteBuffer,
0,
amount);

            }

            inFile.close();

            ins.close();

            s.close();

            javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),

                    "已接保存成功",
"提示!",
javax.swing.JOptionPane.PLAIN_MESSAGE);

                }
catch (IOException
e) {

                        e.printStackTrace();

                }

        }

}

  开放源码如下(android我使用的是4.03的SDK,其它版本请自行更改。2.3.3版本以下的请注意initCamera()里被注释掉的哪一行)

只能在android4.04系统的手机上运行成功哦。

下面是测试成功时的启动画面:

源码链接:

http://files.cnblogs.com/feifei1010/%E5%9F%BA%E4%BA%8Eandroid%E6%89%8B%E6%9C%BA%E7%9A%84%E8%BF%9C%E7%A8%8B%E8%A7%86%E9%A2%91%E7%9B%91%E6%8E%A7%E7%B3%BB%E7%BB%9F.rar

时间: 2024-10-13 10:56:59

基于android的远程视频监控系统(已开放源码)的相关文章

基于Android的远程视频监控系统(含源码)

基本过程是android作为socket客户端将采集到的每一帧图像数据发送出去,PC作为服务器接收并显示每一帧图像实现远程监控.图片如下(后来PC端加了个拍照功能)... (PS.刚学android和java不久很多东西还不懂,高手若是知道哪些地方可以继续优化的话还请多多指点下啊) 系统代码如下:一.android手机客户端(1)AndroidManifest.xml文件.添加camera和socket权限,并设置了程序开始执行的activity. <?xml version="1.0&q

用java写一个远程视频监控系统,实时监控(类似直播)我想用RPT协议,不知道怎么把RPT协议集成到项目中

我最近在用java写一个远程视频监控系统,实时监控(类似直播)我想用RPT协议,不知道怎么把RPT协议集成到项目中,第一次写项目,写过这类项目的多多提意见,哪方面的意见都行,有代码或者demo的求赏给我,谢谢

教你怎么搭建远程视频监控系统

时下,越来越多的情况需要应用到远程监控了,在此将刚做好的一个小项目给大家 首先介绍一下硬件方面, 海康威视DS-7016录像机         1台     西部数据SATA接口500G硬盘      1个     TP-LINK R460+路由器           1个    花生壳动态域名专业服务        1年    电源线和视频线随便几百米就够了 第一步搭建你的监控系统 首先将录像机接电源并连接路由器,通过硬盘录像机的默认管理地址进行登陆管理设置,如http://192.168.0

(转)基于单片机的网络视频监控系统的设计

本文提出了一种网络化视频监控系统,将单片机控制技术.USB数据采集技术与基于TCP/IP协议栈的计算机网络结构有机结合起来,使人们能够通过监控现场的摄像机和麦克风等视频音频捕捉设备. 将监控现场的信号通过局域网传输,并送到监视器上以获得实时图像和声音,从而实现远程遥视现场. 随着嵌入式系统的飞速发展,在许多领域,以微控制器为中心的应用系统正逐步取代以计算机为中心的应用.因此,对于网络应用系统的研究,越来越多的研究人员将研究重点转移到嵌入式系统上来.基于此背景,本文提出了一种网络化视频监控系统方案

基于邮件系统的远程实时监控系统的实现 Python版

人生苦短,我用Python~ 界内的Python宣传标语,对Python而言,这是种标榜,实际上,Python确实是当下最好用的开发语言之一. 在相继学习了C++/C#/Java之后,接触Python,最一开始突然一片茫然,似乎是进入了新世界,所有C家族的语法,在这里都或多或少地发生了改变,方法没有大括号,喜闻乐见的格式.定义变量不需要声明,时间长了,竟爱上了这个简介明了,高效快捷的语言,当然,也是当下开发语言界内的宠儿,不可否认,Python是当下最流行的开发语言了. [前言] 本文拟使用Py

H.264远程视频监控系统

由usb摄像头驱动,h264编码库,采集read_video,传输,客户端显示组成 读取摄像头数据缓冲区是否有帧数据收到,有数据收到就读取数据并进行H264编码,把数据发送到socket发送到客户端. 配置内核:make menuconfig ARCH=arm, 选择gerneral setup-->multimedia devices-->device drivers-->multimedia devices-->video capture adapters-->V4L U

转:FFmpeg的远程视频监控系统编解码

0 引言 随着视频编解码技术.计算机网络技术.数字信号处理技术和嵌入式系统的发展,以嵌入式网络视频服务器为核心的远程视频监控系统开始在市场上崭露头角.该系统把摄像机输出的模拟视频信号通过内置的嵌入式视频编码器直接转换成视频流,通过计算机网络传输出去.嵌入式网络视频服务器具备视频编码处理.网络通信.系统控制等强大功能,直接支持网络视频传输和网络管理,使得监控范围达到前所未有的广度.在远程视频监控系统中,摄像头获取的原始视频流在传输之前需要压缩,而FFmpeg可以将原始视频压缩为H264格式视频流,

项目三. 基于图像压缩的视频监控系统

项目三. 基于图像压缩的视频监控系统 Sprint0-产品设计与规划 基于图像压缩的视频监控系统 1. 产品功能演示 在linux系统上运行程序,弹出登录界面,输入地址.端口信息,弹出视频监控界面,实时传出视频信息. 2. 功能模块分析 采集端: 图像采集子系统 图像编码子系统 传输子系统 主程序 监控端: 传输子系统 图像编码子系统 传输子系统 主程序 Sprint1-基于Epoll架构的采集端程序框架设计 第1课-Epoll机制精通 大纲: v  为什么用Epoll? 阻塞型IO与多路复用

开源倾情奉献:基于.NET打造IP智能网络视频监控系统

转载自 http://www.cnblogs.com/gaochundong/p/opensource_ip_video_surveillance_system_part_1_introduction.html 开源倾情奉献系列链接 开源倾情奉献:基于.NET打造IP智能网络视频监控系统(一)开放源代码 开源倾情奉献:基于.NET打造IP智能网络视频监控系统(二)基础类库介绍 开源倾情奉献:基于.NET打造IP智能网络视频监控系统(三)命令行工具集 开源倾情奉献:基于.NET打造IP智能网络视频