1、同样也可以通过定义一个线程类发送文件,如下: /*发送文件线程*/ class MySendFileThread extends Thread private String username; private String ipname; private int port; private byte byteBuffer = new byte1024; private OutputStream outsocket; private ByteArrayOutputStream myoutputstream; public MySendFileThread(ByteArrayOutputStrea
2、m myoutputstream,String username,String ipname,int port) this.myoutputstream = myoutputstream; this.username=username; this.ipname = ipname; this.port=port; myoutputstream.close(); e.printStackTrace(); public void run() try /将图像数据通过Socket发送出去 Socket tempSocket = new Socket(ipname, port); outsocket =
3、 tempSocket.getOutputStream(); /写入头部数据信息 String msg=.URLEncoder.encode(PHONEVIDEO|+username+|,utf-8); byte buffer= msg.getBytes(); outsocket.write(buffer); ByteArrayInputStream inputstream = new ByteArrayInputStream(myoutputstream.toByteArray(); int amount; while (amount = inputstream.read(byteBuffe
4、r) != -1) outsocket.write(byteBuffer, 0, amount); myoutputstream.flush(); myoutputstream.close(); tempSocket.close(); catch (IOException e) e.printStackTrace();而获取摄像头当前图像的关键在于onPreviewFrame()重载函数里面,该函数里面有两个参数,第一个参数为byte,为摄像头当前图像数据,通过YuvImage可以将该数据转换为图片文件,同时还可用对该图片进行压缩和裁剪,将图片进行压缩转换后转换为 ByteArrayOutpu
5、tStream数据,即前面发送文件线程类中所需的文件数据,然后采用线程发送文件,如下代码: Override public void onPreviewFrame(byte data, Camera camera) / TODO Auto-generated method stub /如果没有指令传输视频,就先不传 if(!startSendVideo) return; if(tempPreRateVideoPreRate) tempPreRate+; tempPreRate=0; try if(data!=null) YuvImage image = new YuvImage(data,Vi
6、deoFormatIndex, VideoWidth, VideoHeight,null); if(image! ByteArrayOutputStream outstream = new ByteArrayOutputStream(); /在此设置图片的尺寸和质量 pressToJpeg(new Rect(0, 0, (int)(VideoWidthRatio*VideoWidth), (int)(VideoHeightRatio*VideoHeight), VideoQuality, outstream); outstream.flush(); /启用线程将图像数据发送出去 Thread
7、th = new MySendFileThread(outstream,pUsername,serverUrl,serverPort); th.start(); catch (IOException e) e.printStackTrace();值得注意的是,在调试中YuvImage可能找不到,在模拟机上无法执行该过程,但是编译后在真机中可以通过。此外,以上传输文字字符都是采用UTF编码,在服务器端接收时进行解析时需要采用对应的编码进行解析,否则可能会出现错误解析。Android客户端中关键的部分主要就这些,新建一个Android项目(项目名称为SocketCamera),在main布局中添加
8、一个SurfaceView和两个按钮,如下图所示:然后在SocketCameraActivity.java中添加代码,具体如下:package com.xzy;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.PrintWriter;import .Socket;import .UnknownHostException;import android.app.Ac
9、tivity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.Intent;import android.content.SharedPreferences;import android.graphics.Rect;import android.graphics.YuvImage;import android.hardware.Camera;import android.hardware.Camera.Size;import android.os.Bundl
10、e;import android.preference.PreferenceManager;import android.view.Menu;import android.view.MenuItem;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;import android.view.WindowManager;import android.view.View.OnClickListener;import android.widget.Button;publi
11、c class SocketCameraActivity extends Activity implements SurfaceHolder.Callback,Camera.PreviewCallback private SurfaceView mSurfaceview = null; / SurfaceView对象:(视图组件)视频显示 private SurfaceHolder mSurfaceHolder = null; / SurfaceHolder对象:(抽象接口)SurfaceView支持类 private Camera mCamera = null; / Camera对象,相机预
12、览 /*服务器地址*/ private String pUsername=XZY; private String serverUrl=192.168.1.100 /*服务器端口*/ private int serverPort=8888; /*视频刷新间隔*/ private int VideoPreRate=1; /*当前视频序号*/ private int tempPreRate=0; /*视频质量*/ private int VideoQuality=85; /*发送视频宽度比例*/ private float VideoWidthRatio=1; /*发送视频高度比例*/ privat
13、e float VideoHeightRatio=1; /*发送视频宽度*/ private int VideoWidth=320; /*发送视频高度*/ private int VideoHeight=240; /*视频格式索引*/ private int VideoFormatIndex=0; /*是否发送视频*/ private boolean startSendVideo=false; /*是否连接主机*/ private boolean connectedServer=false; private Button myBtn01, myBtn02; /* Called when the
14、 activity is first created. */ public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); /禁止屏幕休眠 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); mSurfaceview = (SurfaceView)
15、 findViewById(R.id.camera_preview); myBtn01=(Button)findViewById(R.id.button1); myBtn02=(Button)findViewById(R.id.button2); /开始连接主机按钮 myBtn01.setOnClickListener(new OnClickListener() public void onClick(View v) /Common.SetGPSConnected(LoginActivity.this, false); if(connectedServer)/停止连接主机,同时断开传输 sta
16、rtSendVideo=false; connectedServer=false; myBtn02.setEnabled(false); myBtn01.setText(开始连接 myBtn02.setText(开始传输 /断开连接 Thread th = new MySendCommondThread(PHONEDISCONNECT|+pUsername+ th.start(); else/连接主机 /启用线程发送命令PHONECONNECTPHONECONNECT| connectedServer=true; myBtn02.setEnabled(true);停止连接 ); myBtn02
17、.setEnabled(false); myBtn02.setOnClickListener(new OnClickListener() if(startSendVideo)/停止传输视频 else / 开始传输视频 startSendVideo=true;停止传输 public void onStart()/重新启动的时候 mSurfaceHolder = mSurfaceview.getHolder(); / 绑定SurfaceView,取得SurfaceHolder对象 mSurfaceHolder.addCallback(this); / SurfaceHolder加入回调接口 mSu
18、rfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);/ 设置显示器类型,setType必须设置 /读取配置文件 SharedPreferences preParas = PreferenceManager.getDefaultSharedPreferences(SocketCameraActivity.this); pUsername=preParas.getString(Username, serverUrl=preParas.getString(ServerUrl192.168.0.100 String tempStr=
19、preParas.getString(ServerPort8888 serverPort=Integer.parseInt(tempStr); tempStr=preParas.getString(VideoPreRate1 VideoPreRate=Integer.parseInt(tempStr);VideoQuality85 VideoQuality=Integer.parseInt(tempStr);VideoWidthRatio100 VideoWidthRatio=Integer.parseInt(tempStr);VideoHeightRatio VideoHeightRatio
20、=Integer.parseInt(tempStr); VideoWidthRatio=VideoWidthRatio/100f; VideoHeightRatio=VideoHeightRatio/100f; super.onStart(); protected void onResume() super.onResume(); InitCamera(); /*初始化摄像头*/ private void InitCamera() try mCamera = Camera.open(); catch (Exception e) protected void onPause() super.on
21、Pause(); if (mCamera != null) mCamera.setPreviewCallback(null); / !这个必须在前,不然退出出错 mCamera.stopPreview(); mCamera.release(); mCamera = null; catch (Exception e) public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) if (mCamera = null) return; mCamera.setPreviewCallback(this); mC
22、amera.setDisplayOrientation(90); /设置横行录制 /获取摄像头参数 Camera.Parameters parameters = mCamera.getParameters(); Size size = parameters.getPreviewSize(); VideoWidth=size.width; VideoHeight=size.height; VideoFormatIndex=parameters.getPreviewFormat(); mCamera.startPreview(); public void surfaceCreated(SurfaceHolder holder) mCamera.setPreviewDisplay(mSurfaceHolder); public void surfaceDestroyed(SurfaceHolder holder) if (null != mCamera) mCamera.setPreviewCallback(null); retur
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1