A comprehensive Flutter example demonstrating how to implement and use the camera package for creating live camera feeds in your Flutter applications.
🌟 Features
- 📷 Live camera feed display
- 📱 Supports both front and back cameras
- 🚀 Easy integration with Flutter
- Flutter SDK
- Android Studio / VS Code
- Basic understanding of Flutter development
-
Add the following dependency to your
pubspec.yaml
dependencies camera <latest-version>
-
Run
flutter pub get
-
Change the minimum Android SDK requirements in
android/app/build.gradle
android { compileSdkVersion 34 defaultConfig { minSdkVersion 21 targetSdkVersion 34 } }
-
Add required permissions in
android/app/src/main/AndroidManifest.xml
<manifest xmlnsandroid="http//schemas.android.com/apk/res/android" package="com.example.myapp"> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> ... </manifest>
-
Add usage descriptions in
ios/Runner/Info.plist
<key>NSCameraUsageDescription</key> <string>Allow access to the camera for live feed.</string> <key>NSMicrophoneUsageDescription</key> <string>Allow access to the microphone for audio input.</string>
Make sure to initialize the camera in your main.dart
late List<CameraDescription> cameras;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
cameras = await availableCameras();
runApp(const MyApp());
}
Set up the CameraController
in your widget
class _MyHomePageState extends State<MyHomePage> {
late CameraController _controller;
@override
void initState() {
super.initState();
_controller = CameraController(cameras[0], ResolutionPreset.max);
_controller.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
});
}
}
lib/
├── app.dart
├── main.dart
└── home_page.dart
A utility class that handles
- Initializing the camera
- Displaying the live camera feed
- Managing camera settings
A dedicated widget for displaying
- The live camera feed using
CameraPreview
- Handling camera initialization and errors
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Special thanks to the camera package maintainers.
"مَن صَلَّى عَلَيَّ واحِدَةً صَلَّى اللَّهُ عليه عَشْرًا."
صحيح مسلم