detectRectangle method
检测图片中最大的矩形并返回四个顶点坐标
- Parameter imageData: 图片的字节数据
- Returns: 包含四个顶点坐标的Map,如果未检测到矩形则返回null
Implementation
@override
Future<Map<String, dynamic>?> detectRectangle(Uint8List imageData) async {
try {
// 输入验证
if (imageData.isEmpty) {
debugPrint('矩形检测错误: 图像数据为空');
return null;
}
// 确保 OpenCV.js 已加载
await _ensureOpenCVLoaded();
// 从字节数据创建图像
final imageElement = await _createImageFromBytes(imageData);
// 调用 JavaScript 函数检测矩形
final result = _detectSingleRectangleJS(imageElement as JSAny);
if (result == null) {
debugPrint('矩形检测: 未检测到矩形');
return null;
}
return _convertJSObjectToMap(result);
} catch (e, stackTrace) {
debugPrint('Web 矩形检测错误: $e');
debugPrint('堆栈跟踪: $stackTrace');
return null;
}
}