Camera2 获取摄像头数据,使用 MediaCodec 编码,图像旋转 90 度,怎么解决?

2016-10-29 16:33:01 +08:00
 pursll

主要代码如下

     try {
            mMediaCodec = MediaCodec.createEncoderByType("video/avc");
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        MediaFormat format = MediaFormat.createVideoFormat("video/avc", mVideoSize.getWidth(), mVideoSize.getHeight());
        format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
        format.setInteger(MediaFormat.KEY_BIT_RATE, 1250000);
        format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
        format.setInteger(MediaFormat.KEY_ROTATION, 90);
        format.setInteger(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, 1000 / 30);
        mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        mEncoderSurface = mMediaCodec.createInputSurface();

and Camera2 Configuration is

  mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
            mPreviewBuilder.addTarget(mPreviewSurface);
            if (isEncode) {
                mPreviewBuilder.addTarget(mEncoderSurface);
            }
            setUpCaptureRequestBuilder(mPreviewBuilder);
            mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, mBackgroundHandler);

get encode data from MediaCodec

           MediaCodec.BufferInfo mBufferInfo = new MediaCodec.BufferInfo();
            while (true) {
                int outputBufferIndex = venc.dequeueOutputBuffer(mBufferInfo, 0);
                if (outputBufferIndex >= 0) {
                    // outputBuffer is ready to be processed or rendered.
                    ByteBuffer buffer = venc.getOutputBuffer(outputBufferIndex);
                    onEncodedFrame(buffer, mBufferInfo);

getOutputBuffer获取的数据使用 RTMP 推流到服务器的时候是旋转 90 度的,应该怎么修改?

14252 次点击
所在节点    Android
9 条回复
icylord
2016-10-29 17:13:59 +08:00
xingda920813
2016-10-29 17:30:05 +08:00
lijun20020229
2016-10-31 09:49:55 +08:00
先要 camera.setDisplayOrientation(90);
lijun20020229
2016-10-31 09:57:07 +08:00
看错了 上面那条是调整 preview 的 rawdata 会不会影响没测试过
lijun20020229
2016-10-31 10:01:07 +08:00
可以参考楼上的 Sample Camera2RawFragment.java 文件 1109 行
// Initially, output stream images from the Camera2 API will be rotated to the native
// device orientation from the sensor's orientation 。。。
lijun20020229
2016-10-31 10:10:47 +08:00
没猜错的话应该是 format.setInteger(MediaFormat.KEY_ROTATION, 90);转了 90 度的原因
这条不用转 preview 的时候转 90 度就行了
pursll
2016-10-31 11:38:30 +08:00
@lijun20020229 format.setInteger(MediaFormat.KEY_ROTATION, 90); 这个加不加没差别
lijun20020229
2016-10-31 13:16:56 +08:00
@pursll 那看来应该不是这条的问题
但为什么这条没起作用
是否是 api level 设置过低让这条没起作用
KEY_ROTATION

Added in API level 23
String KEY_ROTATION
A key describing the desired clockwise rotation on an output surface. This key is only used when the codec is configured using an output surface. The associated value is an integer, representing degrees. Supported values are 0, 90, 180 or 270. This is an optional field; if not specified, rotation defaults to 0.
lijun20020229
2016-10-31 13:31:58 +08:00
稍微看了下, google sample 里是用 Matrix 转换成 preview 合适的,它大小和角度都要调整

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/316434

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX