Android 自定义视频录制终极解决方案(翻转问题)

来源:互联网 发布:hdfs数据删除如何恢复 编辑:程序博客网 时间:2024/04/30 12:54

Android 自定义视频录制翻转问题终极解决方案

  • 自定义视频录制
  • 使用系统可用播放器
  • 前后摄像和视频反转问题
  • 总结

自定义视频录制

mediarecorder = new MediaRecorder();// 创建mediarecorder对象        mCamera = getCameraInstance(); // 获取camera        if (null == mCamera) {            LogUtil.d("没有摄像头!");            return;        }        mCamera.unlock();        mediarecorder.setCamera(mCamera); // 设置录制视频源为Camera(相机)        mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);        mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);        // 设置录制文件质量,格式,分辨率之类,这个全部包括了//        //mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));//注意:这里如果调用了setProfile这个方法的话, 再调用setOutputFormat();则会报错。看看setProfile的源码实现:

public void setProfile(CamcorderProfile profile) {
setOutputFormat(profile.fileFormat);
setVideoFrameRate(profile.videoFrameRate);
setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
setVideoEncodingBitRate(profile.videoBitRate);
setVideoEncoder(profile.videoCodec);
if (profile.quality >= CamcorderProfile.QUALITY_TIME_LAPSE_LOW &&
profile.quality <= CamcorderProfile.QUALITY_TIME_LAPSE_QVGA) {
// Nothing needs to be done. Call to setCaptureRate() enables
// time lapse video recording.
} else {
setAudioEncodingBitRate(profile.audioBitRate);
setAudioChannels(profile.audioChannels);
setAudioSamplingRate(profile.audioSampleRate);
setAudioEncoder(profile.audioCodec);
}
}

所以,一般本人不使用上面这个方法。

mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);/ 设置录制的视频编码h263 h264        mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);        mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);outPath = getOutMediaPath(); //造一个存放3gp文件路径mediarecorder.setOutputFile(outPath);mediarecorder.setPreviewDisplay(surfaceHolder.getSurface()); // 设置视频文件输出的路径  try { // 准备录制      mediarecorder.prepare(); // 开始录制      mediarecorder.start();  } catch (Exception e) {      e.printStackTrace();      e.getLocalizedMessage());  }

此处省略了几处的代码:
1.camera于surfaceview的关联
2.生成视频文件存放路径
3.获取camera对象

使用系统可用播放器

以上讲述自定义视频录制的代码逻辑,下面说说怎么调用系统的播放器来播放你刚成功录制下的视频文件。

Uri data = Uri.fromFile(mediaEntity);Intent intent = new Intent(Intent.ACTION_VIEW);intent.putExtra("configchange", 0); //忽略页面横竖屏切换intent.setDataAndType(data, "video/3gp");startActivity(intent);

注意一个点:video/3gp mediaType可以传video/* 或者具体的类型。
但是有一个小区别了。
像楼主设置的video/3gp 启动的时候会调用出系统中可以用来播放的播放器来提供选择,如UC播放器,爱奇艺播放器等。
如果设置的是video/* 则会直接调用系统的播放器播放。

前后摄像和视频反转问题

这里也是最烦人的地方, 网上的解决方案鱼龙混杂, 试了好多。
贴下几种看到的解决方案:
1.设置 mCamera.set(“rotation”,90); 试过,不行。
2.设置mCamera.setDisplayOrientation(90); 然并卵//

下面说下本人的解决方案:

mCamera.setDisplayOrientation(90); 这句必须要的。
第二:
2.1竖屏情况下:
如果是前置摄像头:
mediaRecorder.setOrientationHint(270);
如果是后置摄像头:
mediaRecorder.setOrientationHint(90);
2.2横情况下:
如果是前置摄像头:
mediaRecorder.setOrientationHint(180);
如果是后置摄像头:
mediaRecorder.setOrientationHint(0);
看了下源码解释:

/**     * Sets the orientation hint for output video playback.     * This method should be called before prepare(). This method will not     * trigger the source video frame to rotate during video recording, but to     * add a composition matrix containing the rotation angle in the output     * video if the output format is OutputFormat.THREE_GPP or     * OutputFormat.MPEG_4 so that a video player can choose the proper     * orientation for playback. Note that some video players may choose     * to ignore the compostion matrix in a video during playback.     *     * @param degrees the angle to be rotated clockwise in degrees.     * The supported angles are 0, 90, 180, and 270 degrees.     * @throws IllegalArgumentException if the angle is not supported.     *     */    public void setOrientationHint(int degrees) {        if (degrees != 0   &&            degrees != 90  &&            degrees != 180 &&            degrees != 270) {            throw new IllegalArgumentException("Unsupported angle: " + degrees);        }        setParameter("video-param-rotation-angle-degrees=" + degrees);    }

大概的意思的设置视频文件生成的翻转角度。那么前面那个
mCamera.setDisplayOrientation(90);应该就是摄像头翻转的角度。
可能有的人有点绕, 转来转去头晕晕的。楼主找了个帖子描述相机拍照成像原理,方便大家理解。传送门:
相机成像原理

总结

主要也是相机设备的成像原理没搞清楚。说实在,现在搞懂了也不太记得住。反正这问题也是困了我好几天了, 看了很多帖子也是被绕进去了, 各种不行,就是斜的, 要么左边斜,要么右边斜,来来回回反正正反面又不一样。两种成像原理。终于搞定了, 记录一下。希望对看官有帮助, thks 你的阅读,祝生活愉快!!

2 0
原创粉丝点击