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,35 @@
import 'package:shared_preferences/shared_preferences.dart';
/// 记录上一次使用的 USB 摄像头,插拔后优先重连。
class UsbCameraPrefs {
static const String _lastDeviceIdKey = 'usb_camera_last_device_id';
static const String _lastResolutionKey = 'usb_camera_last_resolution';
static Future<String?> getLastDeviceId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_lastDeviceIdKey);
}
static Future<void> setLastDeviceId(String? id) async {
final prefs = await SharedPreferences.getInstance();
if (id == null) {
await prefs.remove(_lastDeviceIdKey);
return;
}
await prefs.setString(_lastDeviceIdKey, id);
}
static Future<String?> getLastResolution() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_lastResolutionKey);
}
static Future<void> setLastResolution(String? resolution) async {
final prefs = await SharedPreferences.getInstance();
if (resolution == null) {
await prefs.remove(_lastResolutionKey);
return;
}
await prefs.setString(_lastResolutionKey, resolution);
}
}

View File

@@ -0,0 +1,75 @@
import 'dart:async';
import 'package:usb_camera_android/usb_camera_android.dart' as plugin;
import '../domain/usb_camera_device.dart';
import 'usb_camera_prefs.dart';
/// UVC 摄像头原生能力封装。
class UvcCameraService {
bool _isOpen = false;
StreamSubscription? _eventSub;
/// 枚举当前已连接的 UVC 设备。
Future<List<UsbCameraDevice>> enumerateDevices() async {
final list = await plugin.UsbCameraAndroid.getDeviceList();
return list
.map(
(d) => UsbCameraDevice(
deviceId: d.deviceId,
vendorId: d.vendorId,
productId: d.productId,
productName: d.productName,
),
)
.toList();
}
/// 请求并打开指定设备。
Future<void> open(
UsbCameraDevice device, {
String? resolution,
}) async {
await UsbCameraPrefs.setLastDeviceId(device.deviceId);
if (resolution != null) {
await UsbCameraPrefs.setLastResolution(resolution);
}
await plugin.UsbCameraAndroid.open(
device.deviceId,
resolution: resolution,
);
_isOpen = true;
}
/// 关闭当前设备。
Future<void> close() async {
if (!_isOpen) return;
_isOpen = false;
await plugin.UsbCameraAndroid.close();
}
/// 切换分辨率。
Future<void> setResolution(String resolution) async {
await UsbCameraPrefs.setLastResolution(resolution);
await plugin.UsbCameraAndroid.setResolution(resolution);
}
/// 监听原生端插拔/权限等事件。
void startListeningEvents(
void Function(String type, String? deviceId) onEvent,
) {
_eventSub?.cancel();
_eventSub = plugin.UsbCameraAndroid.eventStream.listen((event) {
final type = event['type'] as String?;
final data = event['data'] as Map?;
final deviceId = data?['deviceId'] as String?;
onEvent(type ?? 'unknown', deviceId);
});
}
/// 释放事件监听。
Future<void> dispose() async {
await _eventSub?.cancel();
_eventSub = null;
}
}