firewirecameras.cpp
来自「一个语言识别引擎」· C++ 代码 · 共 1,029 行 · 第 1/3 页
CPP
1,029 行
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
/*
* Copyright (C) 2006 Charles C. Kemp
* CopyPolicy: Released under the terms of the GNU GPL v2.0.
*
*/
//based on libdc1394 example code grab_gray_image.c
//and coriander
#include "FirewireCameras.h"
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
const bool PRINTF_ON=false;
bool FWCameras::SetSize(int x, int y)
{
if ( (x==320) && (y==240))
{
capture_size=_320x240;
fprintf(stderr, "FWCameras:: setting 320x240\n");
return true;
}
else if ( (x==160) && (y==120))
{
capture_size=_160x120;
fprintf(stderr, "FWCameras:: setting 160x120\n");
return true;
}
else
{
capture_size=_320x240;
fprintf(stderr, "FWCameras:: %dx%d, format not supported (yet)\n",
x, y);
fprintf(stderr, "Setting 320x240, returning false\n");
return false;
}
}
void FWCameras::SetAuto(bool auto_adjustment_on, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
int val;
if(auto_adjustment_on) val = 1;
else val = 0;
dc1394_auto_on_off( handle, camera_nodes[camera_num],
FEATURE_EXPOSURE, val);
dc1394_auto_on_off( handle, camera_nodes[camera_num],
FEATURE_SHUTTER, val);
dc1394_auto_on_off( handle, camera_nodes[camera_num],
FEATURE_GAIN, val);
dc1394_auto_on_off( handle, camera_nodes[camera_num],
FEATURE_BRIGHTNESS, val);
}
///////////////////////////
void FWCameras::SetExposure(float exposure_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
// exposure = 260; //0-1023 //260
cameras[camera_num].dstate.SetExposure(exposure_in);
dc1394_set_exposure(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetExposure());
}
void FWCameras::SetShutter(float shutter_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
// shutter = 532; //2-532 (dark to bright) //532
cameras[camera_num].dstate.SetShutter(shutter_in);
dc1394_set_shutter(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetShutter());
}
void FWCameras::SetGain(float gain_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
//////////////////range in coriander //default value shown in coriander
//gain was 450 then 300 then 200
// gain = 220;//400 //730 //220-1023 //562
cameras[camera_num].dstate.SetGain(gain_in);
dc1394_set_gain(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetGain());
}
void FWCameras::SetBrightness(float brightness_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
// brightness = 0; //0-255 //0
cameras[camera_num].dstate.SetBrightness(brightness_in);
dc1394_set_brightness(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetBrightness());
}
void FWCameras::SetColor(float u_b_in, float v_r_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
//u_b_value = 63; //0-63 //50
// v_r_value = 30; //0-63 //50
cameras[camera_num].dstate.SetColor(u_b_in, v_r_in);
dc1394_set_white_balance(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetUBColor(),
(unsigned int) cameras[camera_num].dstate.GetVRColor());
}
//////////////////////////////////
void FWCameras::SetExposure(int exposure_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
// exposure = 260; //0-1023 //260
cameras[camera_num].dstate.SetExposure(exposure_in);
dc1394_set_exposure(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetExposure());
}
void FWCameras::SetShutter(int shutter_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
// shutter = 532; //2-532 (dark to bright) //532
cameras[camera_num].dstate.SetShutter(shutter_in);
dc1394_set_shutter(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetShutter());
}
void FWCameras::SetGain(int gain_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
//////////////////range in coriander //default value shown in coriander
//gain was 450 then 300 then 200
// gain = 220;//400 //730 //220-1023 //562
cameras[camera_num].dstate.SetGain(gain_in);
dc1394_set_gain(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetGain());
}
void FWCameras::SetBrightness(int brightness_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
// brightness = 0; //0-255 //0
cameras[camera_num].dstate.SetBrightness(brightness_in);
dc1394_set_brightness(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetBrightness());
}
void FWCameras::SetColor(int u_b_in, int v_r_in, int camera_num)
{
if(!cam_num_ok(camera_num)) return;
//u_b_value = 63; //0-63 //50
// v_r_value = 30; //0-63 //50
cameras[camera_num].dstate.SetColor(u_b_in, v_r_in);
dc1394_set_white_balance(handle, camera_nodes[camera_num],
(unsigned int) cameras[camera_num].dstate.GetUBColor(),
(unsigned int) cameras[camera_num].dstate.GetVRColor());
}
///////////////////////////
bool FWCameras::init_firewire()
{
int port=0; // port 0 is the first IEEE1394 card. We ONLY probe this one.
//open ohci and assign a handle to it
handle=dc1394_create_handle(port);
if (handle==NULL)
{
printf("Unable to aquire a raw1394 handle\n\n"
"Please check \n"
" - if the kernel modules `ieee1394',`raw1394' and `ohci1394' are loaded \n"
" - if you have read/write access to /dev/raw1394\n\n");
return(false);
}
//get the camera nodes and describe them as we find them
// probe the IEEE1394 bus for DC camera:
num_nodes = raw1394_get_nodecount(handle);
camera_nodes = dc1394_get_camera_nodes(handle, &num_cameras, 1); //used to be 0, 0 not to show the cams.
fflush(stdout); //added with ",1);"
if (num_cameras<1)
{
printf("no cameras found!\n");
dc1394_destroy_handle(handle);
return(false);
}
if(camera_nodes[0]== (num_nodes-1))
{
printf( "\n"
"Sorry, your camera is the highest numbered node\n"
"of the bus, and has therefore become the root node.\n"
"The root node is responsible for maintaining \n"
"the timing of isochronous transactions on the IEEE \n"
"1394 bus. However, if the root node is not cycle master \n"
"capable (it doesn't have to be), then isochronous \n"
"transactions will not work. The host controller card is \n"
"cycle master capable, however, most cameras are not.\n"
"\n"
"The quick solution is to add the parameter \n"
"attempt_root=1 when loading the OHCI driver as a \n"
"module. So please do (as root):\n"
"\n"
" rmmod ohci1394\n"
" insmod ohci1394 attempt_root=1\n"
"\n"
"for more information see the FAQ at \n"
"http://linux1394.sourceforge.net/faq.html#DCbusmgmt\n"
"\n");
return(false);
}
if(PRINTF_ON) printf("Initialization is complete.\n");
return(true);
}
void FWCameras::get_camera_info(int camera_num)
{
int err;
unsigned int channel, speed;
int i;
if(!cam_num_ok(camera_num)) {
printf("camera number %d is not valid\n", camera_num);
return;
}
// allocate memory space for all camera infos & download all infos:
if(num_cameras > MAX_NUM_CAMERAS)
{
printf("the number of available cameras is greater than the maximum allowed number of cameras\n");
printf("num_cameras > MAX_NUM_CAMERAS\n");
printf("recompile if you want more than %d\n\n", MAX_NUM_CAMERAS);
num_cameras = MAX_NUM_CAMERAS;
}
err=1;
i = camera_num;
err*=dc1394_get_camera_misc_info(handle, camera_nodes[i], &(cameras[i].misc_info));
err*=dc1394_get_camera_info(handle, camera_nodes[i], &(cameras[i].camera_info));
err*=dc1394_get_camera_feature_set(handle, cameras[i].camera_info.id, &(cameras[i].feature_set));
if (!err) printf("Could not get camera basic informations!\n");
printf("\n\n");
dc1394_print_feature_set(&(cameras[i].feature_set));
printf("\n\n");
if (dc1394_get_iso_channel_and_speed(handle, cameras[i].camera_info.id,
&channel, &speed)!=DC1394_SUCCESS)
printf("Can't get iso channel and speed\n");
if (dc1394_set_iso_channel_and_speed(handle, cameras[i].camera_info.id,
camera_num, speed)!=DC1394_SUCCESS)
printf("Can't set iso channel and speed\n");
}
bool FWCameras::start_firewire(int camera_num)
{
/***********************************************************
dc1394_setup_capture
Sets up both the camera and the cameracapture structure
to be used other places.
Returns DC1394_SUCCESS on success, DC1394_FAILURE otherwise
NOTE: it is important to call dc1394_release_camera
to free memory allocated by this routine- if you
don't, your application WILL leak memory
************************************************************/
if((camera_num<0)||(camera_num>=GetNumberOfCameras())){
printf("camera %d is not valid and so no attempt will be made to start it!\n", camera_num);
return false;
}
if(!dma_on)
{
if (dc1394_setup_capture(handle,camera_nodes[camera_num],
camera_num, /* channel */
FORMAT_VGA_NONCOMPRESSED,
MODE_640x480_MONO,
SPEED_400,
FRAMERATE_30, //FRAMERATE_7_5, cck 05-05-02
&(cameras[camera_num].capture))!=DC1394_SUCCESS)
{
printf( "\n"
"unable to setup camera %d:\n"
" - the camera may already be in use\n"
" - the requested video mode, framerate or format,\n"
" as specified on line %d of file \"%s\",\n"
" may not be supported by the camera\n\n",
camera_num, __LINE__,__FILE__);
//this command seems to seg fault here, not having it may lead to a memory leak
//dc1394_release_camera(handle, &(cameras[camera_num].capture));
return false;
}
}
else
{
if (access("/dev/video1394/0", R_OK) == 0)
dma_device_file = "/dev/video1394/0";
else if (access("/dev/video1394", R_OK) == 0)
dma_device_file = "/dev/video1394";
else
{
fprintf(stderr, "******************************************************\n");
fprintf(stderr, "Failed to open device file /dev/video1394\n");
fprintf(stderr, "\n");
fprintf(stderr, " Have you setup /dev/video1394 as root?\n");
fprintf(stderr, "\n");
fprintf(stderr, " mkdir /dev/video1394\n");
fprintf(stderr, " mknod -m 666 /dev/video1394/0 c 171 16\n");
fprintf(stderr, "\n");
fprintf(stderr, "******************************************************\n");
return(false);
}
int num_dma_buffers = 4;
int drop_frames = 1;
//int do_extra_buffering = 0; //1 introduces high latency when it's turned on
if(dc1394_dma_setup_capture(handle, camera_nodes[camera_num],
camera_num,
FORMAT_VGA_NONCOMPRESSED,
MODE_640x480_MONO,
SPEED_400,
FRAMERATE_30,
num_dma_buffers,
//do_extra_buffering, //removed for libdc1394-11-dev
drop_frames,
dma_device_file,
&(cameras[camera_num].capture))!=DC1394_SUCCESS)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?