简介

离线超分插件为开发者提供游戏截图(拍照)画质提升能力,提高画面分辨率,并改善画面细节,帮助开发者向玩家提供高质量截图(拍照)功能。
开发者需要:

您将建立什么

在这个Codelab中,你将使用已经创建好的Demo Project实现对离线超分插件API的调用,通过Demo Project你可以体验到:

您将会学到什么

硬件要求

软件要求

需要的知识点

集成离线超分SDK

  1. 创建Android Studio Project。
  2. 修改"/app/build.gradle"文件,指定CMake编译C++文件,在"/app/build.gradle"文件中,创建CMake的编译依赖。
  3. 同时配置编译选项。

  4. 复制库和头文件。
    a) 下载SDK包
    b) 复制SDK头文件到资源库。

    c) 复制SDK so到资源库。
    将SDK中"libs\arm64-v8a\libthirdpartyengine_plugin_offlineSupRes.so"复制到Android Studio下的"/libs/arm64-v8a"。

    d) 修改"app/src/main/cpp/CMakeLists.txt"。

    e) 复制资源文件到手机的自定义目录。

调用离线超分API

  1. 设置资源文件目录。
    CSetAssetsDir("/sdcard/osr"); const char *assetsDir = CGetAssetsDir(); printf("Set directory to %s", assetsDir);
  2. 初始化插件,将资源文件导入插件。
    CInitialize();
  3. 打开图片,并根据图片信息配置BufferDescriptor结构体。
    BufferDescriptor inBuffer; int w, h; unsigned char *pixels = ReadPPM(path, w, h); inBuffer.addr = static_cast<void *>(pixels); inBuffer.width = w; inBuffer.height = h; inBuffer.len = w * h * CHANNELS_RGB; inBuffer.format = PIXEL_FORMAT_R8G8B8_UNORM;
  4. 根据图片的尺寸、格式查询超分功能的支持情况。
    a) 查询AI超分的支持情况。
    PluginConfig pluginConfig; CQuerySuperSamplingPluginConfig(inBuffer.width, inBuffer.height, inBuffer.format, pluginConfig); int outW = pluginConfig.output.width; int outH = pluginConfig.output.height; PixelFormat outFormat = pluginConfig.output.format; int estimatedTime = pluginConfig.estimatedCostTime;
    b) 查询图像处理超分的支持情况。
    PluginConfig pluginConfig; CQuerySuperSamplingPluginConfig(inBuffer.width, inBuffer.height, inBuffer.format, pluginConfig); int outW = pluginConfig.output.width; int outH = pluginConfig.output.height; PixelFormat outFormat = pluginConfig.output.format; int estimatedTime = pluginConfig.estimatedCostTime;
  5. 返回的outW、outH或estimatedTime任一为负值,则表明不支持当前超分场景(图片尺寸不合适,像素格式不正确,手机机型不支持)。否则根据返回值创建BufferDescriptor结构体对象。
    BufferDescriptor outBuffer; unsigned char *pixels = new(std::nothrow) unsigned char[outW * outH * Format2Channel(outFormat)]; outBuffer.addr = static_cast<void *>(pixels); outBuffer.width = outW; outBuffer.height = outH; outBuffer.len = outW * outH * Format2Channel(outFormat); outBuffer.format = outFormat;
  6. 如果支持当前超分场景,则调用相应的功能接口进行处理。
    a) 同步调用AI模型超分接口。
    int timeout = 10000; bool success = CSuperSamplingSyncExecute(&inBuffer, &outBuffer, timeout);
    b) 同步调用图像处理算法超分接口。
    int timeout = 10000; float sharpness = 0.6; bool tonemapping = true; bool success = CImageEnhancingSyncExecute(&inBuffer, &outBuffer, sharpness, tonemapping, timeout);
  7. 对于异步调用的情形,需要先定义回调函数。
    void CallBack(bool success) { // 根据超分功能是否执行成功分别进行处理 return; }
    a) 异步调用AI模型超分接口。<
    bool success = CSuperSamplingAsyncExecute(&inBuffer, &outBuffer, CallBack); std::this_thread::sleep_for(std::chrono::seconds(5));
    b) 异步调用图像处理算法超分接口。
    int timeout = 10000; float sharpness = 0.6; bool tonemapping = true; bool success = CImageEnhancingAsyncExecute(&inBuffer, &outBuffer, sharpness, tonemapping, CallBack); std::this_thread::sleep_for(std::chrono::seconds(2));
  8. 将outBuffer.addr存储的图片数据保存成任意图片格式。
  9. 卸载插件。
    delete[](static_cast<unsigned char *>(inBuffer.addr)); delete[](static_cast<unsigned char *>(outBuffer.addr)); CUninitialize();

干得好,您已经成功完成了Codelab并学到了:

您可以阅读下面链接,了解更多相关的信息。

相关文档
本Codelab中所用Demo源码下载地址如下:

下载Source Code

Code copied