NV12 图像数据的旋转

来源:互联网 发布:应用数学就业前景知乎 编辑:程序博客网 时间:2024/06/05 18:35
/img 原始数据destImg 目的数据m_width 原始数据的宽,旋转后可能改变m_height 原始数据的高m_cammer 0 前置摄像头 1 后置摄像头m_angle 角度看程序决定图像右旋还是左旋还有镜像/static int imgRotationNew(unsigned char *img, unsigned char *destImg, int * m_width, int * m_height, int m_cammer, int m_angle){    int width = *m_width;    int height = *m_height;    unsigned char * srcImagUV = (unsigned char *)((int)img + height * width);    unsigned char * destImgUV = (unsigned char *)((int)destImg + width * height);    int dx = 0;    int dy = 0;    if (0 == m_cammer)    {        // 图像矩阵需要向右旋转90度        if (0 == m_angle)        {            //img.resize(width, height);            *m_width = height;            *m_height = width;            for (int i = 0; i < height; i++)            {                for (int j = 0; j < width; j++)                {                    dx = height - 1 - i;                    dy = j;                    //img(dy, dx) = dest(i, j);                    destImg[dy*height + dx] = img[i*width + j];                    destImgUV[((dy / 2) * (height / 2) + (dx / 2)) * 2 + 0] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 0];                    destImgUV[((dy / 2) * (height / 2) + (dx / 2)) * 2 + 1] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 1];                }            }        }        // 不用旋转        else if (90 == m_angle)        {            memcpy(destImg, img, width * height * sizeof(unsigned char) * 3 / 2);        }        // 旋转180度        else if (270 == m_angle)        {            for (int i = 0; i < height; i++)            {                for (int j = 0; j < width; j++)                {                    dx = width - 1 - j;                    dy = height - 1 - i;                    //img(dy, dx) = dest(i, j);                    destImg[dy*width + dx] = img[i*width + j];                    destImgUV[((dy / 2) * (width / 2) + (dx / 2)) * 2 + 0] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 0];                    destImgUV[((dy / 2) * (width / 2) + (dx / 2)) * 2 + 1] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 1];                }            }        }        // 不做处理,任其错误        else if (180 == m_angle)        {            memcpy(destImg, img, width * height * sizeof(unsigned char) * 3 / 2);        }    }    else if (1 == m_cammer) // 前置摄像头    {        // 左旋90度,左右镜像        if (0 == m_angle)        {            *m_width = height;            *m_height = width;            //img.resize(width, height);            for (int i = 0; i < height; i++)            {                for (int j = 0; j < width; j++)                {                    dx = height - 1 - i;                    dy = width - 1 - j;                    //img(dy, dx) = dest(i, j);                    destImg[dy*height + dx] = img[i*width + j];                    destImgUV[((dy / 2) * (height / 2) + (dx / 2)) * 2 + 0] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 0];                    destImgUV[((dy / 2) * (height / 2) + (dx / 2)) * 2 + 1] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 1];                }            }        }        // 左右镜像        else if (90 == m_angle)        {            for (int i = 0; i < height; i++)            {                for (int j = 0; j < width; j++)                {                    dx = width - 1 - j;                    dy = i;                    //img(dy, dx) = dest(i, j);                    destImg[dy*width + dx] = img[i*width + j];                    destImgUV[((dy / 2) * (width / 2) + (dx / 2)) * 2 + 0] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 0];                    destImgUV[((dy / 2) * (width / 2) + (dx / 2)) * 2 + 1] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 1];                }            }        }        // 上下镜像        else if (270 == m_angle)        {            for (int i = 0; i < height; i++)            {                for (int j = 0; j < width; j++)                {                    dx = j;                    dy = height - 1 - i;                    //img(dy, dx) = dest(i, j);                    destImg[dy*width + dx] = img[i*width + j];                    destImgUV[((dy / 2) * (width / 2) + (dx / 2)) * 2 + 0] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 0];                    destImgUV[((dy / 2) * (width / 2) + (dx / 2)) * 2 + 1] = srcImagUV[((i / 2) * (width / 2) + (j / 2)) * 2 + 1];                }            }        }        // 不做处理        else if (180 == m_angle)        {            memcpy(destImg, img, width * height * sizeof(unsigned char) * 3 / 2);        }    }    return 0;}
0 0