在鸿蒙Next(HarmonyOS Next)中,可以通过自定义图像处理库实现NV21格式图像的旋转。NV21是YUV420的一种格式,常用于安卓和鸿蒙相机预览。以下是核心实现方法:
1. 旋转原理
NV21数据分为Y平面(亮度)和VU交错平面(色度)。旋转时需分别处理:
- Y平面:宽W高H,数据量W×H
- VU平面:宽W/2高H/2,数据量W×H/2
2. 核心代码实现
// 旋转90度示例(其他角度原理类似)
public static byte[] rotateNV21(byte[] data, int width, int height) {
    byte[] rotated = new byte[data.length];
    int ySize = width * height;
    
    // 旋转Y平面
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            rotated[x * height + (height - 1 - y)] = data[y * width + x];
        }
    }
    
    // 旋转UV平面(NV21中VU交错存储)
    int uvHeight = height / 2;
    int uvWidth = width / 2;
    for (int y = 0; y < uvHeight; y++) {
        for (int x = 0; x < uvWidth; x++) {
            int srcIndex = ySize + y * width + x * 2;
            int destIndex = ySize + x * height + (uvHeight - 1 - y) * 2;
            rotated[destIndex] = data[srcIndex];     // V分量
            rotated[destIndex + 1] = data[srcIndex + 1]; // U分量
        }
    }
    return rotated;
}
3. 完整工具类
public class NV21Utils {
    // 支持90/180/270度旋转
    public static byte[] rotateNV21(byte[] data, int width, int height, int rotation) {
        switch (rotation) {
            case 90:
                return rotate90(data, width, height);
            case 180:
                return rotate180(data, width, height);
            case 270:
                return rotate270(data, width, height);
            default:
                return data;
        }
    }
    
    private static byte[] rotate90(byte[] data, int width, int height) {
        // 实现代码见上文示例
    }
    
    private static byte[] rotate180(byte[] data, int width, int height) {
        // 镜像处理Y和UV平面
    }
}
4. 使用示例
// 获取相机NV21数据后调用
byte[] rotatedData = NV21Utils.rotateNV21(cameraData, 1920, 1080, 90);
注意事项:
- 旋转后图像尺寸会变化(90/270度时宽高互换)
- 建议使用Native层实现(C++)提升性能
- 注意内存管理,大尺寸图像可能引起OOM
此实现可直接集成到鸿蒙相机应用中,适用于预览帧校正等场景。