【Android源码 栏目提醒】:网学会员--在 Android源码 编辑为广大网友搜集整理了:基于android的远程视频监控系统 - 软件工程绩等信息,祝愿广大网友取得需要的信息,参考学习。
基于
android 的远程视频监控系统(已开放
源码)基本过程是
android 作为 socket 客户端将采集到的每一帧图像数据发送出去,PC 作为服务器接收并显示每一帧图像实现远程监控。
图片如下(后来 PC 端加了个拍照功能)。
。
。
(PS。
刚学
android 和 java 不久很多东西还不懂,高手若是知道哪些地方可以继续优化的话还请多多指点下啊)系统代码如下:一、
android 手机客户端(1)AndroidManifest.xml 文件。
添加 camera 和 socket 权限,并设置了程序开始执行的activity、ltxml versionquot1.0quot encodingquotutf-8quotgtltmanifest xmlns:androidquothttp://schemas.
android.com/apk/res/androidquot packagequotorg.wanghai.CameraTestquot
android:versionCodequot1quot
android:versionNamequot1.0quot gt ltuses-sdk
android:minSdkVersionquot15quot /gt lt-- 授予程序使用摄像头的权限 --gt ltuses-permission
android:namequotandroid.permission.CAMERAquot /gt ltuses-feature
android:namequotandroid.hardware.cameraquot /gt ltuses-featureandroid:namequotandroid.hardware.camera.autofocusquot /gt ltuses-permission
android:namequotandroid.permission.INTERNETquot/gt ltuses-permissionandroid:namequotandroid.permission.KILL_BACKGROUND_PROCESSESquot/gt ltuses-permissionandroid:namequotandroid.permission.RESTART_PACKAGESquot/gt ltapplication
android:iconquotdrawable/ic_launcherquot
android:labelquotstring/app_namequot gt ltactivity
android:namequot.GetIPquot
android:screenOrientationquotlandscapequot
android:labelquotstring/app_namequot gt ltintent-filtergt ltaction
android:namequotandroid.intent.action.MAINquot /gt ltcategoryandroid:namequotandroid.intent.category.LAUNCHERquot /gt lt/intent-filtergt lt/activitygt ltactivity
android:namequot.CameraTestquot
android:screenOrientationquotlandscapequot
android:labelquotstring/app_namequot gt lt/activitygt lt/applicationgtlt/manifestgt (2)main.xml 设置 surfaceview 用于摄像头采集图像的预览ltxml versionquot1.0quot encodingquotutf-8quotgtltLinearLayoutxmlns:androidquothttp://schemas.
android.com/apk/res/androidquot
android:layout_widthquotfill_parentquot
android:layout_heightquotfill_parentquot
android:orientationquotverticalquot gt ltSurfaceView
android:idquotid/sViewquot
android:layout_widthquotfill_parentquot
android:layout_heightquotfill_parentquot
android:scaleTypequotfitCenterquot/gtlt/LinearLayoutgt (3)login.xml 登录界面,用于输入服务器 IPltxml versionquot1.0quot encodingquotutf-8quotgtltTableLayoutxmlns:androidquothttp://schemas.
android.com/apk/res/androidquot
android:idquotid/loginFormquot
android:orientationquotverticalquot
android:layout_widthquotfill_parentquot
android:layout_heightquotfill_parentquot gtltTableRowgtltTextView
android:layout_widthquotfill_parentquot
android:layout_heightquotwrap_contentquot
android:textquotIP:quot
android:textSizequot10ptquot /gtlt-- 输入用户名的文本框 --gtltEditText
android:idquotid/ipedittextquot
android:layout_widthquotfill_parentquot
android:layout_heightquotwrap_contentquot
android:digitsquot0123456789.quot
android:hintquot请填写服务器 IPquot
android:selectAllOnFocusquottruequot /gtlt/TableRowgtlt/TableLayoutgt (4)GetIP.java 获得服务器 IP 后,通过 Intent 启动 CameraTest 的 activity,ip 信息通过 Bundle 传递public class GetIP extends Activity String ipname null Override public void onCreateBundle savedInstanceState super.onCreatesavedInstanceState // 设置全屏 requestWindowFeatureWindow.FEATURE_NO_TITLE getWindow.setFlagsWindowManager.LayoutParams.FLAG_FULLSCREENWindowManager.LayoutParams.FLAG_FULLSCREEN setContentViewR.layout.main final Builder builder newAlertDialog.Builderthis //定义一个 AlertDialog.Builder 对象 builder.setTitlequot登录服务器对话框quot // 设置对话框的标题 //装载/res/layout/login.xml 界面布局 TableLayout loginForm TableLayoutgetLayoutInflater.inflate R.layout.loginnull final EditText iptext EditTextloginForm.findViewByIdR.id.ipedittext builder.setViewloginForm // 设置对话框显示的 View 对象 // 为对话框设置一个“登录”按钮 builder.setPositiveButtonquot登录quot // 为按钮设置监听器 new OnClickListener Override public void onClickDialogInterfacedialog int which //此处可执行登录处理 ipname iptext.getText.toString.trim Bundle data new Bundle data.putStringquotipnamequotipname Intent intent newIntentGetIP.thisCameraTest.class intent.putExtrasdata startActivityintent // 为对话框设置一个“取消”按钮 builder.setNegativeButtonquot取消quot new OnClickListener Override public void onClickDialogInterfacedialog int which //取消登录,不做任何事情。
System.exit1 //创建、并显示对话框 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 SuppressWarningsquotdeprecationquot Override public void onCreateBundle savedInstanceState super.onCreatesavedInstanceState // 设置全屏 requestWindowFeatureWindow.FEATURE_NO_TITLE getWindow.setFlagsWindowManager.LayoutParams.FLAG_FULLSCREENWindowManager.LayoutParams.FLAG_FULLSCREEN setContentViewR.layout.main // 获取 IP 地址 Intent intent getIntent Bundle data intent.getExtras ipname data.getStringquotipnamequot screenWidth 640 screenHeight 480 sView SurfaceViewfindViewByIdR.id.sView // 获取界面中 SurfaceView组件 surfaceHolder sView.getHolder // 获得 SurfaceView的 SurfaceHolder // 为 surfaceHolder 添加一个回调监听器 surfaceHolder.addCallbacknew Callback Override public void surfaceChangedSurfaceHolderholder int format int widthint height Override public void surfaceCreatedSurfaceHolderholder initCamera // 打开摄像头 Override public void surfaceDestroyedSurfaceHolderholder // 如果 camera 不为 null 释放摄像头 if camera null if isPreview camera.stopPreview camera.release camera null System.exit0 // 设置该 SurfaceView 自己不维护缓冲 surfaceHolder.setTypeSurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS private void initCamera if isPreview camera Camera.open if camera null ampamp isPreview try Camera.Parameters parameters camera.getParameters parameters.setPreviewSizescreenWidth screenHeight // 设置预览照片的大小 parameters.setPreviewFpsRange2030 // 每秒显示 2030帧 parameters.setPictureFormatImageFormat.NV21 // 设置图片格式 parameters.setPictureSizescreenWidth screenHeight // 设置照片的大小 //camera.setParametersparameters //
android2.3.3 以后不需要此行代码 camera.setPreviewDisplaysurfaceHolder // 通过 SurfaceView 显示取景画面 camera.setPreviewCallbacknewStreamItipname // 设置回调的类 camera.startPreview // 开始预览 camera.autoFocusnull // 自动对焦 catch Exception e e.printStackTrace isPreview true class StreamIt implements Camera.PreviewCallback private String ipname public StreamItString ipname this.ipname ipname Override public void onPreviewFramebyte data Camera camera Size size camera.getParameters.getPreviewSize try //调用 image.compressToJpeg()将 YUV 格式图像数据 data转为 jpg 格式 YuvImage image new YuvImagedata ImageFormat.NV21size.width size.height null ifimagenull ByteArrayOutputStream outstream newByteArrayOutputStream image.compressToJpegnew Rect0 0 size.widthsize.height 80 outstream outstream.flush //启用线程将图像数据发送出去 Thread th new MyThreadoutstreamipname th.start catchException ex Log.equotSysquotquotError:quotex.getMessage class MyThread extends Thread private byte byteBuffer new byte1024 private OutputStream outsocket private ByteArrayOutputStream myoutputstream private String ipname public MyThreadByteArrayOutputStream myoutputstreamStringipname this.myoutputstream myoutputstream this.ipname ipname try myoutputstream.close catch IOException e e.printStackTrace public void run try //将图像数据通过 Socket 发送出去 Socket tempSocket new Socketipname 6000 outsocket tempSocket.getOutputStream ByteArrayInputStream inputstream newByteArrayInputStreammyoutputstream.toByteArray int amount while amount inputstream.readbyteBuffer -1 outsocket.writebyteBuffer 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 mainString args throws IOException ss new ServerSocket6000 final ImageFrame frame new ImageFramess frame.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE frame.setVisibletrue whiletrue frame.panel.getimage frame.repaint / A frame with an image panel/SuppressWarningsquotserialquotclass ImageFrame extends JFrame public ImagePanel panel public JButton jb public ImageFrameServerSocket ss // get screen dimensions Toolkit kit Toolkit.getDefaultToolkit Dimension screenSize kit.getScreenSize int screenHeight screenSize.height int screenWidth screenSize.width // center frame in screen setTitlequotImageTestquot setLocationscreenWidth - DEFAULT_WIDTH / 2 screenHeight -DEFAULT_HEIGHT / 2 setSizeDEFAULT_WIDTH DEFAULT_HEIGHT // add panel to frame this.getContentPane.setLayoutnull panel new ImagePanelss panel.setSize640480 panel.setLocation0 0 addpanel jb new JButtonquot拍照quot jb.setBounds048064050 addjb saveimage saveaction new saveimagess jb.addActionListenersaveaction public static final int DEFAULT_WIDTH 640 public static final int DEFAULT_HEIGHT 560/ A panel that displays a tiled image/SuppressWarningsquotserialquotclass ImagePanel extends JPanel private ServerSocket ss private Image image private InputStream ins public ImagePanelServerSocket ss this.ss ss public void getimage throws IOException Socket s this.ss.accept System.out.printlnquot连接成功quot this.ins s.getInputStream this.image ImageIO.readins this.ins.close public void paintComponentGraphics g super.paintComponentg if image null return g.drawImageimage 0 0 null class saveimage implements ActionListener RandomAccessFile inFile null byte byteBuffer new byte1024 InputStream ins private ServerSocket ss public saveimageServerSocket ss this.ss ss public void actionPerformedActionEvent event try Socket s ss.accept ins s.getInputStream // 文件选择器以当前的目录打开 JFileChooser jfc new JFileChooserquot.quot jfc.showSaveDialognew javax.swing.JFrame // 获取当前的选择文件引用 File savedFile jfc.getSelectedFile // 已经选择了文件 if savedFile null // 读取文件的数据,可以每次以快的方式读取数据 try inFile newRandomAccessFilesavedFile quotrwquot catch FileNotFoundException e e.printStackTrace int amount while amount ins.readbyteBuffer -1 inFile.writebyteBuffer 0 amount inFile.close ins.close s.close javax.swing.JOptionPane.showMessageDialognewjavax.swing.JFrame quot已接保存成功quot quot提示quotjavax.swing.JOptionPane.PLAIN_MESSAGE catch IOException e e.printStackTrace 开放
源码如下(
android 我使用的是 4.03 的 SDK,其它版本请自行更改。
2.3.3 版本以下.
上一篇:
Android 文件系统的移植
下一篇:
实现高校教育体制创新与区域经济发展双赢的思考