2013-04-20 28 views
1

我擁有VideoSec IP攝像頭,並在嵌入式Linux NPE控制器上運行守護程序。守護進程需要從IP相機革蘭氏圖像,即一部分與在標準方式的libcurl實現,並與軸相機工作得很好:從MJPEG流中獲取一個圖像幀

static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { 
    size_t written = fwrite(ptr, size, nmemb, stream); 
    return written; 
} 

void refreshCameraImage(char *target, char *url) 
{ 
    CURL *image; 
    CURLcode imgresult; 
    FILE *fp; 

    image = curl_easy_init(); 

    if (image) 
    { 
      fp = fopen(target, "wb"); 
      if(fp == NULL) 
      printf("\nFile cannot be opened"); 


      curl_easy_setopt(image, CURLOPT_URL, url); 
      curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL); 
      curl_easy_setopt(image, CURLOPT_WRITEDATA, fp); 

      imgresult = curl_easy_perform(image); 
      if(imgresult) 
      { 
       printf("\nCannot grab the image!"); 
      } 
    } 
    curl_easy_cleanup(image); 
    fclose(fp); 
} 

問題的VideoSec相機是,我不能定義任何JPEG流,僅MJPEG 。 所以,我需要一種方法從libcurl中只抓取mjpeg流中的一幀。 OpenCV不是一個選項。

回答

1

在M-JPEG中,JPEG圖像被原封不動地嵌入,並通過文本分隔符與子標題分隔。所以提取JPEG是一件容易的事情:

  • 你定位在響應主體
  • 您找到Content-Length值第一個/下一個子頭/分離器,如果有
  • 你最多跳過用\ r \ n \ r \ n找到JPEG數據的開始
  • 收到JPEG數據獲取字節要麼Content-Length的數量,或者如果長度爲unavailalble,你看,直到你獲得下一個分離器

所產生的數據完全是JPEG文件/ im年齡/流。

+0

我已經在Content-Length的幫助下做了這個工作,使用套接字而不是curl,但是這種方法是正確的。 – Mel 2013-04-29 15:18:13