xcamera 0.0.1
xcamera: ^0.0.1 copied to clipboard
An FFI-based Flutter package designed to help Linux developers access and interact with cameras. This package utilizes Video4Linux (V4L2) to retrieve camera information, stream video, and capture imag [...]
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:xcamera/xcamera.dart';
void main() {
runApp(const XCameraExample());
}
class XCameraExample extends StatefulWidget {
const XCameraExample({super.key});
@override
State<XCameraExample> createState() => _XCameraExampleState();
}
class _XCameraExampleState extends State<XCameraExample> {
/// Manages camera operations like preview, capture, and streaming.
CameraController? _cameraController;
/// Used by the [FutureBuilder] to display a loading indicator while the
/// camera is initializing.
late Future<void> _initializeCameraFuture;
@override
void initState() {
super.initState();
// Begin camera initialization as soon as the widget is created.
_initializeCameraFuture = _initializeCamera();
}
/// Discovers available cameras, then creates and initializes a [CameraController].
Future<void> _initializeCamera() async {
try {
// First, get a list of available cameras.
final cameras = await availableCameras();
// If no cameras are found, throw an exception.
if (cameras.isEmpty) {
throw Exception('No cameras available.');
}
// Create a new [CameraController] with auto-detected settings for the first
// available camera. The `create` factory handles finding the best
// resolution and format.
final controller = await CameraController.create(cameras.first);
// Next, initialize the controller. This prepares the camera for streaming.
await controller.initialize();
// If the widget is still mounted, update the state with the new controller.
if (mounted) {
setState(() {
_cameraController = controller;
});
}
} catch (e) {
// Log any errors that occur during camera initialization
debugPrint('Error initializing camera: $e');
}
}
@override
void dispose() {
// Dispose of the controller when the widget is disposed to release resources.
_cameraController?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: FutureBuilder<void>(
future: _initializeCameraFuture,
builder: (context, snapshot) {
// While the camera is initializing, show a loading indicator.
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
// If an error occurs during initialization, display the error.
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}
// If the controller is null, it means initialization failed without an
// error being thrown. Show a fallback message.
if (_cameraController == null) {
return const Center(child: Text('No camera found.'));
}
// Once initialized, display the camera preview.
return CameraPreview(_cameraController!);
},
),
),
),
);
}
}