Android --libyuv

1,220 阅读3分钟

java方式旋转摄像头数据

摄像头nv21数据旋转90度(方法1)

    byte[] mNv21DataBuffer;
    /**
     * 将 NV21 格式的图片数据顺时针旋转 90 度
     * 后置摄像头顺时针旋转 90 度
     * 前置摄像头逆时针旋转 90 度
     * @param data
     */
    private void nv21PictureDataClockwiseRotation90(byte[] data,int mWidth,int mHeight,int mCameraFacing){
        // Y 灰度数据的个数
        int YByteCount = mWidth * mHeight;

        // 色彩度 U, 饱和度 V 数据高度
        int UVByteHeight = mHeight / 2;

        // 色彩度 U, 饱和度 V 数据个数
        int UVByteCount = YByteCount / 4;

        mNv21DataBuffer = new byte[YByteCount*3/2];
        // 数据处理索引值, 用于记录写入到 mNv21DataBuffer 中的元素个数
        // 及下一个将要写入的元素的索引
        int positionIndex = 0;

        /*
            后置摄像头处理
            后置摄像头需要将图像顺时针旋转 90 度
         */
        if(mCameraFacing == Camera.CameraInfo.CAMERA_FACING_BACK){

            /*
                读取 Y 灰度数据
                顺时针旋转 90 度
                外层循环 : 逐行遍历, 从第一行遍历到最后一行
                内存循环 : 遍历每一行时, 从底部遍历到顶部
             */
            for (int i = 0; i < mWidth; i++) {
                // 第 i 行, 从每一列的最后一个像素 ( 索引 mHeight - 1 ) 遍历到第一个像素 ( 索引 0 )
                for (int j = mHeight - 1; j >= 0; j--) {
                    // 将读取到的 Y 灰度值存储到 mNv21DataBuffer 缓冲区中
                    mNv21DataBuffer[positionIndex++] = data[mWidth * j + i];
                }
            }

            /*
                读取 UV 数据

                Y 数据的高度与图像高度相等
                UV 数据高度相当于 Y 数据高度的一半

                UV 数据排列 : V 色彩值在前, U 饱和度在后, UV 数据交替排列
                UV 数据交替排列, 一行 mWidth 中, 排布了 mWidth / 2 组 UV 数据

                UV 数据组有 mWidth / 2 行, mHeight / 2 列, 因此遍历时, 有如下规则 :
                按照行遍历 : 遍历 mWidth / 2 次
                按照列遍历 : 遍历 mHeight / 2 次

                外层遍历 : 遍历行从 0 到 mWidth / 2
                外层按照行遍历时, 每隔 2 行, 遍历一次, 遍历 mWidth / 2 次

                内层遍历时 : 遍历列, 从 mHeight / 2 - 1 遍历到 0
                UV 数据也需要倒着读 , 从 mHeight / 2 - 1 遍历到 0
             */
            for (int i = 0; i < mWidth / 2; i ++) {
                for (int j = UVByteHeight - 1; j >= 0; j--) {
                    // 读取数据时, 要从 YByteCount 之后的数据开始遍历
                    // 使用 mWidth 和 UVByteHeight 定位要遍历的位置
                    // 拷贝 V 色彩值数据
                    mNv21DataBuffer[positionIndex++] = data[YByteCount + mWidth / 2 * 2 * j + i];
                    // 拷贝 U 饱和度数据
                    mNv21DataBuffer[positionIndex++] = data[YByteCount + mWidth / 2 * 2 * j + i + 1];
                }
            }


        }else if(mCameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT){
            /*
                前置摄像头处理
                前置摄像头与后置摄像头相反, 后置摄像头顺时针旋转 90 度
                前置摄像头需要将图像逆时针旋转 90 度
             */

            /*
                读取 Y 灰度数据
                逆时针旋转 90 度
                外层循环 : 逐行遍历, 从最后一行遍历到第一行, 从 mWidth - 1 到 0
                内存循环 : 遍历第 i 行时, 从顶部遍历到底部, 从 0 到 mHeight - 1
             */
            for (int i = mWidth - 1; i >= 0; i--) {
                // 第 i 行, 从每一列的最后一个像素 ( 索引 mHeight - 1 ) 遍历到第一个像素 ( 索引 0 )
                for (int j = 0; j < mHeight; j++) {
                    // 将读取到的 Y 灰度值存储到 mNv21DataBuffer 缓冲区中
                    mNv21DataBuffer[positionIndex++] = data[mWidth * j + i];
                }
            }

            /*
                读取 UV 数据

                Y 数据的高度与图像高度相等
                UV 数据高度相当于 Y 数据高度的一半

                UV 数据排列 : V 色彩值在前, U 饱和度在后, UV 数据交替排列
                UV 数据交替排列, 一行 mWidth 中, 排布了 mWidth / 2 组 UV 数据

                UV 数据组有 mWidth / 2 行, mHeight / 2 列, 因此遍历时, 有如下规则 :
                按照行遍历 : 遍历 mWidth / 2 次
                按照列遍历 : 遍历 mHeight / 2 次

                外层遍历 : 遍历行从 mWidth / 2 - 1 到 0
                外层按照行遍历时, 每隔 2 行, 遍历一次, 遍历 mWidth / 2 次

                内层遍历时 : 遍历列, 从 0 遍历到 mHeight / 2 - 1
                UV 数据也需要倒着读 , 从 0 遍历到 mHeight / 2 - 1
             */
            for (int i = mWidth / 2 - 1; i >= 0 ; i --) {
                for (int j = 0; j < UVByteHeight; j++) {
                    // 读取数据时, 要从 YByteCount 之后的数据开始遍历
                    // 使用 mWidth 和 UVByteHeight 定位要遍历的位置
                    // 拷贝 V 色彩值数据
                    mNv21DataBuffer[positionIndex++] = data[YByteCount + mWidth / 2 * 2 * j + i];
                    // 拷贝 U 饱和度数据
                    mNv21DataBuffer[positionIndex++] = data[YByteCount + mWidth / 2 * 2 * j + i + 1];
                }
            }

        }
    }

摄像头nv21数据旋转90度(方法2)

    /**
     * 前置摄像头270度旋转并且镜像
     * @param data 摄像头nv21格式数据
     * @param imageWidth
     * @param imageHeight
     * @return 处理过和预览画面一样的数据
     */
    public static 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;
    }

    /**
     * 前置摄像头270度旋转
     * @param data 摄像头nv21格式数据
     * @param imageWidth
     * @param imageHeight
     * @return 与预览画面左右镜像的数据
     */
    public static 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度旋转
     * @param data 摄像头nv21格式数据
     * @param imageWidth
     * @param imageHeight
     * @return
     */
    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;
    }

摄像头nv21数据旋转90度(方法3)

 Mat yuvMat = new Mat( height*3/2,width, CvType.CV_8UC1);//初始化一个矩阵,没数据
            yuvMat.put(0,0,data);//从(0,0)开始放数据,直到data放完或者矩阵被填满(若是多通道,则把当前位置的通道全部填满,才继续下一个位置,data长度必须整除通道数).
            Mat mRgb  = new Mat(height, width, CvType.CV_8UC3);
            Imgproc.cvtColor(yuvMat , mRgb, Imgproc.COLOR_YUV420sp2BGR);//转换颜色空间
            Core.rotate(mRgb, mRgb, Core.ROTATE_90_COUNTERCLOCKWISE);
            Core.flip(mRgb, mRgb, 1);

红米8耗时大概18-20ms 荣耀v10耗时大概6-9ms

使用libyuv对YUV数据进行缩放,旋转,镜像,裁剪等操作

www.jianshu.com/p/bd0feaf4c…

android Camera(二): NV21高效处理与libyuv使用

www.jianshu.com/p/24892b86f…

Sample

github.com/SharryChoo/… github.com/TBoys/yuvLi… github.com/gordinmitya…

yuv分量提取(NDK:libyuv)

blog.csdn.net/qq_33717425…

Android CameraX Analyzer ImageProxy YUV_420_888 to NV21

www.jianshu.com/p/321ab6599…

libyuv

github.com/gongluck/An…

libyuv是Google开源的实现各种YUV与RGB之间相互转换、旋转、缩放的库。它是跨平台的,可在Windows、Linux、Mac、Android等操作系统,x86、x64、arm架构上进行编译运行,支持SSE、AVX、NEON等SIMD指令加速。

将手机摄像头采集的原始帧进行 Rotate (旋转)、Scale(拉伸)和 format convert(格式转换)

Android SDK <= 20(Android5.0)Camera.PreviewCallback的onPreviewFrame 的YUV返回格式有两种:NV21,YV12,默认返回格式是nv21