// opencv #include #include #include #include #include #include #include #include #include //close #include /* This will be created under /dev/shm/ */ #define STATE_FILE "/camproxy.shared" #define WIDTH 480 #define HEIGHT 360 #define SIZE 640*480*3 int shm_fd; void ex_program(int sig) { close(shm_fd); printf("Bye\n"); exit(0); } int main(int argc, char **argv) { int num = 0; if (argc > 1) num = atol(argv[1]); printf("CamProxy: Using camera index #%d\n", num); unsigned char *shared_frame; int shm_fd; if ((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_RDWR), (S_IREAD | S_IWRITE))) < 0) { printf("Could not create shm object. %s\n", strerror(errno)); return errno; } ftruncate(shm_fd, SIZE); if((shared_frame = mmap(0, SIZE, (PROT_READ | PROT_WRITE), MAP_SHARED, shm_fd, 0)) == MAP_FAILED) { printf("Could not create shm object. %s\n", strerror(errno)); return errno; } static CvCapture* camera_capture = 0; // initialize camera camera_capture = cvCaptureFromCAM(num); //cvSetCaptureProperty(camera_capture, CV_CAP_PROP_FRAME_WIDTH, 640); //cvSetCaptureProperty(camera_capture, CV_CAP_PROP_FRAME_HEIGHT, 480); // cvSetCaptureProperty(camera_capture, CV_CAP_PROP_CONVERT_RGB, 0); // IplImage *img = cvCreateImage(cvSize(640, 480), 8, 3); printf("Camera initialized.\n"); (void) signal(SIGINT, ex_program); while (1) { if( !cvGrabFrame( camera_capture )) { printf("Cannot grab frames!\n"); return 1; } IplImage *frame = cvRetrieveFrame( camera_capture ); if( !frame) { printf("Cannot grab frames."); return 1; } cvResize(frame, img, 0); int x, y; for (x=0; x<640; x++) { for (y=0; y<480; y++) { shared_frame[3*640*(479-y) + 3*x + 2] = img->imageData[img->widthStep * y + 3 * x + 0]; shared_frame[3*640*(479-y) + 3*x + 1] = img->imageData[img->widthStep * y + 3 * x + 1]; shared_frame[3*640*(479-y) + 3*x + 0] = img->imageData[img->widthStep * y + 3 * x + 2]; } } } return 0; }