向c#数组返回一个字符数组c++

本文关键字:数组 一个 字符 c++ 返回 | 更新日期: 2023-09-27 18:24:02

我正在使用Tesseract从图像中提取文本(OCR)。这一切都很顺利。

以前我正在将一个文件名从C#解析到我的C++DLL(DLL处理OCR部分)。

现在我想优化这个过程,在一个目录中进行解析,并为每个图像返回一个文本数组。

我想返回一个包含以下内容的数组:

[0]=文件1文本[1] =文件2文本

这是我目前拥有的代码:

 char** OCRWrapper::ReadAllPages(char* path, char* lang, char* imgPath)
    {
        std::vector<char*> charArr;
        DIR* pDirectory;
        struct dirent *entry;
        if (pDirectory = opendir(imgPath))
        {
            std::string imgPathString = imgPath;
            std::vector<int> fileNames;
            while (entry = readdir(pDirectory))
            {
                std::string fullPath = imgPathString + "''" + entry->d_name;
                if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
                {
                    std::string filename = entry->d_name;
                    filename = filename.replace(filename.find("."), 1, "");
                    filename = filename.replace(filename.find("p"), 1, "");
                    filename = filename.replace(filename.find("n"), 1, "");
                    filename = filename.replace(filename.find("g"), 1, "");
                    fileNames.push_back(atoi(filename.c_str()));
                }
            }
            closedir(pDirectory);
            std::sort(fileNames.begin(), fileNames.end());
            for (int i = 0; i < fileNames.size(); i++)
            {
                std::string fullFileName = imgPathString + "''" + std::to_string(fileNames[i]) + ".png";
                char* pFileText = GetUTF8Text(path, lang, &fullFileName[0]);
                if (pFileText == NULL)
                {
                    pFileText = "";
                }
                charArr.push_back(pFileText);
            }
        }
        //What do I do here??? I need to convert charArr to an char* array and return the text for each index.
        char** p = charArr.data();
        return p;
    }
char** p = charArr.data() //only returns the first value in the array.

希望它有意义。

我需要将数组解析为C#。

谢谢。

向c#数组返回一个字符数组c++

根据我的理解,您应该首先为p分配内存,然后将charArr的每个值复制到p。或者,如果你确定charArr的大小,我认为你char** p = charArr.data();应该会起作用。

我不明白你说是什么意思

//只返回数组中的第一个值。

您是否对p进行了迭代?如果我没有正确理解,很抱歉。

编辑::试试这个:

#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
  std::vector<char*> v;
  v.push_back("Hello");
  v.push_back("my");
  v.push_back("Friend");
  v.push_back("!");
  std::for_each (v.begin(), v.end(), [&](char* value)
  {
    std::cout << value << std::endl;
  });
  char** v_ptr = v.data();
  for (int i = 0; i < v.size(); i++)
  {
      std::cout << v_ptr[i] << std::endl;
  }
return 0;
}