Camera access lets your app take pictures or record videos. It helps users capture moments directly inside your app.
0
0
Camera access in Flutter
Introduction
You want users to take profile photos inside your app.
Your app needs to scan QR codes or barcodes using the camera.
You want to let users record videos for sharing or saving.
Your app offers augmented reality features that use the camera view.
You want to build a photo editing app that starts with a camera capture.
Syntax
Flutter
import 'package:camera/camera.dart'; // Initialize camera final cameras = await availableCameras(); final firstCamera = cameras.first; // Create controller final controller = CameraController( firstCamera, ResolutionPreset.high, ); await controller.initialize(); // Use controller to show preview or take picture
You must add camera package in pubspec.yaml to use camera features.
Always initialize the camera controller before using it.
Examples
This gets the list of available cameras and selects the first one (usually the back camera).
Flutter
final cameras = await availableCameras(); final camera = cameras.first;
Create a controller to manage the camera and initialize it before use.
Flutter
final controller = CameraController(camera, ResolutionPreset.medium);
await controller.initialize();Take a picture using the initialized camera controller.
Flutter
await controller.takePicture();
Sample App
This app opens the device camera and shows a live preview. When you tap the camera button, it takes a picture and shows it in a popup.
Flutter
import 'package:flutter/material.dart'; import 'package:camera/camera.dart'; import 'dart:io'; void main() async { WidgetsFlutterBinding.ensureInitialized(); final cameras = await availableCameras(); final firstCamera = cameras.first; runApp(MyApp(camera: firstCamera)); } class MyApp extends StatelessWidget { final CameraDescription camera; const MyApp({super.key, required this.camera}); @override Widget build(BuildContext context) { return MaterialApp( home: CameraScreen(camera: camera), ); } } class CameraScreen extends StatefulWidget { final CameraDescription camera; const CameraScreen({super.key, required this.camera}); @override State<CameraScreen> createState() => _CameraScreenState(); } class _CameraScreenState extends State<CameraScreen> { late CameraController _controller; late Future<void> _initializeControllerFuture; @override void initState() { super.initState(); _controller = CameraController( widget.camera, ResolutionPreset.medium, ); _initializeControllerFuture = _controller.initialize(); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Camera Access Example')), body: FutureBuilder<void>( future: _initializeControllerFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return CameraPreview(_controller); } else { return const Center(child: CircularProgressIndicator()); } }, ), floatingActionButton: FloatingActionButton( onPressed: () async { try { await _initializeControllerFuture; final image = await _controller.takePicture(); if (!mounted) return; await showDialog( context: context, builder: (_) => AlertDialog( content: Image.file( File(image.path), fit: BoxFit.cover, ), ), ); } catch (e) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Error: $e')), ); } }, child: const Icon(Icons.camera_alt), ), ); } }
OutputSuccess
Important Notes
Remember to add camera permissions in your AndroidManifest.xml and Info.plist files.
Test camera features on a real device for best results.
Handle errors like camera not available or permission denied gracefully.
Summary
Camera access lets apps take pictures or videos inside the app.
Use the camera package to control the device camera in Flutter.
Always initialize the camera controller before showing preview or taking photos.