USB 摄像头+远程遥控+中英的适配+topic的集成和状态的使用到相应页面

This commit is contained in:
2026-10-20 17:03:37 +08:00
parent 178cd2533c
commit 582242e9aa
144 changed files with 15759 additions and 406 deletions

View File

@@ -0,0 +1,95 @@
import 'dart:async';
import 'package:flutter/services.dart';
/// 原生端返回的 UVC 设备信息。
class UsbCameraDeviceInfo {
const UsbCameraDeviceInfo({
required this.deviceId,
required this.vendorId,
required this.productId,
this.productName,
});
final String deviceId;
final int vendorId;
final int productId;
final String? productName;
factory UsbCameraDeviceInfo.fromMap(Map<dynamic, dynamic> map) {
return UsbCameraDeviceInfo(
deviceId: map['deviceId'] as String,
vendorId: map['vendorId'] as int,
productId: map['productId'] as int,
productName: map['productName'] as String?,
);
}
}
class UsbCameraAndroid {
static const MethodChannel _methodChannel =
MethodChannel('com.maibu.usb_camera_android/methods');
static const EventChannel _eventChannel =
EventChannel('com.maibu.usb_camera_android/events');
static Stream<Map<String, dynamic>>? _eventStream;
/// 原生端事件流:
/// - attached / detached:设备插拔
/// - opened:摄像头已打开并开始预览
/// - cancelled:用户拒绝 USB 权限
static Stream<Map<String, dynamic>> get eventStream {
_eventStream ??= _eventChannel
.receiveBroadcastStream()
.cast<Map<dynamic, dynamic>>()
.map((e) => Map<String, dynamic>.from(e));
return _eventStream!;
}
/// 枚举当前连接的 UVC 设备。
static Future<List<UsbCameraDeviceInfo>> getDeviceList() async {
final List<dynamic> result =
await _methodChannel.invokeMethod('getDeviceList');
return result
.map((e) => UsbCameraDeviceInfo.fromMap(e as Map<dynamic, dynamic>))
.toList();
}
/// 获取当前已打开摄像头支持的分辨率列表。
static Future<List<String>> getSupportedResolutions() async {
final List<dynamic> result =
await _methodChannel.invokeMethod('getSupportedResolutions');
return result.cast<String>();
}
/// 获取当前预览尺寸(摄像头已打开后有效),返回 (width, height);未打开返回 null。
static Future<({int width, int height})?> getPreviewSize() async {
final result = await _methodChannel.invokeMethod('getPreviewSize');
if (result == null) return null;
final map = Map<dynamic, dynamic>.from(result as Map);
return (width: map['width'] as int, height: map['height'] as int);
}
/// 打开指定设备。
static Future<void> open(
String deviceId, {
String? resolution,
}) async {
await _methodChannel.invokeMethod('open', {
'deviceId': deviceId,
'resolution': resolution,
});
}
/// 关闭当前设备。
static Future<void> close() async {
await _methodChannel.invokeMethod('close');
}
/// 切换分辨率。
static Future<void> setResolution(String resolution) async {
await _methodChannel.invokeMethod('setResolution', {
'resolution': resolution,
});
}
}

View File

@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// Android 原生 UVC 预览画面。
class UsbCameraPreviewView extends StatelessWidget {
const UsbCameraPreviewView({super.key});
static const String _viewType = 'com.maibu.usb_camera_android/preview';
@override
Widget build(BuildContext context) {
return const AndroidView(
viewType: _viewType,
layoutDirection: TextDirection.ltr,
creationParamsCodec: StandardMessageCodec(),
);
}
}