android - 将YUV420Sp图像逆时针旋转90度

标签 android matrix rotation yuv libyuv

我想将 YUV420SP 图像逆时针旋转 90。图像尺寸为 640*480,因此旋转后的图像尺寸变为我不想要的 480*640,因此我想提取 480*480 数据(或任何其他正方形尺寸)并旋转该数据。

我看过:Rotate an YUV byte array on Android

但是这个答案顺时针旋转了 90。

有人可以建议一些将 YUV420Sp 数据旋转 90 度(逆时针)或 270 度(顺时针)而不改变图像尺寸的函数。

最佳答案

好的,这是我的 native 代码,经过多次敲击脑袋后演变而来。

我的困难是在看到 this 之前我不了解平面图像格式还有这个:

YUV420SP NV21 image format

这是我最终写的 2 个函数:

// rotate luma image plane 90*
//
//             (dst direction)
//                 ------>
//      dst -> +-------------+
//             |^            |
//             |^ (base dir) |
//             |^            |
//     base -> +-------------+ <- endp
//
//////////////////////////////////////////////////////////
void rotateLumaPlane90(const unsigned char *src, unsigned char *dst,
                       size_t size, size_t width, size_t height)
{
    const unsigned char *endp;
    const unsigned char *base;
    int j;

    endp = src + size;
    for (base = endp - width; base < endp; base++) {
        src = base;
        for (j = 0; j < height; j++, src -= width)
        {
            *dst++ = *src;
        }

    }
}


//
// nv12 chroma plane is interleaved chroma values that map
// from one pair of chroma to 4 pixels:
//
// Y1 Y2 Y3 Y4
// Y5 Y6 Y7 Y8   U1,V1 -> chroma values for block  Y1 Y2
// Y9 Ya Yb Yc                                     Y5 Y6
// Yd Ye Yf Yg
// -----------   U2,V2 -> chroma values for block  Y3 Y4
// U1 V1 U2 V2                                     Y7 Y8
// U3 V3 U4 V4
//
//////////////////////////////////////////////////////////
void rotateChromaPlane90(const unsigned char *src, unsigned char *dst,
                         size_t size, size_t width, size_t height)
{
    // src will start at upper right, moving down to bottom
    // then left 1 col and down...
    //
    // dest will start at end and go to 0

    int row = 0;
    int col = (int) width;
    int src_offset = col - 1;
    int dst_offset = (int) size - 2;

    while (src_offset >= 0)
    {
        dst[dst_offset] = src[src_offset];
        dst[dst_offset+1] = src[src_offset+1];
        dst_offset -= 2;

        src_offset += width;
        row++;

        if (row >= height) {
            col -= 2;
            src_offset = col;
            row = 0;
        }
    }
}

下面是我从 android native 调用这些函数的示例:

  // first rotate the Y plane
  rotateLumaPlane90((unsigned char *) encode_buffer, 
                     rotate_buffer, 
                     yPlaneSize,
                     gInputWidth, 
                     gInputHeight);


  // now rotate the U and V planes
  rotateChromaPlane90((unsigned char *) encode_buffer + yPlaneSize,
                       rotate_buffer + yPlaneSize,
                       yPlaneSize / 2,
                       gInputWidth,
                       gInputHeight/2);

注意 rotateChromaPlane90 的最后一个参数是原始图像的高度/2。我可能应该只更改色度旋转功能,以减少出错的可能性。

当翻转到后置摄像头时,我发现我需要朝相反的方向旋转 90*(或 270*),所以我也有 270* 的变化:

// rotate luma image plane 270*
//
//             +-------------+
//             |^            |
//             |^ (base dir) |
//             |^            |
//     base -> +-------------+ <- endp
//                          ^
//             <----------  |
//              (dst dir)  dst
//
//////////////////////////////////////////////////////////
void rotateLumaPlane270(unsigned char *src,
                        register unsigned char *dst,
                        int size, int width, int height)
{
    unsigned char *endp;
    register unsigned char *base;
    int j;

    endp = src + size;
    dst = dst + size - 1;
    for (base = endp - width; base < endp; base++) {
        src = base;
        for (j = 0; j < height; j++, src -= width)
        {
            *dst-- = *src;
        }

    }
}

//
// nv21 chroma plane is interleaved chroma values that map
// from one pair of chroma to 4 pixels:
//
// Y1 Y2 Y3 Y4
// Y5 Y6 Y7 Y8   U1,V1 -> chroma values for block  Y1 Y2
// Y9 Ya Yb Yc                                     Y5 Y6
// Yd Ye Yf Yg
// -----------   U2,V2 -> chroma values for block  Y3 Y4
// U1 V1 U2 V2                                     Y7 Y8
// U3 V3 U4 V4
//
//////////////////////////////////////////////////////////
void rotateChromaPlane270(unsigned char *src,
                          register unsigned char *dst,
                          int size, int width, int height)
{
    // src will start at upper right, moving down to bottom
    // then left 1 col and down...
    //
    // dest will start at 0 and go til end

    int row = 0;
    int col = width;
    int src_offset = col - 1;
    int dst_offset = 0;

    while (src_offset > 0)
    {
      dst[dst_offset++] = src[src_offset];
      dst[dst_offset++] = src[src_offset+1];

      src_offset += width;
      row++;

      if (row >= height) {
        col -= 2;
        src_offset = col;
        row = 0;
      }
    }
}

关于android - 将YUV420Sp图像逆时针旋转90度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31859708/

相关文章:

android - ionic 运行android不在设备上运行应用程序

android - 无法使用 robolectric gradle 插件确定所有任务的依赖关系

java - android中使用json进行http连接时出错

python - 在 Python 中使用新轴

r - 组合(绑定(bind)列)两个具有不同类的对象

python - 创建张量的欧式距离矩阵

rotation - ImageView 上的多次旋转

javascript - JQuery/Canvas/Javascript ...我可以使用什么来用鼠标将图像 move 或旋转到 div 中?

python - 在 Matplotlib 中将文本旋转到对数刻度上的一条线上

安卓NDK : Assertion failure: TARGET_PLATFORM is not defined