使用MediaCodec创建的Android Rotate Surface [英] Android Rotate Surface created using MediaCodec

查看:429
本文介绍了使用MediaCodec创建的Android Rotate Surface的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Camera2 API从Camera捕获视频.我知道我们需要旋转预览,因为Camera2没有等效的setDisplayOrientation().能够使用矩阵旋转预览.

I am using Camera2 APIs to capture video from the Camera. I understand that we need to rotate the preview as Camera2 does not have an equivalent of setDisplayOrientation(). Able to rotate preview using a Matrix.

我还使用MediaCodec API对视频进行编码.但是,当我将手机旋转180度时,编码的视频会反转.我对如何旋转编码的视频一无所知.

I am also using the MediaCodec APIs to encode the video. The encoded video however is inverted when I rotate the phone 180 degrees. I am out of ideas on how to rotate the encoded video.

我在配置时尝试了MediaFormat中的KEY_ROTATION.但是我想这只是在解码时(输出表面).

I tried KEY_ROTATION in MediaFormat while configuring. But I guess this is only while decoding (Output surface).

谢谢!

推荐答案

Camera2在这里并不重要-您可以通过

Camera2 doesn't directly matter here - you can use MediaRecorder if you want with camera2, via MediaRecorder.getSurface() and the MediaRecoder.setOrientationHint() method to set the desired orientation.

如果您使用的是MediaCodec而不是MediaRecorder,则那里没有方向标记.相应的方法是 MediaMuxer.setOrientationHint ,因为MediaRecoder所做的全部工作就是将请求的方向写入视频文件元数据中;实际的帧通常不会由编码器旋转.

If you're using MediaCodec instead of MediaRecorder, then there's no orientation flag there; the corresponding method is MediaMuxer.setOrientationHint, since all MediaRecoder does is write the requested orientation into the video file metadata; the actual frames are not generally rotated by the encoder.

如果您没有将编码后的缓冲区包装到视频文件中,而是以其他方式使用它们(用于视频聊天等的网络传输),则需要将方向与编码后的帧一起传递并应用转换在接收端自己.

If you're not wrapping the encoded buffers into a video file, but instead consuming them in some other way (network transmission for video chat, etc), you need to pass the orientation along with the encoded frames and apply the transform in the receiving end yourself.

这篇关于使用MediaCodec创建的Android Rotate Surface的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆