原理:https://blog.csdn.net/andrew57/article/details/79644442
1、直接使用libyuv庫 libyuv::I420Scale
注意:要提前給原始YUV圖像分配補邊后內存,否則可能崩潰
2、自己實現,原始YUV保持原本的大小就可以
代碼:
1 int offset_in = 0; 2 int offset_out = 0; 3 for (int i = 0; i < inHeight; ++i) 4 { 5 // Y 6 memcpy(YUV_out + offset_out, YUV_in + offset_in, inWidth); 7 offset_out += outWidth; 8 offset_in += inWidth; 9 } 10 offset_out += (outHeight - inHeight) * outWidth; 11 for (int i = 0; i < inHeight / 2; ++i) 12 { 13 // U 14 memcpy(YUV_out + offset_out, YUV_in + offset_in, inWidth / 2); 15 offset_out += outWidth / 2; 16 offset_in += inWidth / 2; 17 } 18 offset_out += (outHeight - inHeight) * outWidth / 4; 19 for (int i = 0; i < inHeight / 2; ++i) 20 { 21 // V 22 memcpy(YUV_out + offset_out, YUV_in + offset_in, inWidth / 2); 23 offset_out += outWidth / 2; 24 offset_in += inWidth / 2; 25 }