c++ - OpenCV 在使用 C++ 的 iOS 上使用 base64 通过 HTTP 发送图像

标签 c++ ios post opencv base64

我正在 iOS 上创建一个应用程序,我在其中发送一个关键点列表(这个有效)和一个从 OrbDescriptorExtractor 生成的 Mat 图像。图像的发送有效,接收到的base64 与发送的base64 相同。所以我的猜测是编码和解码出了问题。

左边是编码前的图片,右边是服务器端接收到的解码后的图片:

Before base64 After base64

这是用base64编码Mat(desc)图像的代码,我使用的base64函数来自this site .

char sendFile[1000];
char temp[100];

std::sprintf(temp, "^^%d^^%d^^", desc.cols, desc.rows);
strcat(sendFile, temp);

const unsigned char* inBuffer = reinterpret_cast<const unsigned char*>(desc.data);

strcat(sendFile, base64_encode(inBuffer, strlen((char*)inBuffer)).c_str());
strcat(sendFile, "\0");

在此之后,文件通过 HTTP Post 保存在服务器上,然后使用 PHP 中的 exec() 打开 C++ 脚本,这有效。

在此之后图像以这种方式解码:

int processData(string input, int* width, int* height){
    int cur = 0, k = 0;
    for(unsigned int i = 0; i < input.length(); i++){
        if(input.substr(i, 2) == "^^"){
            if(cur == 0){
                k = i + 2;          
            }else if(cur == 1){
                *width = getIntFromString(input, k, i);         
                k = i + 2;
            }else{
                *height = getIntFromString(input, k, i);        
                break;
            }
            cur++;
        }
    }
    return 0;
}

int error, w, h;
string line, data;
ifstream file;

file.open(argv[1]);

if(file.is_open()){
    error = processData(line, &w, &h);
    if(error != 0){
        printf("Processing keypoints failed \n");
        return 1;
    } 
    getline(file, line);
    data = base64_decode(line);

    file.close();
}else{
    printf("Couldn't open file.\n");
    return 1;
}

Mat tex_des(Size(w, h), CV_8UC1, (void*)data.c_str());

如何在不丢失数据的情况下以正确的方式发送 OpenCV 图像?

最佳答案

您不得对二进制数据使用任何 str... 函数!!

strlen((char*)inBuffer) 在第一个零处停止,给出了错误的结果

使用 desc.total() 代替缓冲区长度

关于c++ - OpenCV 在使用 C++ 的 iOS 上使用 base64 通过 HTTP 发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15132766/

相关文章:

c++ - 从配置文件 qsetting 中删除键/值对

c++ - 刷新 boost::iostreams::zlib_compressor。如何获得 "sync flush"?

ios - 在 UISplitView 中选择导航后 DetailView 消失

objective-c - 如何抑制虚拟键盘滑入动画?

ios - 更改 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) 的持续时间

c++ - C++ 中的引用初始化

C++如何将其拆分为 vector 并使用它

php - 将JSON从iOS发布到PHP

mysql - 如何处理通过 url 发送的问号?

javascript - jQuery 拦截表单提交到参数字符串