Example for capturing images using DC1394 library directly without an instance of BIAS::VideoSource_DCAM.
#include <Base/Image/Image.hh>
#include <Base/Image/ImageIO.hh>
#include <dc1394/dc1394.h>
#include <inttypes.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define NUM_DMA_BUFFERS 4
using namespace BIAS;
using namespace std;
void cleanup_and_exit(dc1394camera_t *camera)
{
dc1394_video_set_transmission(camera, DC1394_OFF);
dc1394_capture_stop(camera);
dc1394_camera_free(camera);
exit(1);
}
int main(int argc, char *argv[])
{
dc1394camera_t *camera;
dc1394framerate_t framerate = DC1394_FRAMERATE_15;
dc1394video_mode_t video_mode = DC1394_VIDEO_MODE_800x600_MONO8;
dc1394color_coding_t color_coding = DC1394_COLOR_CODING_MONO8;
dc1394video_frame_t *frame = NULL;
dc1394_t *dc = NULL;
dc1394error_t err;
int image_number = 1;
bool show_features = false;
bool detect_video_mode = true;
if (argc > 1) {
image_number = atoi(argv[1]);
if (image_number < 1) image_number = 1;
}
dc = dc1394_new();
if (!dc) return 1;
dc1394camera_list_t *list;
err = dc1394_camera_enumerate (dc, &list);
DC1394_ERR_RTN(err, "Failed to enumerate cameras");
if (list->num == 0) {
dc1394_log_error("No cameras found");
return 1;
}
camera = dc1394_camera_new(dc, list->ids[0].guid);
if (!camera) {
dc1394_log_error("Failed to initialize camera with guid %d", (int)list->ids[0].guid);
return 1;
}
dc1394_camera_free_list(list);
cout << "Using camera with GUID " << camera->guid << endl;
if (detect_video_mode)
{
dc1394video_modes_t video_modes;
err = dc1394_video_get_supported_modes(camera, &video_modes);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Can't get video modes");
bool found_mode = false;
for (int i = video_modes.num-1; i>= 0; i--) {
if (!dc1394_is_video_mode_scalable(video_modes.modes[i])) {
dc1394_get_color_coding_from_video_mode(camera, video_modes.modes[i], &color_coding);
if (color_coding == DC1394_COLOR_CODING_MONO8) {
video_mode = video_modes.modes[i];
found_mode = true;
break;
}
}
}
if (!found_mode) {
dc1394_log_error("Could not get a valid MONO8 mode");
cleanup_and_exit(camera);
}
err = dc1394_get_color_coding_from_video_mode(camera, video_mode, &color_coding);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not get color coding");
dc1394framerates_t framerates;
err = dc1394_video_get_supported_framerates(camera, video_mode, &framerates);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not get framerates");
framerate = framerates.framerates[framerates.num-1];
}
if (color_coding != DC1394_COLOR_CODING_MONO8 &&
color_coding != DC1394_COLOR_CODING_RGB8) {
dc1394_log_error("Color coding has to be MONO8 or RGB8");
cleanup_and_exit(camera);
}
err = dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set iso speed");
err = dc1394_video_set_mode(camera, video_mode);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set video mode");
err = dc1394_video_set_framerate(camera, framerate);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set framerate");
err = dc1394_capture_setup(camera, NUM_DMA_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not setup camera\n"
"Make sure that the video mode and framerate are\n"
"supported by your camera");
if (show_features) {
dc1394featureset_t features;
err = dc1394_feature_get_all(camera, &features);
if (err != DC1394_SUCCESS) {
dc1394_log_warning("Could not get feature set");
}
else {
dc1394_feature_print_all(&features, stdout);
}
}
unsigned int width, height;
float fps;
dc1394_get_image_size_from_video_mode(camera, video_mode, &width, &height);
dc1394_framerate_as_float(framerate, &fps);
const bool grey_image = (color_coding == DC1394_COLOR_CODING_MONO8);
const unsigned int channels = (grey_image ? 1 : 3);
const unsigned int bytes = channels * width * height;
cout << "Starting capture with image size " << width << " x "
<< height << " (" << (grey_image ? "MONO8" : "RGB8") << ") with "
<< fps << " frames/sec" << endl;
err = dc1394_video_set_transmission(camera, DC1394_ON);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera),
"Could not start camera iso transmission");
for (int image_no = 0; image_no < image_number; image_no++)
{
err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not capture a frame");
memcpy(image.GetImageData(), frame->image, bytes);
char filename[32];
sprintf(filename, "image-%04d.%s", image_no, grey_image ? "pgm" : "ppm");
cout << "Wrote " << filename << endl << flush;
err = dc1394_capture_enqueue(camera, frame);
}
dc1394_video_set_transmission(camera, DC1394_OFF);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not stop the camera");
dc1394_capture_stop(camera);
dc1394_camera_free(camera);
dc1394_free(dc);
cout << "Closed camera connection" << endl;
return 0;
}