2013-02-28 46 views
1

我正在iOS上创建一个应用程序,我发送一个keyPoints(此作品)和一个MatOrbDescriptorExtractor生成的图像。图像的发送工作,接收到的base64与base64发送相同。所以我的猜测是编码和解码出错了。OpenCV使用C++在iOS上使用Base64在iOS上使用base64发送图像

左侧的图像是编码之前和在右边的图像是在服务器上接收到的解码图像:

Before base64 After 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的图像,而不会丢失数据的正确方法是什么?

回答

3

你绝不能在二进制数据上使用任何str ...函数!

的strlen((字符*)inBuffer)被停在1号零点,给人一种错误的结果

使用desc.total(),而不是为缓冲区长度

+0

非常感谢你的工作!你能解释为什么我不能在二进制数据上使用str函数吗? – tversteeg 2013-02-28 10:58:54

+1

strlen遍历数组,直到找到第一个0为止。这对字符串来说很好,但是对于二进制信息,比如图像,其中0是一个合法的黑色像素。看看你的形象,它有很多。 strcat,strcpy,无论他们都遭受同样的问题 – berak 2013-02-28 11:03:07

0

我正在使用一种稍微不同的方法。我会写它,希望它会帮助:

//Consider we have the image saved in UIImage 
UIImage * myImage; 

//Get the data 
NSData imageData = UIImageJPEGRepresentation(myImage, 1.0); 

//I am using this extension to encode to base 64 (https://gist.github.com/Abizern/1643491) 
//Article from the save author http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html 
NSString *encodedImage = [imageData base64EncodedString]; 

我送encodedImage到服务器。

+0

但是,当它转换为JPEG它的压缩。并且因为它用于计算函数,所以我担心它会导致错误。 – tversteeg 2013-02-28 10:53:37

相关问题