1 | initial version |
Here is a short example on how to convert IplImage to unsigned char and send it via a socket. (to simulate jpeg image sent from a web server). It is in C because I was not able to find any example for it... CvCapture* camera; IplImage* img_cam; IplImage* img_web; int params[2]; unsigned char* ptr; unsigned char buf; int i; int buf_len; char head[1024];
// Capture image and convert to jpg. // I used cvCloneimage so I don't work on the original image. img_cam = cvQueryFrame(camera); img_web = cvCloneImage(img_cam); params[0]= CV_IMWRITE_JPEG_QUALITY; params[1]= 50; // Quality 0-100 CvMat* ei = cvEncodeImage(".jpg", img_web, params); buf_len = ((ei->rows) * (ei->cols)); ptr = ei->data.ptr; // Copy jpeg data from CvMat to unsigned char buffer. for (i = 0 ; i < buf_len ; i++) { buf[i] = (*(ptr++)); }
...
// Send data via tcp socket. SOCKET sock = Accept(master); // See post above... sprintf(head, "HTTP/1.1 200 OK\r\nServer: Built-in\r\nCache-Control: no-cache\r\nCache-Control: private\r\nContent-Type: image/jpeg\r\nContent-Length: %lu\r\nConnection: close\r\n\r\n", buf_len); send(sock, head, strlen(head), MSG_NOSIGNAL); send(sock, (uc*)(&buf[0]), buf_len, MSG_NOSIGNAL); close(sock);
2 | No.2 Revision |
Here is a short example on how to convert IplImage to unsigned char and send it via a socket. (to simulate jpeg image sent from a web server). It is in C because I was not able to find any example for it...
it...
CvCapture* camera;
IplImage* img_cam;
IplImage* img_web;
int params[2];
unsigned char* ptr;
unsigned char buf;
int i;
int buf_len;
char head[1024];head[1024];
// Capture image and convert to jpg.
// I used cvCloneimage so I don't work on the original image.
img_cam = cvQueryFrame(camera);
img_web = cvCloneImage(img_cam);
params[0]= CV_IMWRITE_JPEG_QUALITY;
params[1]= 50; // Quality 0-100
CvMat* ei = cvEncodeImage(".jpg", img_web, params);
buf_len = ((ei->rows) * (ei->cols));
ptr = ei->data.ptr;
// Copy jpeg data from CvMat to unsigned char buffer.
for (i = 0 ; i < buf_len ; i++) {
buf[i] = (*(ptr++));
}}
...
...
// Send data via tcp socket.
SOCKET sock = Accept(master); // See post above...
sprintf(head, "HTTP/1.1 200 OK\r\nServer: Built-in\r\nCache-Control: no-cache\r\nCache-Control: private\r\nContent-Type: image/jpeg\r\nContent-Length: %lu\r\nConnection: close\r\n\r\n", buf_len);
send(sock, head, strlen(head), MSG_NOSIGNAL);
send(sock, (uc*)(&buf[0]), buf_len, MSG_NOSIGNAL);
close(sock);close(sock);