int main(int argc,char *argv[]) { XEvent xev; XGCValues xgcv; long background=0x010203; int i, j; dc1394_t * d; dc1394camera_list_t * list; get_options(argc,argv); /* process options */ switch(fps) { case 1: fps = DC1394_FRAMERATE_1_875; break; case 3: fps = DC1394_FRAMERATE_3_75; break; case 15: fps = DC1394_FRAMERATE_15; break; case 30: fps = DC1394_FRAMERATE_30; break; case 60: fps = DC1394_FRAMERATE_60; break; default: fps = DC1394_FRAMERATE_7_5; break; } switch(res) { case 1: res = DC1394_VIDEO_MODE_640x480_YUV411; device_width=640; device_height=480; format=XV_YUY2; break; case 2: res = DC1394_VIDEO_MODE_640x480_RGB8; device_width=640; device_height=480; format=XV_YUY2; break; case 3: res = DC1394_VIDEO_MODE_800x600_YUV422; device_width=800; device_height=600; format=XV_UYVY; break; default: res = DC1394_VIDEO_MODE_320x240_YUV422; device_width=320; device_height=240; format=XV_UYVY; break; } dc1394error_t err; d = dc1394_new (); if (!d) return 1; err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { dc1394_log_error("No cameras found"); return 1; } j = 0; for (i = 0; i < list->num; i++) { if (j >= MAX_CAMERAS) break; cameras[j] = dc1394_camera_new (d, list->ids[i].guid); if (!cameras[j]) { dc1394_log_warning("Failed to initialize camera with guid %llx", list->ids[i].guid); continue; } j++; } numCameras = j; dc1394_camera_free_list (list); if (numCameras == 0) { dc1394_log_error("No cameras found"); exit (1); } /* setup cameras for capture */ for (i = 0; i < numCameras; i++) { //err=dc1394_video_set_iso_speed(cameras[i], DC1394_ISO_SPEED_400); //dc1394_video_set_operation_mode(my_camera_ptr[i], DC1394_OPERATION_MODE_1394B); err= dc1394_video_set_operation_mode(cameras[i], DC1394_OPERATION_MODE_1394B); err=dc1394_video_set_iso_speed(cameras[i], DC1394_ISO_SPEED_800); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set ISO speed"); err=dc1394_video_set_mode(cameras[i], res); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set video mode"); err=dc1394_video_set_framerate(cameras[i], fps); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set framerate"); err=dc1394_capture_setup(cameras[i],NUM_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera"); err=dc1394_video_set_transmission(cameras[i], DC1394_ON); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not start camera iso transmission"); // Camera settings err = dc1394_feature_set_value(cameras[i],DC1394_FEATURE_SHUTTER,1400); err = dc1394_feature_set_value(cameras[i],DC1394_FEATURE_BRIGHTNESS,800); err = dc1394_feature_set_value(cameras[i],DC1394_FEATURE_EXPOSURE,150); err = dc1394_feature_whitebalance_set_value(cameras[i],500,400); } fflush(stdout); if (numCameras < 1) { perror("no cameras found :(\n"); cleanup(); exit(-1); } switch(format){ case XV_YV12: set_frame_length(device_width*device_height*3/2, numCameras); break; case XV_YUY2: case XV_UYVY: set_frame_length(device_width*device_height*2, numCameras); break; default: dc1394_log_error("Unknown format set (internal error)"); exit(255); } /* make the window */ display=XOpenDisplay(getenv("DISPLAY")); if(display==NULL) { dc1394_log_error("Could not open display \"%s\"",getenv("DISPLAY")); cleanup(); exit(-1); } QueryXv(); if ( adaptor < 0 ) { cleanup(); exit(-1); } width=device_width; height=device_height * numCameras; //width=device_width * numCameras; //height=device_height; window=XCreateSimpleWindow(display,DefaultRootWindow(display),0,0,width,height,0, WhitePixel(display,DefaultScreen(display)), background); XSelectInput(display,window,StructureNotifyMask|KeyPressMask); XMapWindow(display,window); connection=ConnectionNumber(display); gc=XCreateGC(display,window,0,&xgcv); /* main event loop */ while(1){ for (i = 0; i < numCameras; i++) { if (dc1394_capture_dequeue(cameras[i], DC1394_CAPTURE_POLICY_WAIT, &frames[i])!=DC1394_SUCCESS) dc1394_log_error("Failed to capture from camera %d", i); } display_frames(); XFlush(display); while(XPending(display)>0){ XNextEvent(display,&xev); switch(xev.type){ case ConfigureNotify: width=xev.xconfigure.width; height=xev.xconfigure.height; display_frames(); break; case KeyPress: switch(XKeycodeToKeysym(display,xev.xkey.keycode,0)){ case XK_q: case XK_Q: cleanup(); exit(0); break; case XK_comma: case XK_less: //width=width/2; //height=height/2; width--; XResizeWindow(display,window,width,height); display_frames(); break; case XK_period: case XK_greater: //width=2*width; //height=2*height; width++; XResizeWindow(display,window,width,height); display_frames(); break; case XK_0: freeze = !freeze; break; case XK_1: fps = DC1394_FRAMERATE_1_875; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; case XK_2: fps = DC1394_FRAMERATE_3_75; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; case XK_4: fps = DC1394_FRAMERATE_15; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; case XK_5: fps = DC1394_FRAMERATE_30; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; case XK_3: fps = DC1394_FRAMERATE_7_5; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; } break; } } /* XPending */ for (i = 0; i < numCameras; i++) { if (frames[i]) dc1394_capture_enqueue (cameras[i], frames[i]); } } /* while not interrupted */ exit(0); }
int main(int argc, char *argv[]) { XGCValues xgcv; long background = 0x010203; unsigned int channel; unsigned int speed; int i, p, cn; raw1394handle_t raw_handle; struct raw1394_portinfo ports[MAX_PORTS]; get_options(argc, argv); /* process options */ switch (fps) { case 1: fps = FRAMERATE_1_875; break; case 3: fps = FRAMERATE_3_75; break; case 15: fps = FRAMERATE_15; break; case 30: fps = FRAMERATE_30; break; case 60: fps = FRAMERATE_60; break; default: fps = FRAMERATE_7_5; break; } switch (res) { case 1: res = MODE_640x480_YUV411; device_width = 640; device_height = 480; format = XV_YUY2; break; case 2: res = MODE_640x480_RGB; device_width = 640; device_height = 480; format = XV_YUY2; break; default: res = MODE_320x240_YUV422; device_width = 320; device_height = 240; format = XV_UYVY; break; } /* get the number of ports (cards) */ raw_handle = raw1394_new_handle(); if (raw_handle == NULL) { perror("Unable to aquire a raw1394 handle\n"); perror("did you load the drivers?\n"); exit(-1); } numPorts = raw1394_get_port_info(raw_handle, ports, numPorts); raw1394_destroy_handle(raw_handle); if (verbose) printf("number of ports = %d\n", numPorts); /* get dc1394 handle to each port */ for (p = 0; p < numPorts; p++) { int camCount; handles[p] = dc1394_create_handle(p); if (handles[p] == NULL) { perror("Unable to aquire a raw1394 handle\n"); perror("did you load the drivers?\n"); cleanup(); exit(-1); } /* get the camera nodes and describe them as we find them */ camera_nodes = dc1394_get_camera_nodes(handles[p], &camCount, verbose); /* setup cameras for capture */ for (i = 0; i < camCount; i++) { cameras[numCameras].node = camera_nodes[i]; if (dc1394_get_camera_feature_set (handles[p], cameras[numCameras].node, &features) != DC1394_SUCCESS) { printf("unable to get feature set\n"); } else if (verbose) { dc1394_print_feature_set(&features); } if (dc1394_get_iso_channel_and_speed (handles[p], cameras[numCameras].node, &channel, &speed) != DC1394_SUCCESS) { printf("unable to get the iso channel number\n"); cleanup(); exit(-1); } if (dc1394_dma_setup_capture (handles[p], cameras[numCameras].node, i + 1 /*channel */ , FORMAT_VGA_NONCOMPRESSED, res, SPEED_400, fps, NUM_BUFFERS, DROP_FRAMES, device_name, &cameras[numCameras]) != DC1394_SUCCESS) { fprintf(stderr, "unable to setup camera - check line %d of %s to make sure\n", __LINE__, __FILE__); perror("that the video mode, framerate and format are supported\n"); printf("by your camera(s)\n"); cleanup(); exit(-1); } /*have the camera start sending us data */ if (dc1394_start_iso_transmission (handles[p], cameras[numCameras].node) != DC1394_SUCCESS) { perror("unable to start camera iso transmission\n"); cleanup(); exit(-1); } numCameras++; } } fflush(stdout); if (numCameras < 1) { perror("no cameras found :(\n"); cleanup(); exit(-1); } //set_manual_exposure_gain(0, 440, 30); switch (format) { case XV_YV12: set_frame_length(device_width * device_height * 3 / 2, numCameras); break; case XV_YUY2: case XV_UYVY: set_frame_length(device_width * device_height * 2, numCameras); break; default: fprintf(stderr, "Unknown format set (internal error)\n"); exit(255); } /* create OpenCV image wrappers */ for (cn = 0; cn < MAX_CAMERAS; cn++) { if (cn < numCameras) { iplImages[cn] = cvCreateImage(cvSize(device_width, device_height), IPL_DEPTH_8U, 3); readOnlyImg = cvCreateImageHeader(cvSize(device_width, device_height), IPL_DEPTH_8U, 3); } else { iplImages[cn] = NULL; } } /* initialize handvu */ hvInitialize(device_width, device_height); hvLoadConductor(string(conductor_fname)); hvStartRecognition(); hvSetOverlayLevel(3); if (async_processing) { hvAsyncSetup(num_async_bufs, displayCallback); hvAsyncGetImageBuffer(&m_async_image, &m_async_bufID); if (sync_display) async_display_image = cvCloneImage(iplImages[0]); } /* make the window */ display = XOpenDisplay(getenv("DISPLAY")); if (display == NULL) { fprintf(stderr, "Could not open display \"%s\"\n", getenv("DISPLAY")); cleanup(); exit(-1); } QueryXv(); if (adaptor < 0) { cleanup(); exit(-1); } width = device_width; height = device_height * numCameras; window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, width, height, 0, WhitePixel(display, DefaultScreen (display)), background); XSelectInput(display, window, StructureNotifyMask | KeyPressMask); XMapWindow(display, window); connection = ConnectionNumber(display); gc = XCreateGC(display, window, 0, &xgcv); /* local main event loop */ while (1) { if (async_processing) { // asynchronous processing in HandVu capture_frame(); process_frame(); if (sync_display) display_frame(); handle_events(); } else { // synchronous processing in HandVu capture_frame(); process_frame(); display_frame(); handle_events(); } /* XPending */ } /* while not interrupted */ exit(0); }