a_cameras 1.0.9 a_cameras: ^1.0.9 copied to clipboard
A Flutter plugin for accessing camera functionalities across various platforms with a unified API. Currently supports web only.
Cameras Plugin #
A Flutter plugin for accessing camera features across multiple platforms with a single unified API. Currently supports Web platform, with more platforms coming soon.
Features #
- Access camera streams
- Capture images
- Switch between available cameras
- Unified API across platforms
Installation #
Add the following to your pubspec.yaml
file:
dependencies:
a_cameras: ^1.0.9
Then run flutter pub get
.
Platform Specific Configuration #
iOS #
Before using this plugin on iOS, you need to update your Info.plist
file with permission descriptions for camera and microphone access. These keys are required for iOS applications that access the camera or microphone.
Add the following keys to your ios/Runner/Info.plist
:
Privacy - Camera Usage Description
: Provide a message describing why your app needs access to the camera.Privacy - Microphone Usage Description
: Provide a message describing why your app needs access to the microphone.
Here is how you can add them in text format:
<key>NSCameraUsageDescription</key>
<string>your usage description here</string>
<key>NSMicrophoneUsageDescription</key>
<string>your usage description here</string>
Android #
For Android, you'll need to ensure that the minimum SDK version is set to 21 or higher. This is required for the plugin to work correctly.
Update your android/app/build.gradle
file with the following:
minSdkVersion 21
Usage #
Initialize the Camera #
First, import the package and initialize the camera.
import 'package:a_cameras/a_cameras.dart';
final camerasPlugin = Cameras();
List<CameraDescription> availableCameras = await camerasPlugin.getAvailableCameras();
CameraController controller = await camerasPlugin.getCameraController();
await controller.initializeCamera(availableCameras.first);
Start/Stop Camera Stream #
You can start and stop the camera stream using the following methods.
await controller.startStream();
await controller.stopStream();
Capture an Image #
To capture an image, use the following method.
Uint8List? imageBytes = await controller.captureImage();
Switch Between Available Cameras #
To switch between the available cameras, you can use your own logic. Here's a simple example:
Future<void> switchCamera() async {
final newIndex = (currentCameraIndex + 1) % availableCameras.length;
await controller.initializeCamera(availableCameras[newIndex]);
setState(() {
currentCameraIndex = newIndex;
});
}
Display the Camera Stream #
To display the camera stream on the screen, you can use the buildPreview
method from the controller inside your widget tree. Here's a simple example:
import 'package:flutter/material.dart';
import 'package:a_cameras/a_cameras.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late CameraController controller;
@override
void initState() {
super.initState();
initCamera();
}
Future<void> initCamera() async {
final camerasPlugin = Cameras();
List<CameraDescription> availableCameras = await camerasPlugin.getAvailableCameras();
controller = await camerasPlugin.getCameraController();
await controller.initializeCamera(availableCameras.first);
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: controller.buildPreview(context),
),
);
}
}
In this example, we used controller.buildPreview(context)
to display the camera stream within a Scaffold'
Supported Platforms #
- ✅ Web
- ✅ Android
- ✅ iOS
Contributing #
Contributions are welcome! Feel free to open an issue or submit a pull request if you have any improvements.
License #
MIT License.