add: 添加分辨率设置逻辑

This commit is contained in:
alivender
2025-07-13 11:42:26 +08:00
parent b913f58f13
commit 352ee1f4f2
5 changed files with 581 additions and 21 deletions

View File

@@ -26,19 +26,13 @@ class Camera
const uint CAM_I2C_ADDR = 0x3C;
const Peripherals.I2cClient.I2cProtocol CAM_PROTO = Peripherals.I2cClient.I2cProtocol.SCCB;
const UInt16 H_START = 0; //default: 0
const UInt16 V_START = 0; //default: 0
const UInt16 DVPHO = 640; //default: 2592 (0xA20)
const UInt16 DVPVO = 480; //default: 1944 (0x798)
const UInt16 H_END = H_START + 1500 - 1; //default: 2624-1 (0xA3F)
const UInt16 V_END = V_START + 1300 - 1; //default: 1951-1 (0x79F)
const UInt16 HTS = 1700; //default: 2844 (0xB1C)
const UInt16 VTS = 1500; //default: 1968 (0x7B0)
const UInt16 H_OFFSET = 16; //default: 16 (0x10)
const UInt16 V_OFFSET = 4; //default: 4 (0x04)
const byte PLL_MUX = 10;
const UInt32 FrameAddr = 0x00;
const UInt32 FrameLength = DVPHO * DVPVO * 16 / 32;
// 动态分辨率参数
private UInt16 _currentWidth = 640;
private UInt16 _currentHeight = 480;
private UInt32 _currentFrameLength = 640 * 480 * 2 / 4; // RGB565格式2字节/像素按4字节对齐
/// <summary>
@@ -183,8 +177,7 @@ class Camera
this.ep,
this.taskID, // taskID
FrameAddr,
// ((int)FrameLength),
1280*720/2,
(int)(_currentWidth * _currentHeight * 2), // 使用当前分辨率的动态大小
this.timeout);
if (!result.IsSuccessful)
@@ -423,6 +416,67 @@ class Camera
);
}
/// <summary>
/// 切换摄像头分辨率
/// </summary>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns>配置结果</returns>
public async ValueTask<Result<bool>> ChangeResolution(int width, int height)
{
try
{
logger.Info($"正在切换摄像头分辨率到 {width}x{height}");
Result<bool> result;
switch ($"{width}x{height}")
{
case "640x480":
result = await ConfigureResolution640x480();
break;
case "1280x720":
result = await ConfigureResolution1280x720();
break;
default:
logger.Error($"不支持的分辨率: {width}x{height}");
return new(new ArgumentException($"不支持的分辨率: {width}x{height}"));
}
if (result.IsSuccessful)
{
_currentWidth = (UInt16)width;
_currentHeight = (UInt16)height;
_currentFrameLength = (UInt32)(width * height * 2 / 4); // RGB565格式按4字节对齐
logger.Info($"摄像头分辨率已切换到 {width}x{height}");
}
return result;
}
catch (Exception ex)
{
logger.Error(ex, $"切换分辨率到 {width}x{height} 时发生错误");
return new(ex);
}
}
/// <summary>
/// 获取当前分辨率
/// </summary>
/// <returns>当前分辨率(宽度, 高度)</returns>
public (int Width, int Height) GetCurrentResolution()
{
return (_currentWidth, _currentHeight);
}
/// <summary>
/// 获取当前帧长度
/// </summary>
/// <returns>当前帧长度</returns>
public UInt32 GetCurrentFrameLength()
{
return _currentFrameLength;
}
/// <summary>
/// 复位摄像头
/// </summary>