天天看点

全网首发:使用安卓MediaCodec Encoder进行编码时的方向问题

  安卓MediaCodec Encoder进行编码,会发现方向总是差90度。怎么办?网上通常说的两种方法,是不行的:

  • setDisplayOrientation()。这个是预览画面使用的,不影响收到的数据。
  • KEY_ROTATE。这个是解码输出用的,编码无效。

  那么怎么办?先旋转,再编码。具体做法是:

  • 初始化的判断
public AndroidVideoEncoder(int width, int height, int rotate, int framerate, int bitrate)
    {
        mRotate = rotate;
        if (mRotate == 90 || mRotate == 270)
        {
            int temp = height;
            height   = width;
            width    = temp;
        }
        super.initParams(null, width, height);
    }      
  • NV21旋转

注意宽高的对换。

if (mRotate == 90)
        {
            MediaCodecKit.NV21_rotate_to_90 (mDataArray, rotateBuffer, mHeight, mWidth);
        }
        else if (mRotate == 180)
        {
            MediaCodecKit.NV21_rotate_to_180(mDataArray, rotateBuffer, mWidth, mHeight);
        }
        else if (mRotate == 270)
        {
            MediaCodecKit.NV21_rotate_to_270(mDataArray, rotateBuffer, mHeight, mWidth);
        }
        else
        {
            return;
        }
        System.arraycopy(rotateBuffer, 0, mDataArray, 0, mDataSize);      

旋转代码参考

https://blog.csdn.net/quantum7/article/details/79762714

封装源码

吾已将源码进行了封装,简单好用。具体位置:

https://github.com/quantum6/Quantum6-CameraCodec-Android

继续阅读