我正在iOS上创建一个应用程序,我发送一个keyPoints(此作品)和一个Mat
从OrbDescriptorExtractor
生成的图像。图像的发送工作,接收到的base64与base64发送相同。所以我的猜测是编码和解码出错了。OpenCV使用C++在iOS上使用Base64在iOS上使用base64发送图像
左侧的图像是编码之前和在右边的图像是在服务器上接收到的解码图像:
这是编码Mat
代码( desc)图像与base64,我使用的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和再C++脚本打开与PHP exec()
,这个工程。
在此之后,图像解码这样:
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函数吗? – tversteeg 2013-02-28 10:58:54
strlen遍历数组,直到找到第一个0为止。这对字符串来说很好,但是对于二进制信息,比如图像,其中0是一个合法的黑色像素。看看你的形象,它有很多。 strcat,strcpy,无论他们都遭受同样的问题 – berak 2013-02-28 11:03:07