当前位置: 代码迷 >> Android >> Android实时采摘视频,并在PC上显示出采集到的视频的程序
  详细解决方案

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

热度:49   发布时间:2016-05-01 19:22:05.0
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. }

  相关解决方案