HarmonyOS Next中如何在C++中操作Rawfile文件?

HarmonyOS Next中如何在C++中操作Rawfile文件?

在应用中要接入自研的图像检测等AI模型,这些模型保存在Rawfile中,是否可以在C++中直接读取Rwafile文件?可以的话如何读取?

阅读 799
avatarAI BotBETA

在HarmonyOS Next中,要在C++中操作Rawfile文件,你可以使用标准的文件I/O函数来读取Rawfile文件的内容。虽然Rawfile是一种二进制文件,但它仍然可以通过文件流进行读取和处理。

以下是一个简单的示例,演示如何在C++中读取Rawfile文件:

#include <iostream>
#include <fstream>
#include <vector>

int main() {
    std::string filepath = "path/to/your/rawfile.bin"; // 替换为你的Rawfile文件路径

    // 打开文件
    std::ifstream file(filepath, std::ios::binary);
    if (!file) {
        std::cerr << "无法打开文件: " << filepath << std::endl;
        return 1;
    }

    // 获取文件大小
    file.seekg(0, std::ios::end);
    size_t fileSize = file.tellg();
    file.seekg(0, std::ios::beg);

    // 读取文件内容到vector中
    std::vector<char> buffer(fileSize);
    if (!file.read(buffer.data(), fileSize)) {
        std::cerr << "读取文件失败: " << filepath << std::endl;
        return 1;
    }

    // 在这里可以对buffer中的数据进行处理
    // 例如,你可以使用buffer.data()指针访问原始数据,并根据你的模型格式进行解析

    // 关闭文件
    file.close();

    return 0;
}

上述示例代码使用std::ifstream类打开Rawfile文件,并将其内容读取到一个std::vector<char>类型的缓冲区中。你可以根据你的模型和Rawfile的具体格式,对缓冲区中的数据进行解析和处理。

请注意,Rawfile文件的具体格式和内容取决于你的模型和数据,因此你可能需要根据实际情况进行适当的解析和处理。

希望这可以帮助到你!如果你有任何进一步的问题或需要更详细的解释,请随时提问。

1 个回答

可以读取,HarmonyOS Next提供了读取Rawfile接口。

  1. CMakeLists.txt中的target_link_libraries依赖中添加资源的librawfile.z.so
  2. 在cpp文件中获取Js的资源对象,并转为Native的资源对象,即可调用资源的Native接口,获取rawfile列表、rawfile文件内容以及rawfile描述符{fd, offset, length}三种调用方式示例代码如下:

    #include <rawfile/raw_file.h>
    #include <rawfile/raw_dir.h>
    #include <rawfile/raw_file_manager.h>
    
    // 示例一:获取rawfile文件列表 GetFileList
    static napi_value GetFileList(napi_env env, napi_callback_info info)
    {
     OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest Begin");
     size_t requireArgc = 3;
     size_t argc = 2;
     napi_value argv[2] = { nullptr };
     // 获取参数信息
     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
    
     // argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象。
     NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
    
     // 获取函数argv[1],此为为rawfile相对路径
     size_t strSize;
     char strBuf[256];
     napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
     std::string dirName(strBuf, strSize);
    
     // 获取对应的rawDir指针对象
     RawDir* rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, dirName.c_str());
    
     // 获取rawDir下文件及文件夹数量
     int count = OH_ResourceManager_GetRawFileCount(rawDir);
    
     // 遍历获取文件名称,并保存
     std::vector<std::string> tempArray;
     for(int i = 0; i < count; i++) {
         std::string filename = OH_ResourceManager_GetRawFileName(rawDir, i);
         tempArray.emplace_back(filename);
     }
    
     napi_value fileList;
     napi_create_array(env, &fileList);
     for (size_t i = 0; i < tempArray.size(); i++) {
         napi_value jsString;
         napi_create_string_utf8(env, tempArray[i].c_str(), NAPI_AUTO_LENGTH, &jsString);
         napi_set_element(env, fileList, i, jsString);
     }
    
     // 关闭打开的指针对象
     OH_ResourceManager_CloseRawDir(rawDir);
     OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
     return fileList;
    }
    
    // 示例二:获取rawfile文件内容 GetRawFileContent
    napi_value CreateJsArrayValue(napi_env env, std::unique_ptr<uint8_t[]> &data, long length)
    {
     napi_value buffer;
     napi_status status = napi_create_external_arraybuffer(env, data.get(), length,
             [](napi_env env, void *data, void *hint) {
                 delete[] static_cast<char*>(data);
             }, nullptr, &buffer);
     if (status != napi_ok) {
         OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create external array buffer");
         return nullptr;
     }
     napi_value result = nullptr;
     status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result);
     if (status != napi_ok) {
         OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create media typed array");
         return nullptr;
     }
     data.release();
     return result;
    }
    static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
    {
     OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "GetFileContent Begin");
     size_t requireArgc = 3;
     size_t argc = 2;
     napi_value argv[2] = { nullptr };
     // 获取参数信息
     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
    
     // argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象。
     NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
     size_t strSize;
     char strBuf[256];
     napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
     std::string filename(strBuf, strSize);
    
     // 获取rawfile指针对象
     RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
     if (rawFile != nullptr) {
         OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "OH_ResourceManager_OpenRawFile success");
     }
     // 获取rawfile大小并申请内存
     long len = OH_ResourceManager_GetRawFileSize(rawFile);
     std::unique_ptr<uint8_t[]> data= std::make_unique<uint8_t[]>(len);
    
     // 一次性读取rawfile全部内容
     int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len);
    
     // 多次部分读取rawfile, 每次读取100 Byte。获取全部内容
     // long offset = 0;
     // while (OH_ResourceManager_GetRawFileRemainingLength(rawFile) > 0) {
     //     OH_ResourceManager_ReadRawFile(rawFile, data.get() + offset, 100);
     //     offset += 100;
     // }
    
     // 关闭打开的指针对象
     OH_ResourceManager_CloseRawFile(rawFile);
     OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
     // 转为js对象
     return CreateJsArrayValue(env, data, len);
    }
    
    // 示例三:获取rawfile文件描述符 GetRawFileDescriptor
    napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor &descriptor)
    {
     napi_value result;
     napi_status status = napi_create_object(env, &result);
     if (status != napi_ok) {
         return result;
     }
    
     napi_value fd;
     status = napi_create_int32(env, descriptor.fd, &fd);
     if (status != napi_ok) {
         return result;
     }
     status = napi_set_named_property(env, result, "fd", fd);
     if (status != napi_ok) {
         return result;
     }
    
     napi_value offset;
     status = napi_create_int64(env, descriptor.start, &offset);
     if (status != napi_ok) {
         return result;
     }
     status = napi_set_named_property(env, result, "offset", offset);
     if (status != napi_ok) {
         return result;
     }
    
     napi_value length;
     status = napi_create_int64(env, descriptor.length, &length);
     if (status != napi_ok) {
         return result;
     }
     status = napi_set_named_property(env, result, "length", length);
     if (status != napi_ok) {
         return result;
     }
     return result;
    }
    static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
    {
     OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest GetRawFileDescriptor Begin");
     size_t requireArgc = 3;
     size_t argc = 2;
     napi_value argv[2] = { nullptr };
     // 获取参数信息
     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
     
     napi_valuetype valueType;
     napi_typeof(env, argv[0], &valueType);
     // 获取native的resourceManager对象
     NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
     size_t strSize;
     char strBuf[256];
     napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
     std::string filename(strBuf, strSize);
     // 获取rawfile指针对象
     RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
     if (rawFile != nullptr) {
         OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "OH_ResourceManager_OpenRawFile success");
     }
     // 获取rawfile的描述符RawFileDescriptor {fd, offset, length}
     RawFileDescriptor descriptor;
     OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
     // 关闭打开的指针对象
     OH_ResourceManager_CloseRawFile(rawFile);
     OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
     // 转为js对象
     return createJsFileDescriptor(env,descriptor);
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进