打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
分享几个Android摄像头采集的YUV数据旋转与镜像翻转的方法

最近在做直播推流方面的工作,因为需要添加美白,滤镜,AR贴图等效果。所以不能简单的使用SufaceView加Camera的方式进行数据的采集,而是需要对Camera采集到的YUV数据进行相关的处理之后然后再进行推流的操作,YUV数据的返回接口。


@Override 
public void onPreviewFrame(byte[] data, Camera camera) { 



  当然,美白,滤镜,AR贴图等效果采用的是第三方的SDK了。除此之外,因为Android摄像头采集的数据都是有一定的旋转的。一般前置摄像头有270度的旋转,后置摄像头有90的旋转。所以要对YUV数据进行一定旋转操作,同时对于前置摄像头的数据还要进行镜像翻转的操作。网上一般比较多的算法是关于旋转的


private byte[] rotateYUVDegree90(byte[] data, int imageWidth, int imageHeight) { 
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2]; 
// Rotate the Y luma 
int i = 0; 
for (int x = 0; x < imageWidth; x++) { 
for (int y = imageHeight - 1; y >= 0; y--) { 
yuv[i] = data[y * imageWidth + x]; 
i++; 


// Rotate the U and V color components 
i = imageWidth * imageHeight * 3 / 2 - 1; 
for (int x = imageWidth - 1; x > 0; x = x - 2) { 
for (int y = 0; y < imageHeight / 2; y++) { 
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x]; 
i--; 
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)]; 
i--; 


return yuv; 


private byte[] rotateYUVDegree270(byte[] data, int imageWidth, int imageHeight) { 
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2]; 
// Rotate the Y luma 
int i = 0; 
for (int x = imageWidth - 1; x >= 0; x--) { 
for (int y = 0; y < imageHeight; y++) { 
yuv[i] = data[y * imageWidth + x]; 
i++; 

}// Rotate the U and V color components 
i = imageWidth * imageHeight; 
for (int x = imageWidth - 1; x > 0; x = x - 2) { 
for (int y = 0; y < imageHeight / 2; y++) { 
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)]; 
i++; 
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x]; 
i++; 


return yuv; 


  上述两个算法分别用于90度旋转(后置摄像头)和270度旋转(前置摄像头),但是对于前置摄像头的YUV数据是需要镜像的,参照上面的算法,实现了前置摄像头的镜像算法。


private byte[] rotateYUVDegree270AndMirror(byte[] data, int imageWidth, int imageHeight) { 
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2]; 
// Rotate and mirror the Y luma 
int i = 0; 
int maxY = 0; 
for (int x = imageWidth - 1; x >= 0; x--) { 
maxY = imageWidth * (imageHeight - 1) + x * 2; 
for (int y = 0; y < imageHeight; y++) { 
yuv[i] = data[maxY - (y * imageWidth + x)]; 
i++; 


// Rotate and mirror the U and V color components 
int uvSize = imageWidth * imageHeight; 
i = uvSize; 
int maxUV = 0; 
for (int x = imageWidth - 1; x > 0; x = x - 2) { 
maxUV = imageWidth * (imageHeight / 2 - 1) + x * 2 + uvSize; 
for (int y = 0; y < imageHeight / 2; y++) { 
yuv[i] = data[maxUV - 2 - (y * imageWidth + x - 1)]; 
i++; 
yuv[i] = data[maxUV - (y * imageWidth + x)]; 
i++; 


return yuv; 


  至于更多关于YUV和推流的知识,目前我还不是很了解。这篇文章也主要是分享这三个算法。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android 摄像头 yuv420sp 格式转 yuv420
图片压缩
Html5 中获取镜像图像
图像小波变换的Visual C++实现
Android拍摄视频流的格式转换(YUV --- RGB) - - JavaEye技术网...
Android如何实现边采集边上传
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服