Android 录制 H264视频并上传至服务器 例子源码

Android 录制 H264视频并上传至服务器 例子源码
资源下载需要积分:2
  • 4302***@qq.com 开通了 铂金会员
    11小时前
  • 1878***@qq.com 开通了 黄金会员
    22小时前
  • 5082***@qq.com 开通了 铂金会员
    1天前
  • 6875***@qq.com 开通了 黄金会员
    1天前
  • 1874***@qq.com 开通了 钻石会员
    1天前
  • 1447***@qq.com 开通了 钻石会员
    1天前
  • 7353***@qq.com 开通了 钻石会员
    1天前
  • 1273***@qq.com 开通了 黄金会员
    1天前
  • 8029***@qq.com 开通了 铂金会员
    1天前
  • 6497***@qq.com 开通了 黄金会员
    2天前
  • 4813***@qq.com 开通了 黄金会员
    2天前
  • 1559***@qq.com 开通了 铂金会员
    2天前
  • 6070***@qq.com 开通了 永久会员
    3天前
  • 5606***@qq.com 开通了 永久会员
    3天前
  • 4070***@qq.com 开通了 黄金会员
    3天前
  • 8895***@qq.com 开通了 铂金会员
    3天前
  • 2286***@qq.com 开通了 钻石会员
    3天前
  • 5929***@qq.com 开通了 永久会员
    3天前
  • 源码分类:游戏源码
  • 发布日期:2019-08-29 11:51
  • 展示次数:1398
  • 下载次数:1
  • 收藏次数:4
  • 分享人员:本站会员

Android h264视频采集,通过LOCALSOCKET上传到服务器上-android video capture and playbackpackage com.android.getH264FormMediaRecorder;   import Java.io.bufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;   import java.net.Socket;   import java.net.UnknownHostException; import java.security.InvalidParameterException;       import android.R.integer; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.hardware.Camera.PictureCallback;       import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioTrack; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.net.LocalserverSocket; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.os.Bundle; import android.os.Process; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.SurfaceHolder.Callback; import android.widget.Toast;   public class VideoCameraActivity extends Activity implements Callback, Runnable {                       private String ip=null;     private SurfaceHolder mSurfaceHolder;     private boolean mMediaRecorderRecording = false;     private static Camera m_camera=null;           private CameraStreamer streamer = null;     private MediaPlayer mMediaPlayer=null;     /** Called when the activity is first created. */     @override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);        // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);                      InitSurfaceView();         InitMediaSharePreference();         streamer =new CameraStreamer();             }       private PictureCallback jpegCallback = new PictureCallback() {         public void onPictureTaken(byte[] _data, Camera _camera) {             // TODO Handle JPEG image data               /* onPictureTaken传入的第一个参数即为相片的byte */             Bitmap bm = BitmapFactory.decodeByteArray(_data, 0, _data.length);                 File myCaptureFile = new File("/sdcard/1234566.jpg");             try {                 BufferedOutputStream bos = new BufferedOutputStream(                         new FileOutputStream(myCaptureFile));                   /* 采用压缩转档方法 */                 bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);                                   /* 调用flush()方法,更新BufferStream */                 bos.flush();                                   /* 结束OutputStream */                 bos.close();                 Log.e("","file length :"  myCaptureFile.length());                 int length =(int) myCaptureFile.length();                 DataInputStream data_input= new DataInputStream(new FileInputStream(myCaptureFile));                 byte [] buffer =new byte[length];                 pic_data_output.write(intToByteArray(length));                 int read =0,temp=0;                 while(read<length)                 {                     temp= data_input.read(buffer, read, length-read);                     if(temp==-1)                     {                         Log.e("", "no data get wait for data coming.....");                         return;                     }                     read  = temp;                 }                 pic_data_output.write(buffer,0, length);                 pic_data_output.flush();                                   /* 将拍照下来且存储完毕的图文件,显示出来 */                 // mImageView01.seTimageBitmap(bm);                   /* 显示完图文件,立即重置相机,并关闭预览 */                 resetCamera();                 pic_data_input.close();                 pic_data_output.close();                 cap_picture_fd.close();               } catch (Exception e) {                 e.printStackTrace();             }         }     };           private void resetCamera()     {                  if (m_camera != null )       {           m_camera.lock();           m_camera.stopPreview();                     m_camera.release();           m_camera = null;                     }     }     SharedPreferences mediaPreference;     private final String mediaShare = "media";             private void InitMediaSharePreference()     {         mediaPreference = this.getSharedPreferences(mediaShare, Context.MODE_PRIVATE);               }                    LocalServerSocket lss =null;      LocalSocket receiver=null;      LocalSocket sender=null;       private void InitLocalSocket(){         try {             lss = new LocalServerSocket ("h264");             receiver = new LocalSocket ();                           receiver.connect(new LocalSocketAddress("h264"));             receiver.setReceiveBufferSize(500000);             receiver.setSendBufferSize(500000);                           sender = lss.accept();             sender.setReceiveBufferSize(500000);             sender.setSendBufferSize(500000);         } catch (IOException e) {             e.printStackTrace();             this.finish();             return;         }     }        LocalServerSocket lss2 =null;     LocalSocket receiver2=null;     LocalSocket sender2=null;              private SurfaceView mSurfaceView;     private void InitSurfaceView()     {         mSurfaceView = (SurfaceView)this.findViewById(R.id.surface_camera);         SurfaceHolder holder = mSurfaceView.getHolder();         holder.addCallback(this);         holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);               }             public void surfaceChanged(SurfaceHolder holder, int format, int width,             int height)     {         mSurfaceHolder  = holder;         if(!mMediaRecorderRecording)         {             InitLocalSocket();             getSPSAndPPS();             if (!initializeVideo()) {               return;                }             startVideoRecording();         }     }           //get sps and pps if have record     private void getSPSAndPPS(){         StartMdatPlace = mediaPreference.getInt(                 String.format("mdata_%d%d.mdat", videoWidth,videoHeight),                 -1);         if(StartMdatPlace!=-1)         {             byte[] temp = new byte[100];             try {                 FileInputStream file_in = VideoCameraActivity.this.openFileInput(                         String.format("%d%d.sps", videoWidth,videoHeight));                                   int index = 0;                 int read=0;                 while(true)                 {                     read = file_in.read(temp,index,10);                     if(read==-1) break;                     else index  = read;                 }                 Log.e("", "sps length:" index);                 SPS = new byte[index];                 system.arraycopy(temp, 0, SPS, 0, index);                                                  file_in.close();                                   index =0;                 //read PPS                 file_in = VideoCameraActivity.this.openFileInput(                         String.format("%d%d.pps", videoWidth,videoHeight));                 while(true)                 {                     read = file_in.read(temp,index,10);                     if(read==-1) break;                     else index =read;                 }                 Log.e("", "pps length:" index);                 PPS = new byte[index];                 System.arraycopy(temp, 0, PPS, 0, index);                               } catch (FileNotFoundException e) {                 e.printStackTrace();             } catch (IOException e) {                 e.printStackTrace();             }         }         else         {             SPS = null;             PPS = null;         }     }                 public void surfaceCreated(SurfaceHolder holder)     {         mSurfaceHolder = holder;     }           public void surfaceDestroyed(SurfaceHolder holder)     {         releaseMediaRecorder();         if (data_output!=null) {             try {                 data_output.close();                 data_output=null;             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }         if (client_fd != null) {             try {                 client_fd.close();                 client_fd=null;             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }     }           private static MediaRecorder  mMediaRecorder = null;     private int videoWidth = 320;     private int videoHeight = 240;     private int videorate = 15;         private String fd = "data/data/com.android.getH264FormMediaRecorder/h264.3gp";           private boolean initializeVideo(){         if(mSurfaceHolder==null)             return false;           //      TODO:get supported width and height                   if (!init_streaming_socket()) {             return false;         }         mMediaRecorderRecording = true;         if(mMediaRecorder == null)             mMediaRecorder = new MediaRecorder ();         else             mMediaRecorder.reset();           try         {             m_camera = Camera.open();         } catch (RuntimeException e)          {             // TODO Auto-generated catch block             e.printStackTrace();         }         m_camera.setDisplayOrientation(90);           m_camera.unlock();           mMediaRecorder.setCamera(m_camera);         mMediaRecorder.setVideosource(MediaRecorder.VideoSource.CAMERA);         mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);         mMediaRecorder.setVideoEncodingBitRate(1000);         mMediaRecorder.setVideoFrameRate(videoRate);         mMediaRecorder.setVideoSize(videoWidth, videoHeight);         mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);         mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());                  mMediaRecorder.setMaxDuration(0);         mMediaRecorder.setMaxFileSize(0);         if(SPS==null)         {             mMediaRecorder.setOutputFile(fd);         }         else         {             mMediaRecorder.setOutputFile(sender.getFileDescriptor());         }                             try {             mMediaRecorder.prepare();             mMediaRecorder.start();         } catch (IllegalStateException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();             releaseMediaRecorder();         }                 return true;     }                 private void startVideoRecording()     {         new Thread (streaming).start();                   new Thread (picture_thread).start();         new Thread (voice_thread).start();         //new Thread (recv_voice_thread).start();             }       private Runnable voice_thread=new Runnable() {                   @Override         public void run() {             // TODO Auto-generated method stub             Intent i=getIntent();             String ip=i.getStringExtra("ip");                        //while (mMediaRecorderRecording) {                 try {                     streamer.setup(mSurfaceHolder,ip);                   } catch (Exception e) {                     // TODO: handle exception                     Log.e("","error");                     return;                 }                 streamer.start();                 while (mMediaRecorderRecording)                 {                     ;                 }                 //streamer.stop();             //}         }     };           private Runnable streaming = new Runnable()     {         public void run() {             try {                                   if(SPS == null)                 {                     Log.e("", "Rlease MediaRecorder and&

Android 录制 H264视频并上传至服务器 例子源码

评论


亲,登录后才可以留言!

来源:Android 录制 H264视频并上传至服务器 例子源码

星火源码网所有素材均为本站用户上传,仅供学习与参考,请勿用于商业用途,如有侵犯您的版权请联系客服服务QQ
本站提供各类程序源码素材。
如果对本站有任何意见请点击右侧侧边栏的反馈意见,我们会及时处理。

相似素材

本站会员

这个家伙很懒未写签名~