android 使用javacv进行录像[模仿vine]

来源:互联网 发布:网络克隆安装windows7 编辑:程序博客网 时间:2024/06/11 23:07

http://blog.csdn.net/wodong/article/details/20772045

相关代码如下:

1. 初始化 ffmpeg_recorder

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void initRecorder() {  
  2.     String ffmpeg_link = parentPath + "/" + "video.mp4";  
  3.     Log.w(LOG_TAG, "init recorder");  
  4.   
  5.     if (yuvIplimage == null) {  
  6.         yuvIplimage = IplImage.create(cameraManager.getDefaultSize().width,  
  7.                 cameraManager.getDefaultSize().height, IPL_DEPTH_8U, 2);  
  8.         Log.i(LOG_TAG, "create yuvIplimage");  
  9.     }  
  10.   
  11.     Log.i(LOG_TAG, "ffmpeg_url: " + ffmpeg_link);  
  12.     recorder = new FFmpegFrameRecorder(ffmpeg_link,  
  13.             cameraManager.getDefaultSize().width,  
  14.             cameraManager.getDefaultSize().height, 1);  
  15.     recorder.setFormat("mp4");  
  16.     recorder.setSampleRate(sampleAudioRateInHz);  
  17.     // Set in the surface changed method  
  18.     recorder.setFrameRate(frameRate);  
  19.   
  20.     Log.i(LOG_TAG, "recorder initialize success");  
  21.   
  22.     audioRecordRunnable = new AudioRecordRunnable();  
  23.     audioThread = new Thread(audioRecordRunnable);  
  24.     try {  
  25.         recorder.start();  
  26.     } catch (Exception e) {  
  27.         // TODO Auto-generated catch block  
  28.         e.printStackTrace();  
  29.     }  
  30.     audioThread.start();  
  31. }  

2. 捕捉摄像头视频数据:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void onPreviewFrame(byte[] data, Camera camera) {  
  2.     int during = checkIfMax(new Date().getTime());  
  3.     /* get video data */  
  4.     if (yuvIplimage != null && isStart) {  
  5.         yuvIplimage.getByteBuffer().put(data);  
  6.         //yuvIplimage = rotateImage(yuvIplimage.asCvMat(), 90).asIplImage();  
  7.         Log.v(LOG_TAG, "Writing Frame");  
  8.         try {  
  9.             System.out.println(System.currentTimeMillis() - videoStartTime);  
  10.             if (during < 6000) {  
  11.                 recorder.setTimestamp(1000 * during);  
  12.                 recorder.record(yuvIplimage);  
  13.             }  
  14.         } catch (FFmpegFrameRecorder.Exception e) {  
  15.             Log.v(LOG_TAG, e.getMessage());  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
  19. }  

3. 捕捉声音数据:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class AudioRecordRunnable implements Runnable {  
  2.   
  3.     @Override  
  4.     public void run() {  
  5.         android.os.Process  
  6.                 .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);  
  7.   
  8.         // Audio  
  9.         int bufferSize;  
  10.         short[] audioData;  
  11.         int bufferReadResult;  
  12.   
  13.         bufferSize = AudioRecord.getMinBufferSize(sampleAudioRateInHz,  
  14.                 AudioFormat.CHANNEL_CONFIGURATION_MONO,  
  15.                 AudioFormat.ENCODING_PCM_16BIT);  
  16.         audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,  
  17.                 sampleAudioRateInHz,  
  18.                 AudioFormat.CHANNEL_CONFIGURATION_MONO,  
  19.                 AudioFormat.ENCODING_PCM_16BIT, bufferSize);  
  20.   
  21.         audioData = new short[bufferSize];  
  22.   
  23.         Log.d(LOG_TAG, "audioRecord.startRecording()");  
  24.         audioRecord.startRecording();  
  25.   
  26.         /* ffmpeg_audio encoding loop */  
  27.         while (!isFinished) {  
  28.             // Log.v(LOG_TAG,"recording? " + recording);  
  29.             bufferReadResult = audioRecord.read(audioData, 0,  
  30.                     audioData.length);  
  31.             if (bufferReadResult > 0) {  
  32.                 // Log.v(LOG_TAG, "bufferReadResult: " + bufferReadResult);  
  33.                 // If "recording" isn't true when start this thread, it  
  34.                 // never get's set according to this if statement...!!!  
  35.                 // Why? Good question...  
  36.                 if (isStart) {  
  37.                     try {  
  38.                         Buffer[] barray = new Buffer[1];  
  39.                         barray[0] = ShortBuffer.wrap(audioData, 0,  
  40.                                 bufferReadResult);  
  41.                         recorder.record(barray);  
  42.                         // Log.v(LOG_TAG,"recording " + 1024*i + " to " +  
  43.                         // 1024*i+1024);  
  44.                     } catch (FFmpegFrameRecorder.Exception e) {  
  45.                         Log.v(LOG_TAG, e.getMessage());  
  46.                         e.printStackTrace();  
  47.                     }  
  48.                 }  
  49.             }  
  50.         }  
  51.         Log.v(LOG_TAG, "AudioThread Finished, release audioRecord");  
  52.   
  53.         /* encoding finish, release recorder */  
  54.         if (audioRecord != null) {  
  55.             audioRecord.stop();  
  56.             audioRecord.release();  
  57.             audioRecord = null;  
  58.             Log.v(LOG_TAG, "audioRecord released");  
  59.         }  
  60.     }  
  61. }  

源码:

http://download.csdn.net/detail/wodong/7008905


0 0