📄 main.cpp
字号:
/*****************************************************************************
* DirectShow Pan/Tilt/Zoom sample for Logitech QuickCam devices
*
* Copyright 2007 (c) Logitech. All Rights Reserved.
*
* This code and information is provided "as is" without warranty of
* any kind, either expressed or implied, including but not limited to
* the implied warranties of merchantability and/or fitness for a
* particular purpose.
*
* Version: 1.1
****************************************************************************/
#include <dshow.h>
#include <Ks.h> // Required by KsMedia.h
#include <KsMedia.h> // For KSPROPERTY_CAMERACONTROL_FLAGS_*
struct ControlInfo {
long min;
long max;
long step;
long def;
long flags;
};
/*
* Print information about a control in an easily readable fashion.
*/
void print_control_info(ControlInfo *info)
{
char flags[32] = "";
if(info->flags & KSPROPERTY_CAMERACONTROL_FLAGS_AUTO)
{
strcat_s(flags, sizeof(flags), "AUTO | ");
}
else if(info->flags & KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL)
{
strcat_s(flags, sizeof(flags), "MANUAL | ");
}
if(info->flags & KSPROPERTY_CAMERACONTROL_FLAGS_RELATIVE)
{
strcat_s(flags, sizeof(flags), "RELATIVE");
}
else
{
strcat_s(flags, sizeof(flags), "ABSOLUTE");
}
printf(
" min: %d\n"
" max: %d\n"
" step: %d\n"
" def: %d\n"
" flags: 0x%08X (%s)\n",
info->min, info->max, info->step, info->def, info->flags, flags
);
}
/*
* Pans the camera by a given angle.
*
* The angle is given in degrees, positive values are clockwise rotation (seen from the top),
* negative values are counter-clockwise rotation. If the "Mirror horizontal" option is
* enabled, the panning sense is reversed.
*/
HRESULT set_mechanical_pan_relative(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_RELATIVE | KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
hr = pCameraControl->Set(CameraControl_Pan, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Pan property value to %d. (Error 0x%08X)\n", value, hr);
// Note that we need to wait until the movement is complete, otherwise the next request will
// fail with hr == 0x800700AA == HRESULT_FROM_WIN32(ERROR_BUSY).
//Sleep(500);
return hr;
}
/*
* Tilts the camera by a given angle.
*
* The angle is given in degrees, positive values are downwards, negative values are upwards.
* If the "Mirror vertical" option is enabled, the tilting sense is reversed.
*/
HRESULT set_mechanical_tilt_relative(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_RELATIVE | KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
hr = pCameraControl->Set(CameraControl_Tilt, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Tilt property value to %d. (Error 0x%08X)\n", value, hr);
// Note that we need to wait until the movement is complete, otherwise the next request will
// fail with hr == 0x800700AA == HRESULT_FROM_WIN32(ERROR_BUSY).
//Sleep(500);
return hr;
}
/*
* Pans the camera by a given angle.
*
* The angle is given in degrees, positive values are clockwise rotation (seen from the top),
* negative values are counter-clockwise rotation. If the "Mirror horizontal" option is
* enabled, the panning sense is reversed.
*/
HRESULT set_mechanical_pan_absolute(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE | KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
hr = pCameraControl->Set(CameraControl_Pan, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Pan property value to %d. (Error 0x%08X)\n", value, hr);
// Note that we need to wait until the movement is complete, otherwise the next request will
// fail with hr == 0x800700AA == HRESULT_FROM_WIN32(ERROR_BUSY).
//Sleep(500);
return hr;
}
/*
* Tilts the camera by a given angle.
*
* The angle is given in degrees, positive values are downwards, negative values are upwards.
* If the "Mirror vertical" option is enabled, the tilting sense is reversed.
*/
HRESULT set_mechanical_tilt_absolute(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE | KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
hr = pCameraControl->Set(CameraControl_Tilt, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Tilt property value to %d. (Error 0x%08X)\n", value, hr);
// Note that we need to wait until the movement is complete, otherwise the next request will
// fail with hr == 0x800700AA == HRESULT_FROM_WIN32(ERROR_BUSY).
//Sleep(500);
return hr;
}
/*
* Resets the camera's pan/tilt position by moving into a corner and then back to the center.
*/
void reset_machanical_pan_tilt(IAMCameraControl *pCameraControl)
{
set_mechanical_pan_relative(pCameraControl, 180);
Sleep(500);
set_mechanical_tilt_relative(pCameraControl, 180);
Sleep(500);
set_mechanical_pan_relative(pCameraControl, -64);
Sleep(500);
set_mechanical_tilt_relative(pCameraControl, -24);
Sleep(500);
}
/*
* Sets the digital pan angle.
*
* Positive values pan to the right, negative values pan to the left. Note that the digital pan
* angle only has an influence if the digital zoom is active.
*/
HRESULT set_digital_pan_absolute(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
// Specifying the KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE flag instructs the driver
// to use digital instead of mechanical pan.
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE | KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
hr = pCameraControl->Set(CameraControl_Pan, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Pan property value to %d. (Error 0x%08X)\n", value, hr);
return hr;
}
/*
* Sets the digital tilt angle.
*
* Positive values tilt downwards, negative values tilt upwards. Note that the digital pan
* angle only has an influence if the digital zoom is active.
*/
HRESULT set_digital_tilt_absolute(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
// Specifying the KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE flag instructs the driver
// to use digital instead of mechanical tilt.
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE | KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
hr = pCameraControl->Set(CameraControl_Tilt, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Tilt property value to %d. (Error 0x%08X)\n", value, hr);
return hr;
}
/*
* Sets the digital zoom value.
*
* The minimum value is 50 and means no zoom (100%). The maximum value is 200
* and means 4x zoom (400%).
*/
HRESULT set_digital_zoom_absolute(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE | KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
hr = pCameraControl->Set(CameraControl_Zoom, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Zoom property value to %d. (Error 0x%08X)\n", value, hr);
return hr;
}
/*
* Sets the focus value.
*
*/
HRESULT set_focus_absolute(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE | KSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
hr = pCameraControl->Set(CameraControl_Focus, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Zoom property value to %d. (Error 0x%08X)\n", value, hr);
return hr;
}
HRESULT set_autofocus_absolute(IAMCameraControl *pCameraControl, long value)
{
HRESULT hr = 0;
long flags = KSPROPERTY_CAMERACONTROL_FLAGS_ABSOLUTE | KSPROPERTY_CAMERACONTROL_FLAGS_AUTO;
hr = pCameraControl->Set(CameraControl_Focus, value, flags);
if(hr != S_OK)
fprintf(stderr, "ERROR: Unable to set CameraControl_Zoom property value to %d. (Error 0x%08X)\n", value, hr);
return hr;
}
/*
* Resets the digital pan and tilt angles.
*/
void reset_digital_pan_tilt(IAMCameraControl *pCameraControl)
{
set_digital_pan_absolute(pCameraControl, 0);
set_digital_tilt_absolute(pCameraControl, 0);
}
/*
* Resets the digital zoom.
*/
void reset_digital_zoom(IAMCameraControl *pCameraControl)
{
set_digital_zoom_absolute(pCameraControl, 50);
}
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -