安卓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