`
252831020
  • 浏览: 33466 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Android实时采集视频,并在PC上显示出采集到的视频的程序

 
阅读更多



  1. package wl.android.activity;
  2. import java.io.File;
  3. import java.io.RandomAccessFile;
  4. import android.app.Activity;
  5. import android.content.res.Configuration;
  6. import android.graphics.PixelFormat;
  7. import android.hardware.Camera;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.SurfaceHolder;
  11. import android.view.SurfaceView;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. import android.view.SurfaceHolder.Callback;
  15. public class AndroidVideo extends Activity implements Callback,
  16. Camera.PictureCallback {
  17. private SurfaceView mSurfaceView = null;
  18. private SurfaceHolder mSurfaceHolder = null;
  19. private Camera mCamera = null;
  20. private boolean mPreviewRunning = false;
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24.    getWindow().setFormat(PixelFormat.TRANSLUCENT);
  25. requestWindowFeature(Window.FEATURE_NO_TITLE);
  26. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  27. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  28.    setContentView(R.layout.main);
  29.    mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
  30. mSurfaceHolder = mSurfaceView.getHolder();
  31. mSurfaceHolder.addCallback(this);
  32. mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  33. }
  34. @Override
  35. public void onPictureTaken(byte[] data, Camera camera) {
  36. try {
  37. Log.v(“System.out”, “get it!”);
  38. File file = new File(“/sdcard/camera.jpg”);
  39. RandomAccessFile raf = new RandomAccessFile(file, “rw”);
  40. raf.write(data);
  41. raf.close();
  42. } catch (Exception ex) {
  43. Log.v(“System.out”, ex.toString());
  44. }
  45. }
  46. @Override
  47. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  48. int height) {
  49. if (mPreviewRunning) {
  50. mCamera.stopPreview();
  51. }
  52. Camera.Parameters p = mCamera.getParameters();
  53. p.setPreviewSize(width, height);
  54. mCamera.setPreviewCallback(new StreamIt());
  55. mCamera.setParameters(p);
  56. try {
  57. mCamera.setPreviewDisplay(holder);
  58. } catch (Exception ex) {
  59. }
  60. mCamera.startPreview();
  61. mPreviewRunning = true;
  62. }
  63. @Override
  64. public void surfaceCreated(SurfaceHolder holder) {
  65. mCamera = Camera.open();
  66. }
  67. @Override
  68. public void surfaceDestroyed(SurfaceHolder holder) {
  69. mCamera.stopPreview();
  70. mPreviewRunning = false;
  71. mCamera.release();
  72. }
  73. @Override
  74. public void onConfigurationChanged(Configuration newConfig) {
  75. try {
  76. super.onConfigurationChanged(newConfig);
  77. if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  78. } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  79. }
  80. } catch (Exception ex) {
  81. }
  82. }
  83. }
  84. class StreamIt implements Camera.PreviewCallback {
  85. private int tick = 1;
  86. @Override
  87. public void onPreviewFrame(byte[] data, Camera camera) {
  88. // TODO Auto-generated method stub
  89. if (tick == 20) {
  90. System.out.println(“data len: ” + data.length);
  91. try {
  92. File file = new File(“/sdcard/pal.pal”);
  93. if (!file.exists())
  94. file.createNewFile();
  95. RandomAccessFile raf = new RandomAccessFile(file, “rw”);
  96. raf.write(data);
  97. raf.close();
  98. tick++;
  99. } catch (Exception ex) {
  100. Log.v(“System.out”, ex.toString());
  101. }
  102. }
  103. tick++;
  104. }
  105. }
复制代码
xml 布局文件
[p=26, null, left]
  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:layout_width=”fill_parent” android:layout_height=”fill_parent”
  4. android:orientation=”vertical”>
  5. <SurfaceView android:id=”@+id/surface_camera”
  6. android:layout_width=”fill_parent” android:layout_height=”fill_parent”>
  7. </SurfaceView>
  8. </LinearLayout>
复制代码
注意在项目配置文件中还要加上访问权限
  1. <uses-permission android:name=”android.permission.CAMERA” />
复制代码
通过查资料发现,Android每帧的数据流的格式是YUV420下面附上一个将 YUV420转成RGB的函数,
  1. static public void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp, int width, int height) {   
  2.     final int frameSize = width * height;   
  3. if (rgbBuf == null)   
  4.     throw new NullPointerException(“buffer ‘rgbBuf’ is null”);   
  5. if (rgbBuf.length < frameSize * 3)   
  6.     throw new IllegalArgumentException(“buffer ‘rgbBuf’ size “  
  7.              + rgbBuf.length + ” < minimum “ + frameSize * 3);   
  8.   
  9. if (yuv420sp == null)   
  10.     throw new NullPointerException(“buffer ‘yuv420sp’ is null”);   
  11.   
  12. if (yuv420sp.length < frameSize * 3 / 2)   
  13.     throw new IllegalArgumentException(“buffer ‘yuv420sp’ size “ + yuv420sp.length   
  14.              + ” < minimum “ + frameSize * 3 / 2);   
  15.       
  16.     int i = 0, y = 0;   
  17.     int uvp = 0, u = 0, v = 0;   
  18.     int y1192 = 0, r = 0, g = 0, b = 0;   
  19.       
  20.     for (int j = 0, yp = 0; j < height; j++) {   
  21.          uvp = frameSize + (j >> 1) * width;   
  22.          u = 0;   
  23.          v = 0;   
  24.         for (i = 0; i < width; i++, yp++) {   
  25.              y = (0xff & ((int) yuv420sp[yp])) – 16;   
  26.             if (y < 0) y = 0;   
  27.             if ((i & 1) == 0) {   
  28.                  v = (0xff & yuv420sp[uvp++]) – 128;   
  29.                  u = (0xff & yuv420sp[uvp++]) – 128;   
  30.              }   
  31.                
  32.              y1192 = 1192 * y;   
  33.              r = (y1192 + 1634 * v);   
  34.              g = (y1192 – 833 * v – 400 * u);   
  35.              b = (y1192 + 2066 * u);   
  36.                
  37.             if (r < 0) r = 0; else if (r > 262143) r = 262143;   
  38.             if (g < 0) g = 0; else if (g > 262143) g = 262143;   
  39.             if (b < 0) b = 0; else if (b > 262143) b = 262143;   
  40.                
  41.              rgbBuf[yp * 3] = (byte)(r >> 10);   
  42.              rgbBuf[yp * 3 + 1] = (byte)(g >> 10);   
  43.              rgbBuf[yp * 3 + 2] = (byte)(b >> 10);   
  44.          }   
  45.      }   
  46.    }  
复制代码
Android 端
  1. package wl.android.activity;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.net.Socket;
  5. import android.app.Activity;
  6. import android.content.res.Configuration;
  7. import android.graphics.PixelFormat;
  8. import android.hardware.Camera;
  9. import android.os.Bundle;
  10. import android.view.SurfaceHolder;
  11. import android.view.SurfaceView;
  12. import android.view.View;
  13. import android.view.Window;
  14. import android.view.WindowManager;
  15. import android.view.SurfaceHolder.Callback;
  16. import android.view.View.OnClickListener;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. public class AndroidVideo extends Activity implements Callback,OnClickListener{
  20. private SurfaceView mSurfaceView = null;
  21. private SurfaceHolder mSurfaceHolder = null;
  22. private Camera mCamera = null;
  23. private boolean mPreviewRunning = false;
  24. //连接相关
  25. private EditText remoteIP=null;
  26. private Button connect=null;
  27. private String remoteIPStr=null;
  28. //视频数据
  29. private StreamIt streamIt=null;
  30. public static  Kit kit=null;
  31. @Override
  32. public void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. getWindow().setFormat(PixelFormat.TRANSLUCENT);
  35. requestWindowFeature(Window.FEATURE_NO_TITLE);
  36. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  37. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  38. setContentView(R.layout.main);
  39. mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
  40. mSurfaceHolder = mSurfaceView.getHolder();
  41. mSurfaceHolder.addCallback(this);
  42. mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  43. remoteIP=(EditText)this.findViewById(R.id.remoteIP);
  44. connect=(Button)this.findViewById(R.id.connect);
  45. connect.setOnClickListener(this);
  46. }
  47. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  48. int height) {
  49. if (mPreviewRunning) {
  50. mCamera.stopPreview();
  51. }
  52. Camera.Parameters p = mCamera.getParameters();
  53. p.setPreviewSize(width, height);
  54. streamIt=new StreamIt();
  55. kit=new Kit();
  56. mCamera.setPreviewCallback(streamIt);
  57. mCamera.setParameters(p);
  58. try {
  59. mCamera.setPreviewDisplay(holder);
  60. } catch (Exception ex) {
  61. }
  62. mCamera.startPreview();
  63. mPreviewRunning = true;
  64. }
  65. public void surfaceCreated(SurfaceHolder holder) {
  66. mCamera = Camera.open();
  67. }
  68. public void surfaceDestroyed(SurfaceHolder holder) {
  69. mCamera.stopPreview();
  70. mPreviewRunning = false;
  71. mCamera.release();
  72. }
  73. @Override
  74. public void onConfigurationChanged(Configuration newConfig) {
  75. try {
  76. super.onConfigurationChanged(newConfig);
  77. if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  78. } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  79. }
  80. } catch (Exception ex) {
  81. }
  82. }
  83. class Kit implements Runnable {
  84. private boolean run=true;
  85. //        private final int dataLen=57600; //307200 OR 230400 76800 OR 57600
  86. private final int tt=28800;
  87. public void run() {
  88. // TODO Auto-generated method stub
  89. try {
  90. Socket socket = new Socket(remoteIPStr, 8899);
  91. DataOutputStream dos = new DataOutputStream(socket
  92. .getOutputStream());
  93. DataInputStream dis = new DataInputStream(socket
  94. .getInputStream());
  95. while (run) {
  96. dos.write(streamIt.yuv420sp, 0, 28800);
  97. dos.write(streamIt.yuv420sp, 28800, 28800);
  98. dis.readBoolean();
  99. Thread.sleep(155);
  100. }
  101. } catch (Exception ex) {
  102. run=false;
  103. ex.printStackTrace();
  104. }
  105. }
  106. }
  107. @Override
  108. public void onClick(View view) {
  109. // TODO Auto-generated method stub
  110. if(view==connect){//连接函数
  111. remoteIPStr=remoteIP.getText().toString();
  112. new Thread(AndroidVideo.kit).start();
  113. }
  114. }
  115. }
  116. class StreamIt implements Camera.PreviewCallback {
  117. public byte[] yuv420sp =null;
  118. private boolean t=true;
  119. public void onPreviewFrame(byte[] data, Camera camera) {
  120. // TODO Auto-generated method stub
  121. //        if(t){
  122. //            t=false;
  123. //            new Thread(AndroidVideo.kit).start();
  124. //        }
  125. yuv420sp=data;
  126. }
  127. }
复制代码
PC端
  1. import java.awt.Frame;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.awt.Transparency;
  5. import java.awt.color.ColorSpace;
  6. import java.awt.image.BufferedImage;
  7. import java.awt.image.ComponentColorModel;
  8. import java.awt.image.DataBuffer;
  9. import java.awt.image.DataBufferByte;
  10. import java.awt.image.PixelInterleavedSampleModel;
  11. import java.awt.image.Raster;
  12. import java.awt.image.SampleModel;
  13. import java.awt.image.WritableRaster;
  14. import java.io.DataInputStream;
  15. import java.io.DataOutputStream;
  16. import java.net.ServerSocket;
  17. import java.net.Socket;
  18. public class FlushMe extends Frame {
  19. private static final long serialVersionUID = 1L;
  20. private BufferedImage im;
  21. // 图像信息
  22. //    private final int width = 480;
  23. //    private final int height = 320;
  24. private static final int width = 240;
  25. private static final int height = 160;
  26. private static final int numBands = 3;
  27. private static final int dataLen = 57600;//307200 OR 230400//57600 76800
  28. private static final int tt = 28800;//14400;//28800;
  29. // 图像数组
  30. private byte[] byteArray = new byte[width * height * numBands];// 图像RGB数组
  31. private byte[] yuv420sp = new byte[dataLen];// 图像YUV数组
  32. private static final int[] bandOffsets = new int[] { 0, 1, 2 };
  33. private static final SampleModel sampleModel = new PixelInterleavedSampleModel(
  34. DataBuffer.TYPE_BYTE, width, height, 3, width * 3,
  35. bandOffsets);
  36. // ColorModel
  37. private static final ColorSpace cs=ColorSpace.getInstance(ColorSpace.CS_sRGB);
  38. private static final ComponentColorModel cm=new ComponentColorModel(cs, false, false,
  39. Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
  40. public FlushMe() {
  41. super(“Flushing”);
  42. updateIM();
  43. setSize(480, 320);
  44. // 窗口关闭方法
  45. this.addWindowListener(new java.awt.event.WindowAdapter() {
  46. public void windowClosing(java.awt.event.WindowEvent e) {
  47. System.exit(0);
  48. }
  49. });
  50. // 窗口居中
  51. this.setLocationRelativeTo(null);
  52. this.setResizable(false);
  53. this.setVisible(true);
  54. this.getData();
  55. }
  56. public void update(Graphics g){
  57. paint(g);
  58. }
  59. public void paint(Graphics g) {
  60. g.drawImage(im, 0, 0, 480, 320, this);
  61. }
  62. public void getData() {
  63. try {
  64. ServerSocket server = new ServerSocket(8899);
  65. Socket socket = server.accept();
  66. DataInputStream dis = new DataInputStream(socket.getInputStream());
  67. DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
  68. while (true) {
  69. for (int i = 0; i < dataLen / tt; i++) {
  70. dis.read(yuv420sp, i * tt, tt);
  71. }
  72. // 得到数据之后立即更新显示
  73. updateIM();
  74. im.flush();
  75. repaint();
  76. dos.writeBoolean(true);
  77. }
  78. } catch (Exception ex) {
  79. ex.printStackTrace();
  80. }
  81. }
  82. private void updateIM() {
  83. try {
  84. // 解析YUV成RGB格式
  85. decodeYUV420SP(byteArray, yuv420sp, width, height);
  86. DataBuffer dataBuffer = new DataBufferByte(byteArray, numBands);
  87. WritableRaster wr = Raster.createWritableRaster(sampleModel,
  88. dataBuffer, new Point(0, 0));
  89. im = new BufferedImage(cm, wr, false, null);
  90. } catch (Exception ex) {
  91. ex.printStackTrace();
  92. }
  93. }
  94. private static void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp,
  95. int width, int height) {
  96. final int frameSize = width * height;
  97. if (rgbBuf == null)
  98. throw new NullPointerException(“buffer ‘rgbBuf’ is null”);
  99. if (rgbBuf.length < frameSize * 3)
  100. throw new IllegalArgumentException(“buffer ‘rgbBuf’ size ”
  101. + rgbBuf.length + ” < minimum ” + frameSize * 3);
  102. if (yuv420sp == null)
  103. throw new NullPointerException(“buffer ‘yuv420sp’ is null”);
  104. if (yuv420sp.length < frameSize * 3 / 2)
  105. throw new IllegalArgumentException(“buffer ‘yuv420sp’ size ”
  106. + yuv420sp.length + ” < minimum ” + frameSize * 3 / 2);
  107. int i = 0, y = 0;
  108. int uvp = 0, u = 0, v = 0;
  109. int y1192 = 0, r = 0, g = 0, b = 0;
  110. for (int j = 0, yp = 0; j < height; j++) {
  111. uvp = frameSize + (j >> 1) * width;
  112. u = 0;
  113. v = 0;
  114. for (i = 0; i < width; i++, yp++) {
  115. y = (0xff & ((int) yuv420sp[yp])) – 16;
  116. if (y < 0)
  117. y = 0;
  118. if ((i & 1) == 0) {
  119. v = (0xff & yuv420sp[uvp++]) – 128;
  120. u = (0xff & yuv420sp[uvp++]) – 128;
  121. }
  122. y1192 = 1192 * y;
  123. r = (y1192 + 1634 * v);
  124. g = (y1192 – 833 * v – 400 * u);
  125. b = (y1192 + 2066 * u);
  126. if (r < 0)
  127. r = 0;
  128. else if (r > 262143)
  129. r = 262143;
  130. if (g < 0)
  131. g = 0;
  132. else if (g > 262143)
  133. g = 262143;
  134. if (b < 0)
  135. b = 0;
  136. else if (b > 262143)
  137. b = 262143;
  138. rgbBuf[yp * 3] = (byte) (r >> 10);
  139. rgbBuf[yp * 3 + 1] = (byte) (g >> 10);
  140. rgbBuf[yp * 3 + 2] = (byte) (b >> 10);
  141. }
  142. }
  143. }
  144. public static void main(String[] args) {
  145. Frame f = new FlushMe();
  146. }
  147. }

分享到:
评论

相关推荐

    android-pc实时采集及在电脑上显示

    在Android和PC之间实现实时数据传输,特别是视频流的采集与显示,是一项常见的跨平台通信技术,主要涉及以下几个核心知识点: 1. **Android摄像头API**:Android设备上的摄像头数据采集通常通过Camera API或Camera2...

    android摄像上传PC并显示

    总结,实现“Android摄像上传PC并显示”涉及到Android的图像采集与编码、Socket通信,以及PC端的服务器编程、数据解码和图像显示。这个过程中,不仅要关注代码的正确性,还要注意性能优化,确保在各种网络环境下的...

    android实时传输视频Socket

    在Android平台上实现实时视频传输至PC服务器,通常涉及到多个关键技术和步骤。这个项目的核心是利用Socket编程来构建一个可靠的通信管道,将Android设备捕获的视频流高效地发送到PC服务器。以下是对这一主题的详细...

    Android PC机摄像头摄像数据在Android手机上同步显示的源.zip

    该压缩包文件“Android PC机摄像头摄像数据在Android手机上同步显示的源.zip”涉及到的知识点主要集中在Android应用开发和视频流同步技术上。这里我们将深入探讨如何实现这一功能,以及可能涉及的关键技术和挑战。 ...

    android视频采集

    在Android平台上进行视频采集涉及到多个技术领域,包括多媒体处理、网络通信、设备驱动以及应用程序接口(API)的使用。在本项目中,我们看到主要关注的是手机端与PC端的视频采集程序,其中使用华为C8500手机作为...

    Android手机视频实时传输.rar

    这个项目的核心是通过一个名为"T6Client"的服务器端程序和一个运行在Android手机上的应用程序(apk)来完成视频流的实时传输。 首先,我们需要了解视频传输的基本原理。视频传输通常基于网络通信协议,如TCP或UDP,...

    手机WIFI发送采集视频画面至PC

    5. **多线程处理**:为了保证用户体验,Android应用和PC程序通常会使用多线程来处理视频采集、编码、发送和接收任务。这样可以避免主线程被阻塞,保持用户界面的流畅。 6. **端口映射与网络配置**:确保PC和Android...

    基于Android手机实时视频采集与传输软件设计.pdf

    视频采集模块通过调用Android平台的OpenCore多媒体框架来实现视频的采集与编码,而视频传输模块则利用RTP(实时传输协议)来打包视频数据,并发送到PC客户端进行播放。 RTP是一个网络协议,用于实时传输音频和视频...

    基于Socket的Android手机视频实时传输服务器端

    标题中的“基于Socket的Android手机视频实时传输服务器端”指的是使用Socket编程技术,在Android设备上实现视频流的实时传输到服务器端(PC)的应用。Socket是网络编程的基础,它允许两个程序通过网络进行通信,这里...

    android手机与PC视频互通源码

    【Android手机与PC视频互通源码详解】 在移动设备与个人电脑(PC)之间的互联互通已经成为现代数字生活的重要一环,特别是在视频通信领域。这个“android手机与PC视频互通源码”提供了一个很好的学习平台,让...

    Android手机摄像头采集图片上传pc显示

    在Android手机上,摄像头采集图片并上传到PC显示是一个常见的跨平台交互场景,涉及到移动设备与桌面设备的数据传输以及图像处理。在这个过程中,通常需要一个服务端来接收和存储图片,客户端(Android手机)则负责...

    Android视频

    Android视频,比较实用的在Android实时采集视频,并在PC上显示出采集到的视频的程序,包括PC端和Android端程序,基于Android 1.5 在HTC G3上测试通过。

    手机WIFI发送采集视频画面至电脑

    在这个系统中,手机作为视频采集设备,通过Wi-Fi网络将实时视频流传输到电脑上进行显示。 1. **Android开发**:在Android平台上,开发这样的应用需要对Android SDK有深入理解,特别是多媒体和网络编程部分。Android...

    手机端摄像头采集数据,通过USB传输到PC

    PC端接收到数据后,解码并显示在屏幕上,实现同步预览。 当PC端需要拍照时,它发送一个控制命令到手机端,触发手机的拍照操作。手机端的摄像头API接收到命令后执行拍照,保存图片文件,然后通过USB将文件传输给PC。...

    WebRTC的Android实现 源码下载 包括服务器,pc端,android端

    6. **渲染媒体**:在用户界面中显示接收到的视频流,并处理播放音频。 7. **错误处理和调试**:WebRTC提供了丰富的日志和调试工具,可以帮助开发者排查问题。 在提供的压缩包中,可能包含有服务器端、PC端和...

    基于Android平台的远程数据采集系统(PC端)

    在Android设备上,除了接收到的STM32传来的信息,还结合了GPS定位信息,这样可以为数据添加地理位置标签,增强了数据的上下文关联性。数据经过整合后,以特定的数据报格式发送到PC端。Android设备作为移动数据中继站...

    Android java swing 视频监控

    5. **客户端显示**:Android应用接收并解码服务器发送的视频流,显示在用户界面上。 另一方面,Java Swing是用于构建桌面应用程序的Java库。在PC机上作为视频监控客户端,Java Swing可以创建图形用户界面(GUI),...

    基于Android手机的无线视频监控.pdf

    服务器端使用Java语言编写,负责监听特定端口,接收来自客户端的数据,解析命令,还原视频数据,并将这些数据传送到ImagePanel组件进行显示,从而实现实时视频播放。 系统还具备视频截图功能,用户只需点击相应的...

Global site tag (gtag.js) - Google Analytics