C# USB camera virtual PTZ control

Cameras that only have a USB connection, usually without pan/tilt (PTZ) control. Even a camera with a PTZ needs an infrared remote control, network, or RS-232 (485) to control, and it cannot be controlled only through USB.

But in the property menu of the camera, it is possible to control the "virtual" PTZ. The zooming and left-right (up-down) translation at this time is the result of the "roaming" of the visible window after the image is digitally enlarged.

 

The control of this "virtual" pan-tilt can slightly make up for the control of the USB camera pan-tilt, but it has little practical value for low-resolution, low-pixel cameras. The specific implementation is as follows:

1. Add a reference to the AForge library

2. Key code

//定义摄像头设备云台控制
public FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
public FilterInfo info = null;
public VideoCaptureDevice videoCaptureDevice = null;
……
List<string> VideoList = GetVideoInDevicesList();
VideolistBox.DataSource = VideoList;
List<string> AudioList = GetAudioInDevicesList();
AudiolistBox.DataSource = AudioList;
……
this.VideolistBox.Text = VideoDriveName;
this.AudiolistBox.Text = AudioDriveName;
……
/// <summary>
/// 获取摄像头列表
/// </summary>
/// <returns></returns>
public static List<string> GetVideoInDevicesList()
{
    List<string> devicesList = new List<string>();
    try
    {
        var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo device in videoDevices)
        {
            devicesList.Add(device.Name);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return devicesList;
}

/// <summary>
/// 获取音频设备列表
/// </summary>
/// <returns></returns>
public static List<string> GetAudioInDevicesList()
{
    List<string> devicesList = new List<string>();
    try
    {
        var videoDevices = new FilterInfoCollection(FilterCategory.AudioInputDevice);//输入设备
        foreach (FilterInfo device in videoDevices)
        {
            devicesList.Add(device.Name);
        }
    }
    catch (ApplicationException)
    {
        Console.WriteLine("No local capture devices");
    }
    return devicesList;
}
……
//摄像头云台控制初始化
info = videoDevices[VideolistBox.SelectedIndex];
videoCaptureDevice = new VideoCaptureDevice(info.MonikerString);
PTZReset();

3. Call the system control panel

try
{
    videoCaptureDevice.DisplayPropertyPage(this.Handle);
}
catch 
{
    MessageBox.Show("所选视频设备不支持设置页面","出错",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
}

4. "Zoom" code (you must zoom first, otherwise other translation functions will be invalid)

//放大
int PropertyValue = 0;
CameraControlFlags cameraControlFlags = CameraControlFlags.Manual;
videoCaptureDevice.GetCameraProperty(CameraControlProperty.Zoom, out PropertyValue, out cameraControlFlags);
Console.Write(PropertyValue);
if (PropertyValue < 400)
{
    PropertyValue = PropertyValue + 1;
}
try
{
    videoCaptureDevice.SetCameraProperty(CameraControlProperty.Zoom, PropertyValue, CameraControlFlags.Manual);
}
catch
{}
//缩小
……
if (PropertyValue > 100)
{
    PropertyValue = PropertyValue - 1;
}
try
{
    videoCaptureDevice.SetCameraProperty(CameraControlProperty.Zoom, PropertyValue, CameraControlFlags.Manual);
}
……

5. Pan up and down

//上移
int PropertyValue = 0;
CameraControlFlags cameraControlFlags = CameraControlFlags.Manual;
videoCaptureDevice.GetCameraProperty(CameraControlProperty.Tilt, out PropertyValue, out cameraControlFlags);
Console.Write(PropertyValue);
if (PropertyValue > -16)
{
    PropertyValue = PropertyValue - 1;
}
else
try
{
    videoCaptureDevice.SetCameraProperty(CameraControlProperty.Tilt, PropertyValue, CameraControlFlags.Manual);
}
//下移
……
if (PropertyValue < 16)
{
    PropertyValue = PropertyValue + 1;
}
try
{
    videoCaptureDevice.SetCameraProperty(CameraControlProperty.Tilt, PropertyValue, CameraControlFlags.Manual);
}
……

6. Pan left and right

//向右
int PropertyValue = 0;
CameraControlFlags cameraControlFlags = CameraControlFlags.Manual;
videoCaptureDevice.GetCameraProperty(CameraControlProperty.Pan, out PropertyValue, out cameraControlFlags);
if (PropertyValue < 16)
{
    PropertyValue = PropertyValue + 1;
}
try
{
    videoCaptureDevice.SetCameraProperty(CameraControlProperty.Pan, PropertyValue, CameraControlFlags.Manual);
}
//向左
……
if (PropertyValue > -16)
{
    PropertyValue = PropertyValue - 1;
}
try
{
    videoCaptureDevice.SetCameraProperty(CameraControlProperty.Pan, PropertyValue, CameraControlFlags.Manual);
}
……

7. Reset

videoCaptureDevice.SetCameraProperty(CameraControlProperty.Zoom, 100, CameraControlFlags.Manual);
videoCaptureDevice.SetCameraProperty(CameraControlProperty.Tilt, 0, CameraControlFlags.Manual);
videoCaptureDevice.SetCameraProperty(CameraControlProperty.Pan, 0, CameraControlFlags.Manual);

Guess you like

Origin blog.csdn.net/dgnankai/article/details/129142868