HarmonyOS鸿蒙Next中YUV数据渲染示例代码

HarmonyOS鸿蒙Next中YUV数据渲染示例代码

介绍

OpenGLES渲染yuv数据。

demo详情链接

https://gitee.com/scenario-samples/yuv-demo

2 回复

在HarmonyOS鸿蒙Next中,YUV数据的渲染可以通过OHOS::SurfaceOHOS::Graphic模块来实现。以下是一个简单的示例代码,展示了如何使用Surface来渲染YUV数据。

#include <graphic/graphic_common.h>
#include <surface/surface.h>
#include <surface/surface_buffer.h>
#include <surface/surface_utils.h>
#include <memory>

using namespace OHOS;

void RenderYUVData(std::shared_ptr<Surface> surface, const uint8_t* yuvData, int width, int height) {
    SurfaceBuffer* buffer = SurfaceUtils::GetInstance().RequestBuffer(surface, width, height, PIXEL_FMT_YCRCB_420_SP);
    if (buffer == nullptr) {
        return;
    }

    uint8_t* dstY = static_cast<uint8_t*>(buffer->GetVirAddr());
    uint8_t* dstUV = dstY + width * height;

    // 假设YUV数据已经是NV21格式
    memcpy(dstY, yuvData, width * height);
    memcpy(dstUV, yuvData + width * height, width * height / 2);

    SurfaceUtils::GetInstance().FlushBuffer(surface, buffer, -1, Rect{0, 0, width, height});
}

int main() {
    std::shared_ptr<Surface> surface = Surface::CreateSurface();
    if (surface == nullptr) {
        return -1;
    }

    // 假设YUV数据已经准备好
    const uint8_t* yuvData = /* YUV数据 */;
    int width = 1920;
    int height = 1080;

    RenderYUVData(surface, yuvData, width, height);

    return 0;
}

这段代码首先通过Surface::CreateSurface()创建一个Surface对象,然后通过RequestBuffer请求一个SurfaceBuffer来存储YUV数据。YUV数据被复制到SurfaceBuffer中,最后通过FlushBuffer将数据渲染到屏幕上。

更多关于HarmonyOS鸿蒙Next中YUV数据渲染示例代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)中渲染YUV数据通常涉及使用SurfaceEGL进行图形渲染。以下是一个简单的示例代码,展示如何将YUV数据渲染到屏幕:

import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.utils.Rect;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

public class YUVRenderer {
    private SurfaceProvider surfaceProvider;
    private SurfaceOps surfaceOps;

    public YUVRenderer(Context context) {
        surfaceProvider = new SurfaceProvider(context);
        surfaceOps = surfaceProvider.getSurfaceOps();
        surfaceOps.addCallback(new SurfaceOps.Callback() {
            @Override
            public void surfaceCreated(SurfaceOps ops) {
                // Surface创建时初始化
            }

            @Override
            public void surfaceChanged(SurfaceOps ops, int format, int width, int height) {
                // Surface尺寸变化时处理
            }

            @Override
            public void surfaceDestroyed(SurfaceOps ops) {
                // Surface销毁时清理资源
            }
        });
    }

    public void renderYUV(byte[] yuvData, int width, int height) {
        // 将YUV数据转换为PixelMap
        ImageSource.SourceOptions options = new ImageSource.SourceOptions();
        ImageSource imageSource = ImageSource.create(yuvData, options);
        PixelMap pixelMap = imageSource.createPixelmap(null);

        // 渲染PixelMap到Surface
        surfaceOps.getSurface().lockCanvas(new Rect(0, 0, width, height));
        surfaceOps.getSurface().unlockCanvasAndPost(canvas);
    }

    public SurfaceProvider getSurfaceProvider() {
        return surfaceProvider;
    }
}

代码说明:

  1. SurfaceProvider:用于管理Surface的创建和销毁。
  2. SurfaceOps.Callback:监听Surface的生命周期事件。
  3. renderYUV:将YUV数据转换为PixelMap,并通过Surface渲染到屏幕上。

注意事项:

  • YUV到PixelMap的转换需要根据具体的YUV格式(如NV12、YV12等)进行处理。
  • 实际应用中可能需要使用EGL进行更高效的渲染。

此示例展示了基本的YUV数据渲染流程,具体实现可能需要根据实际需求进行调整。

回到顶部