How to take picture using the camera in Flutter.

Gourav Rathor
Apr 30, 2021
  1. Add the dependencies
    dependencies:
    flutter:
    sdk: flutter
    camera:
    path_provider:
    path:
  2. Make a list of all available cameras
    WidgetsFlutterBinding.ensureInitialized();
    final cameras = await availableCameras();
    final firstCamera = cameras.first;
  3. Create and initialize the Cameracontroller

CameraController _cameraController;

@override
void initState() {
// TODO: implement initState
super.initState();
_cameraController = CameraController(cameras[0], ResolutionPreset.high);
cameraValue = _cameraController.initialize();
}

4. Take a picture with the CameraController

FloatingActionButton(
child: Icon(Icons.camera_alt),
onPressed: () async {
await _initializeControllerFuture;
final image = await _controller.takePicture();
},
)

5.Display the picture with an Image widget

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Display the Picture')),
body: Image.file(File(imagePath)),
);
}

For more detailed information please visit https://flutter.dev/docs/cookbook/plugins/picture-using-camera.

--

--