- package wl.android.activity;
- import java.io.File;
- import java.io.RandomAccessFile;
- import android.app.Activity;
- import android.content.res.Configuration;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.SurfaceHolder.Callback;
- public class AndroidVideo extends Activity implements Callback,
- Camera.PictureCallback {
- private SurfaceView mSurfaceView = null;
- private SurfaceHolder mSurfaceHolder = null;
- private Camera mCamera = null;
- private boolean mPreviewRunning = false;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
- mSurfaceHolder = mSurfaceView.getHolder();
- mSurfaceHolder.addCallback(this);
- mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
- @Override
- public void onPictureTaken(byte[] data, Camera camera) {
- try {
- Log.v(“System.out”, “get it!”);
- File file = new File(“/sdcard/camera.jpg”);
- RandomAccessFile raf = new RandomAccessFile(file, “rw”);
- raf.write(data);
- raf.close();
- } catch (Exception ex) {
- Log.v(“System.out”, ex.toString());
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- if (mPreviewRunning) {
- mCamera.stopPreview();
- }
- Camera.Parameters p = mCamera.getParameters();
- p.setPreviewSize(width, height);
- mCamera.setPreviewCallback(new StreamIt());
- mCamera.setParameters(p);
- try {
- mCamera.setPreviewDisplay(holder);
- } catch (Exception ex) {
- }
- mCamera.startPreview();
- mPreviewRunning = true;
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- mCamera = Camera.open();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- mCamera.stopPreview();
- mPreviewRunning = false;
- mCamera.release();
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- try {
- super.onConfigurationChanged(newConfig);
- if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
- } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- }
- } catch (Exception ex) {
- }
- }
- }
- class StreamIt implements Camera.PreviewCallback {
- private int tick = 1;
- @Override
- public void onPreviewFrame(byte[] data, Camera camera) {
- // TODO Auto-generated method stub
- if (tick == 20) {
- System.out.println(“data len: ” + data.length);
- try {
- File file = new File(“/sdcard/pal.pal”);
- if (!file.exists())
- file.createNewFile();
- RandomAccessFile raf = new RandomAccessFile(file, “rw”);
- raf.write(data);
- raf.close();
- tick++;
- } catch (Exception ex) {
- Log.v(“System.out”, ex.toString());
- }
- }
- tick++;
- }
- }
复制代码
xml 布局文件
[p=26, null, left]
- <?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/surface_camera”
- android:layout_width=”fill_parent” android:layout_height=”fill_parent”>
- </SurfaceView>
- </LinearLayout>
复制代码
注意在项目配置文件中还要加上访问权限
- <uses-permission android:name=”android.permission.CAMERA” />
复制代码
通过查资料发现,Android每帧的数据流的格式是YUV420下面附上一个将 YUV420转成RGB的函数,
- static public void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp, int width, int height) {
- final int frameSize = width * height;
- if (rgbBuf == null)
- throw new NullPointerException(“buffer ‘rgbBuf’ is null”);
- if (rgbBuf.length < frameSize * 3)
- throw new IllegalArgumentException(“buffer ‘rgbBuf’ size “
- + rgbBuf.length + ” < minimum “ + frameSize * 3);
-
- if (yuv420sp == null)
- throw new NullPointerException(“buffer ‘yuv420sp’ is null”);
-
- if (yuv420sp.length < frameSize * 3 / 2)
- throw new IllegalArgumentException(“buffer ‘yuv420sp’ size “ + yuv420sp.length
- + ” < minimum “ + frameSize * 3 / 2);
-
- int i = 0, y = 0;
- int uvp = 0, u = 0, v = 0;
- int y1192 = 0, r = 0, g = 0, b = 0;
-
- for (int j = 0, yp = 0; j < height; j++) {
- uvp = frameSize + (j >> 1) * width;
- u = 0;
- v = 0;
- for (i = 0; i < width; i++, yp++) {
- y = (0xff & ((int) yuv420sp[yp])) – 16;
- if (y < 0) y = 0;
- if ((i & 1) == 0) {
- v = (0xff & yuv420sp[uvp++]) – 128;
- u = (0xff & yuv420sp[uvp++]) – 128;
- }
-
- y1192 = 1192 * y;
- r = (y1192 + 1634 * v);
- g = (y1192 – 833 * v – 400 * u);
- b = (y1192 + 2066 * u);
-
- if (r < 0) r = 0; else if (r > 262143) r = 262143;
- if (g < 0) g = 0; else if (g > 262143) g = 262143;
- if (b < 0) b = 0; else if (b > 262143) b = 262143;
-
- rgbBuf[yp * 3] = (byte)(r >> 10);
- rgbBuf[yp * 3 + 1] = (byte)(g >> 10);
- rgbBuf[yp * 3 + 2] = (byte)(b >> 10);
- }
- }
- }
复制代码
Android 端
- package wl.android.activity;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.net.Socket;
- import android.app.Activity;
- import android.content.res.Configuration;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.os.Bundle;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.SurfaceHolder.Callback;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class AndroidVideo extends Activity implements Callback,OnClickListener{
- private SurfaceView mSurfaceView = null;
- private SurfaceHolder mSurfaceHolder = null;
- private Camera mCamera = null;
- private boolean mPreviewRunning = false;
- //连接相关
- private EditText remoteIP=null;
- private Button connect=null;
- private String remoteIPStr=null;
- //视频数据
- private StreamIt streamIt=null;
- public static Kit kit=null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
- mSurfaceHolder = mSurfaceView.getHolder();
- mSurfaceHolder.addCallback(this);
- mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- remoteIP=(EditText)this.findViewById(R.id.remoteIP);
- connect=(Button)this.findViewById(R.id.connect);
- connect.setOnClickListener(this);
- }
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- if (mPreviewRunning) {
- mCamera.stopPreview();
- }
- Camera.Parameters p = mCamera.getParameters();
- p.setPreviewSize(width, height);
- streamIt=new StreamIt();
- kit=new Kit();
- mCamera.setPreviewCallback(streamIt);
- mCamera.setParameters(p);
- try {
- mCamera.setPreviewDisplay(holder);
- } catch (Exception ex) {
- }
- mCamera.startPreview();
- mPreviewRunning = true;
- }
- public void surfaceCreated(SurfaceHolder holder) {
- mCamera = Camera.open();
- }
- public void surfaceDestroyed(SurfaceHolder holder) {
- mCamera.stopPreview();
- mPreviewRunning = false;
- mCamera.release();
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- try {
- super.onConfigurationChanged(newConfig);
- if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
- } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- }
- } catch (Exception ex) {
- }
- }
- class Kit implements Runnable {
- private boolean run=true;
- // private final int dataLen=57600; //307200 OR 230400 76800 OR 57600
- private final int tt=28800;
- public void run() {
- // TODO Auto-generated method stub
- try {
- Socket socket = new Socket(remoteIPStr, 8899);
- DataOutputStream dos = new DataOutputStream(socket
- .getOutputStream());
- DataInputStream dis = new DataInputStream(socket
- .getInputStream());
- while (run) {
- dos.write(streamIt.yuv420sp, 0, 28800);
- dos.write(streamIt.yuv420sp, 28800, 28800);
- dis.readBoolean();
- Thread.sleep(155);
- }
- } catch (Exception ex) {
- run=false;
- ex.printStackTrace();
- }
- }
- }
- @Override
- public void onClick(View view) {
- // TODO Auto-generated method stub
- if(view==connect){//连接函数
- remoteIPStr=remoteIP.getText().toString();
- new Thread(AndroidVideo.kit).start();
- }
- }
- }
- class StreamIt implements Camera.PreviewCallback {
- public byte[] yuv420sp =null;
- private boolean t=true;
- public void onPreviewFrame(byte[] data, Camera camera) {
- // TODO Auto-generated method stub
- // if(t){
- // t=false;
- // new Thread(AndroidVideo.kit).start();
- // }
- yuv420sp=data;
- }
- }
复制代码
PC端
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Transparency;
- import java.awt.color.ColorSpace;
- import java.awt.image.BufferedImage;
- import java.awt.image.ComponentColorModel;
- import java.awt.image.DataBuffer;
- import java.awt.image.DataBufferByte;
- import java.awt.image.PixelInterleavedSampleModel;
- import java.awt.image.Raster;
- import java.awt.image.SampleModel;
- import java.awt.image.WritableRaster;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class FlushMe extends Frame {
- private static final long serialVersionUID = 1L;
- private BufferedImage im;
- // 图像信息
- // private final int width = 480;
- // private final int height = 320;
- private static final int width = 240;
- private static final int height = 160;
- private static final int numBands = 3;
- private static final int dataLen = 57600;//307200 OR 230400//57600 76800
- private static final int tt = 28800;//14400;//28800;
- // 图像数组
- private byte[] byteArray = new byte[width * height * numBands];// 图像RGB数组
- private byte[] yuv420sp = new byte[dataLen];// 图像YUV数组
- private static final int[] bandOffsets = new int[] { 0, 1, 2 };
- private static final SampleModel sampleModel = new PixelInterleavedSampleModel(
- DataBuffer.TYPE_BYTE, width, height, 3, width * 3,
- bandOffsets);
- // ColorModel
- private static final ColorSpace cs=ColorSpace.getInstance(ColorSpace.CS_sRGB);
- private static final ComponentColorModel cm=new ComponentColorModel(cs, false, false,
- Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
- public FlushMe() {
- super(“Flushing”);
- updateIM();
- setSize(480, 320);
- // 窗口关闭方法
- this.addWindowListener(new java.awt.event.WindowAdapter() {
- public void windowClosing(java.awt.event.WindowEvent e) {
- System.exit(0);
- }
- });
- // 窗口居中
- this.setLocationRelativeTo(null);
- this.setResizable(false);
- this.setVisible(true);
- this.getData();
- }
- public void update(Graphics g){
- paint(g);
- }
- public void paint(Graphics g) {
- g.drawImage(im, 0, 0, 480, 320, this);
- }
- public void getData() {
- try {
- ServerSocket server = new ServerSocket(8899);
- Socket socket = server.accept();
- DataInputStream dis = new DataInputStream(socket.getInputStream());
- DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
- while (true) {
- for (int i = 0; i < dataLen / tt; i++) {
- dis.read(yuv420sp, i * tt, tt);
- }
- // 得到数据之后立即更新显示
- updateIM();
- im.flush();
- repaint();
- dos.writeBoolean(true);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- private void updateIM() {
- try {
- // 解析YUV成RGB格式
- decodeYUV420SP(byteArray, yuv420sp, width, height);
- DataBuffer dataBuffer = new DataBufferByte(byteArray, numBands);
- WritableRaster wr = Raster.createWritableRaster(sampleModel,
- dataBuffer, new Point(0, 0));
- im = new BufferedImage(cm, wr, false, null);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- private static void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp,
- int width, int height) {
- final int frameSize = width * height;
- if (rgbBuf == null)
- throw new NullPointerException(“buffer ‘rgbBuf’ is null”);
- if (rgbBuf.length < frameSize * 3)
- throw new IllegalArgumentException(“buffer ‘rgbBuf’ size ”
- + rgbBuf.length + ” < minimum ” + frameSize * 3);
- if (yuv420sp == null)
- throw new NullPointerException(“buffer ‘yuv420sp’ is null”);
- if (yuv420sp.length < frameSize * 3 / 2)
- throw new IllegalArgumentException(“buffer ‘yuv420sp’ size ”
- + yuv420sp.length + ” < minimum ” + frameSize * 3 / 2);
- int i = 0, y = 0;
- int uvp = 0, u = 0, v = 0;
- int y1192 = 0, r = 0, g = 0, b = 0;
- for (int j = 0, yp = 0; j < height; j++) {
- uvp = frameSize + (j >> 1) * width;
- u = 0;
- v = 0;
- for (i = 0; i < width; i++, yp++) {
- y = (0xff & ((int) yuv420sp[yp])) – 16;
- if (y < 0)
- y = 0;
- if ((i & 1) == 0) {
- v = (0xff & yuv420sp[uvp++]) – 128;
- u = (0xff & yuv420sp[uvp++]) – 128;
- }
- y1192 = 1192 * y;
- r = (y1192 + 1634 * v);
- g = (y1192 – 833 * v – 400 * u);
- b = (y1192 + 2066 * u);
- if (r < 0)
- r = 0;
- else if (r > 262143)
- r = 262143;
- if (g < 0)
- g = 0;
- else if (g > 262143)
- g = 262143;
- if (b < 0)
- b = 0;
- else if (b > 262143)
- b = 262143;
- rgbBuf[yp * 3] = (byte) (r >> 10);
- rgbBuf[yp * 3 + 1] = (byte) (g >> 10);
- rgbBuf[yp * 3 + 2] = (byte) (b >> 10);
- }
- }
- }
- public static void main(String[] args) {
- Frame f = new FlushMe();
- }
- }
分享到:
相关推荐
在Android和PC之间实现实时数据传输,特别是视频流的采集与显示,是一项常见的跨平台通信技术,主要涉及以下几个核心知识点: 1. **Android摄像头API**:Android设备上的摄像头数据采集通常通过Camera API或Camera2...
总结,实现“Android摄像上传PC并显示”涉及到Android的图像采集与编码、Socket通信,以及PC端的服务器编程、数据解码和图像显示。这个过程中,不仅要关注代码的正确性,还要注意性能优化,确保在各种网络环境下的...
在Android平台上实现实时视频传输至PC服务器,通常涉及到多个关键技术和步骤。这个项目的核心是利用Socket编程来构建一个可靠的通信管道,将Android设备捕获的视频流高效地发送到PC服务器。以下是对这一主题的详细...
该压缩包文件“Android PC机摄像头摄像数据在Android手机上同步显示的源.zip”涉及到的知识点主要集中在Android应用开发和视频流同步技术上。这里我们将深入探讨如何实现这一功能,以及可能涉及的关键技术和挑战。 ...
在Android平台上进行视频采集涉及到多个技术领域,包括多媒体处理、网络通信、设备驱动以及应用程序接口(API)的使用。在本项目中,我们看到主要关注的是手机端与PC端的视频采集程序,其中使用华为C8500手机作为...
这个项目的核心是通过一个名为"T6Client"的服务器端程序和一个运行在Android手机上的应用程序(apk)来完成视频流的实时传输。 首先,我们需要了解视频传输的基本原理。视频传输通常基于网络通信协议,如TCP或UDP,...
5. **多线程处理**:为了保证用户体验,Android应用和PC程序通常会使用多线程来处理视频采集、编码、发送和接收任务。这样可以避免主线程被阻塞,保持用户界面的流畅。 6. **端口映射与网络配置**:确保PC和Android...
视频采集模块通过调用Android平台的OpenCore多媒体框架来实现视频的采集与编码,而视频传输模块则利用RTP(实时传输协议)来打包视频数据,并发送到PC客户端进行播放。 RTP是一个网络协议,用于实时传输音频和视频...
标题中的“基于Socket的Android手机视频实时传输服务器端”指的是使用Socket编程技术,在Android设备上实现视频流的实时传输到服务器端(PC)的应用。Socket是网络编程的基础,它允许两个程序通过网络进行通信,这里...
【Android手机与PC视频互通源码详解】 在移动设备与个人电脑(PC)之间的互联互通已经成为现代数字生活的重要一环,特别是在视频通信领域。这个“android手机与PC视频互通源码”提供了一个很好的学习平台,让...
在Android手机上,摄像头采集图片并上传到PC显示是一个常见的跨平台交互场景,涉及到移动设备与桌面设备的数据传输以及图像处理。在这个过程中,通常需要一个服务端来接收和存储图片,客户端(Android手机)则负责...
Android视频,比较实用的在Android实时采集视频,并在PC上显示出采集到的视频的程序,包括PC端和Android端程序,基于Android 1.5 在HTC G3上测试通过。
在这个系统中,手机作为视频采集设备,通过Wi-Fi网络将实时视频流传输到电脑上进行显示。 1. **Android开发**:在Android平台上,开发这样的应用需要对Android SDK有深入理解,特别是多媒体和网络编程部分。Android...
PC端接收到数据后,解码并显示在屏幕上,实现同步预览。 当PC端需要拍照时,它发送一个控制命令到手机端,触发手机的拍照操作。手机端的摄像头API接收到命令后执行拍照,保存图片文件,然后通过USB将文件传输给PC。...
6. **渲染媒体**:在用户界面中显示接收到的视频流,并处理播放音频。 7. **错误处理和调试**:WebRTC提供了丰富的日志和调试工具,可以帮助开发者排查问题。 在提供的压缩包中,可能包含有服务器端、PC端和...
在Android设备上,除了接收到的STM32传来的信息,还结合了GPS定位信息,这样可以为数据添加地理位置标签,增强了数据的上下文关联性。数据经过整合后,以特定的数据报格式发送到PC端。Android设备作为移动数据中继站...
5. **客户端显示**:Android应用接收并解码服务器发送的视频流,显示在用户界面上。 另一方面,Java Swing是用于构建桌面应用程序的Java库。在PC机上作为视频监控客户端,Java Swing可以创建图形用户界面(GUI),...
服务器端使用Java语言编写,负责监听特定端口,接收来自客户端的数据,解析命令,还原视频数据,并将这些数据传送到ImagePanel组件进行显示,从而实现实时视频播放。 系统还具备视频截图功能,用户只需点击相应的...