### Import uvccamera in Dart Source: https://pub.dev/packages/uvccamera/install Import the uvccamera package into your Dart files to start using its functionalities. ```dart import 'package:uvccamera/uvccamera.dart'; ``` -------------------------------- ### Initialize the Flutter Application Source: https://pub.dev/packages/uvccamera/example Set up the main entry point for the application. ```dart import 'package:flutter/material.dart'; import 'uvccamera_demo_app.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(const UvcCameraDemoApp()); } ``` -------------------------------- ### Build Camera UI Source: https://pub.dev/packages/uvccamera/example Renders the camera preview and controls based on connection and permission states. ```dart @override Widget build(BuildContext context) { if (!_isDeviceAttached) { return Center(child: Text('Device is not attached', style: TextStyle(fontSize: 18))); } if (!_hasCameraPermission) { return Center(child: Text('Camera permission is not granted', style: TextStyle(fontSize: 18))); } if (!_hasDevicePermission) { return Center(child: Text('Device permission is not granted', style: TextStyle(fontSize: 18))); } if (!_isDeviceConnected) { return Center(child: Text('Device is not connected', style: TextStyle(fontSize: 18))); } return FutureBuilder( future: _cameraControllerInitializeFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return Stack( children: [ Align( alignment: Alignment.topCenter, child: UvcCameraPreview( _cameraController!, child: Padding( padding: const EdgeInsets.all(8.0), child: SingleChildScrollView( child: SelectableText( _log, style: TextStyle(color: Colors.red, fontFamily: 'Courier', fontSize: 10.0), ), ), ), ), ), Align( alignment: Alignment.bottomCenter, child: Padding( padding: const EdgeInsets.only(bottom: 50.0), child: ValueListenableBuilder( valueListenable: _cameraController!, builder: (context, value, child) { return Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ FloatingActionButton( backgroundColor: Colors.white, onPressed: value.isTakingPicture ? null : () async { await _takePicture(); }, child: Icon(Icons.camera_alt, color: Colors.black), ), FloatingActionButton( backgroundColor: value.isRecordingVideo ? Colors.red : Colors.white, onPressed: () async { if (value.isRecordingVideo) { await _stopVideoRecording(); } else { await _startVideoRecording(value.previewMode!); ``` -------------------------------- ### Define the Demo Application Widget Source: https://pub.dev/packages/uvccamera/example Create the root widget for the demo application. ```dart import 'package:flutter/material.dart'; import 'uvccamera_devices_screen.dart'; class UvcCameraDemoApp extends StatefulWidget { const UvcCameraDemoApp({super.key}); @override State createState() => _UvcCameraDemoAppState(); } class _UvcCameraDemoAppState extends State { @override Widget build(BuildContext context) { return MaterialApp( title: 'UVC Camera Example', home: Scaffold(appBar: AppBar(title: const Text('UVC Camera Example')), body: UvcCameraDevicesScreen()), ); } } ``` -------------------------------- ### Configure pubspec.yaml for UVCCamera Source: https://pub.dev/packages/uvccamera/example Include the uvccamera dependency and required permissions in your project configuration. ```yaml name: uvccamera_example description: "Demonstrates how to use the uvccamera plugin." publish_to: none environment: sdk: ^3.7.0 dependencies: flutter: sdk: flutter uvccamera: 0.0.13 cross_file: ^0.3.4+2 cupertino_icons: ^1.0.8 permission_handler: ^11.3.1 dev_dependencies: integration_test: sdk: flutter flutter_test: sdk: flutter flutter_lints: ^5.0.0 flutter: uses-material-design: true ``` -------------------------------- ### Camera Recording and Capture Methods Source: https://pub.dev/packages/uvccamera/example Methods to start/stop video recording and capture images. ```dart Future _startVideoRecording(UvcCameraMode videoRecordingMode) async { await _cameraController!.startVideoRecording(videoRecordingMode); } Future _takePicture() async { final XFile outputFile = await _cameraController!.takePicture(); outputFile.length().then((length) { setState(() { _log = 'image file: ${outputFile.path} ($length bytes) $_log'; }); }); } Future _stopVideoRecording() async { final XFile outputFile = await _cameraController!.stopVideoRecording(); outputFile.length().then((length) { setState(() { _log = 'video file: ${outputFile.path} ($length bytes) $_log'; }); }); } ``` -------------------------------- ### Request Camera and Device Permissions Source: https://pub.dev/packages/uvccamera/example Requests camera access first, followed by UVC device permission. ```dart Future _requestPermissions() async { final hasCameraPermission = await _requestCameraPermission().then((value) { setState(() { _hasCameraPermission = value; }); return value; }); // NOTE: Requesting UVC device permission can be made only after camera permission is granted if (!hasCameraPermission) { return; } _requestDevicePermission().then((value) { setState(() { _hasDevicePermission = value; }); return value; }); } ``` -------------------------------- ### Implement UvcCameraWidget for UVC device management Source: https://pub.dev/packages/uvccamera/example A StatefulWidget that handles device attachment, permission requests, and event stream subscriptions for UVC cameras. ```dart import 'dart:async'; import 'package:cross_file/cross_file.dart'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:uvccamera/uvccamera.dart'; class UvcCameraWidget extends StatefulWidget { final UvcCameraDevice device; const UvcCameraWidget({super.key, required this.device}); @override State createState() => _UvcCameraWidgetState(); } class _UvcCameraWidgetState extends State with WidgetsBindingObserver { bool _isAttached = false; bool _hasDevicePermission = false; bool _hasCameraPermission = false; bool _isDeviceAttached = false; bool _isDeviceConnected = false; UvcCameraController? _cameraController; Future? _cameraControllerInitializeFuture; StreamSubscription? _errorEventSubscription; StreamSubscription? _statusEventSubscription; StreamSubscription? _buttonEventSubscription; StreamSubscription? _deviceEventSubscription; String _log = ''; @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); _attach(); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); _detach(force: true); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { _attach(); } else if (state == AppLifecycleState.paused) { _detach(); } } void _attach({bool force = false}) { if (_isAttached && !force) { return; } UvcCamera.getDevices().then((devices) { if (!devices.containsKey(widget.device.name)) { return; } setState(() { _isDeviceAttached = true; }); _requestPermissions(); }); _deviceEventSubscription = UvcCamera.deviceEventStream.listen((event) { if (event.device.name != widget.device.name) { return; } if (event.type == UvcCameraDeviceEventType.attached && !_isDeviceAttached) { // NOTE: Requesting UVC device permission will trigger connection request _requestPermissions(); } setState(() { if (event.type == UvcCameraDeviceEventType.attached) { // _hasCameraPermission - maybe // _hasDevicePermission - maybe _isDeviceAttached = true; _isDeviceConnected = false; } else if (event.type == UvcCameraDeviceEventType.detached) { _hasCameraPermission = false; _hasDevicePermission = false; _isDeviceAttached = false; _isDeviceConnected = false; } else if (event.type == UvcCameraDeviceEventType.connected) { _hasCameraPermission = true; _hasDevicePermission = true; _isDeviceAttached = true; _isDeviceConnected = true; _log = ''; _cameraController = UvcCameraController(device: widget.device); _cameraControllerInitializeFuture = _cameraController!.initialize().then((_) async { _errorEventSubscription = _cameraController!.cameraErrorEvents.listen((event) { setState(() { _log = 'error: ${event.error}\n$_log'; }); if (event.error.type == UvcCameraErrorType.previewInterrupted) { _detach(); _attach(); } }); _statusEventSubscription = _cameraController!.cameraStatusEvents.listen((event) { setState(() { _log = 'status: ${event.payload}\n$_log'; }); }); _buttonEventSubscription = _cameraController!.cameraButtonEvents.listen((event) { setState(() { _log = 'btn(${event.button}): ${event.state}\n$_log'; }); }); }); } else if (event.type == UvcCameraDeviceEventType.disconnected) { _hasCameraPermission = false; _hasDevicePermission = false; // _isDeviceAttached - maybe? _isDeviceConnected = false; _buttonEventSubscription?.cancel(); _buttonEventSubscription = null; _statusEventSubscription?.cancel(); _statusEventSubscription = null; _errorEventSubscription?.cancel(); _errorEventSubscription = null; _cameraController?.dispose(); _cameraController = null; _cameraControllerInitializeFuture = null; _log = ''; } }); }); _isAttached = true; } void _detach({bool force = false}) { if (!_isAttached && !force) { return; } _hasDevicePermission = false; _hasCameraPermission = false; _isDeviceAttached = false; _isDeviceConnected = false; _buttonEventSubscription?.cancel(); _buttonEventSubscription = null; ``` -------------------------------- ### Request Camera Permission Source: https://pub.dev/packages/uvccamera/example Checks and requests system camera permissions. ```dart Future _requestCameraPermission() async { var cameraPermissionStatus = await Permission.camera.status; if (cameraPermissionStatus.isGranted) { return true; } else if (cameraPermissionStatus.isDenied || cameraPermissionStatus.isRestricted) { cameraPermissionStatus = await Permission.camera.request(); return cameraPermissionStatus.isGranted; } else { // NOTE: Permission is permanently denied return false; } } ``` -------------------------------- ### List Connected UVC Devices Source: https://pub.dev/packages/uvccamera/example Manage device discovery and display a list of connected UVC cameras. ```dart import 'dart:async'; import 'package:flutter/material.dart'; import 'package:uvccamera/uvccamera.dart'; import 'uvccamera_device_screen.dart'; class UvcCameraDevicesScreen extends StatefulWidget { const UvcCameraDevicesScreen({super.key}); @override State createState() => _UvcCameraDevicesScreenState(); } class _UvcCameraDevicesScreenState extends State { bool _isSupported = false; StreamSubscription? _deviceEventSubscription; final Map _devices = {}; @override void initState() { super.initState(); UvcCamera.isSupported().then((value) { setState(() { _isSupported = value; }); }); _deviceEventSubscription = UvcCamera.deviceEventStream.listen((event) { setState(() { if (event.type == UvcCameraDeviceEventType.attached) { _devices[event.device.name] = event.device; } else if (event.type == UvcCameraDeviceEventType.detached) { _devices.remove(event.device.name); } }); }); UvcCamera.getDevices().then((devices) { setState(() { _devices.addAll(devices); }); }); } @override void dispose() { _deviceEventSubscription?.cancel(); _deviceEventSubscription = null; super.dispose(); } @override Widget build(BuildContext context) { if (!_isSupported) { return const Center(child: Text('UVC Camera is not supported on this device.', style: TextStyle(fontSize: 18))); } if (_devices.isEmpty) { return const Center(child: Text('No UVC devices connected.', style: TextStyle(fontSize: 18))); } return ListView( children: _devices.values.map((device) { return ListTile( leading: const Icon(Icons.videocam), title: Text(device.name), subtitle: Text('Vendor ID: ${device.vendorId}, Product ID: ${device.productId}'), onTap: () { Navigator.push(context, MaterialPageRoute(builder: (context) => UvcCameraDeviceScreen(device: device))); }, ); }).toList(), ); } } ``` -------------------------------- ### Request UVC Device Permission Source: https://pub.dev/packages/uvccamera/example Requests permission for the specific UVC device. ```dart Future _requestDevicePermission() async { final devicePermissionStatus = await UvcCamera.requestDevicePermission(widget.device); return devicePermissionStatus; } ``` -------------------------------- ### Display a Specific UVC Camera Device Source: https://pub.dev/packages/uvccamera/example Implement a screen to display a specific camera device using the UvcCameraWidget. ```dart import 'package:flutter/material.dart'; import 'package:uvccamera/uvccamera.dart'; import 'uvccamera_widget.dart'; class UvcCameraDeviceScreen extends StatelessWidget { final UvcCameraDevice device; const UvcCameraDeviceScreen({super.key, required this.device}); @override Widget build(BuildContext context) { return Scaffold(appBar: AppBar(title: Text(device.name)), body: Center(child: UvcCameraWidget(device: device))); } } ``` -------------------------------- ### uvccamera pubspec.yaml Entry Source: https://pub.dev/packages/uvccamera/install This is how the uvccamera dependency will appear in your pubspec.yaml file after running `flutter pub add`. ```yaml dependencies: uvccamera: ^0.0.13 ``` -------------------------------- ### Dispose Camera Resources Source: https://pub.dev/packages/uvccamera/example Cleans up subscriptions and controllers to prevent memory leaks. ```dart _statusEventSubscription?.cancel(); _statusEventSubscription = null; _cameraController?.dispose(); _cameraController = null; _cameraControllerInitializeFuture = null; _deviceEventSubscription?.cancel(); _deviceEventSubscription = null; _isAttached = false; } ``` -------------------------------- ### Add uvccamera Dependency Source: https://pub.dev/packages/uvccamera/install Use this command to add the uvccamera package to your Flutter project. It automatically updates your pubspec.yaml file. ```bash $ flutter pub add uvccamera ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.