Taking pictures with Flutter
Tags: camera, flutter
Published: 2022-03-31 09:26:07

---
title: Taking pictures with Flutter
published: true
date: 2022-02-28 18:51:08 UTC
tags: Camera,Flutter
canonical_url: https://bartvwezel.nl/flutter/taking-pictures-with-flutter/
cover_image: https://bartvwezel.nl/wp-content/uploads/2022/02/camera-g20ab4b6c2_1920-768x576.jpg
series: Flutter Camera
---

In this blog post, I will show you how to use the [camera package](https://pub.dev/packages/camera) to take a picture in Flutter. Furthermore, I will provide an example of how to deal with the result file. In this example, I will open another page to display the picture. Please note that even though most of the features of the camera package are working, the plugin is still under development and some of the APIs might change.

## Setup the project

Before we can start with coding, we are going to add the [camera package](https://pub.dev/packages/camera) to the project.

```YAML
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  camera: ^0.9.4+14
```

Do not forget to install the dependency, running the following command:

```
flutter pub get
```

This is also a good time to make sure your settings for Android and IOs are correct. For Android, you have to make sure the minSdkVersion in the _android/app/build.gradle_ file is at least 21. For IOs, you only have to make a change if you compile for a version below 10.0. The instructions with those changes are listed [here](https://pub.dev/packages/camera#ios).

## Preview of the camera 

Before I can show the preview of the camera, I need to initialize the cameras. I do this by calling the availableCameras method. This method is provided by the camera package. This method returns the available cameras on the device running the application. Since the method is asynchronous I delay the startup of the application till they are loaded.

```dart
List<CameraDescription> cameras = [];

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  cameras = await availableCameras();
  runApp(
    const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: CameraApp(),
    ),
  );
}
```

Now I have a list of cameras. Let’s use this to create a new [CameraController](https://pub.dev/documentation/camera/latest/camera/CameraController-class.html). To create a new camera controller I need a camera and a resolution. In this example, I will use the maximum resolution and the first camera. To update the view with the preview of the camera, I have to wait till the controller is initialized, to call the setState method. After this method, the Widget will update and show the current preview of the camera.

To show the current view of the camera controller, I can use the CameraPreview Widget. This Widget shows the current result of the camera. I use the SizedBox to make the CameraPreview occupy the full screen.

```dart
class CameraApp extends StatefulWidget {
  const CameraApp({Key? key}) : super(key: key);

  @override
  _CameraAppState createState() => _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
  late CameraController controller;

  @override
  void initState() {
    super.initState();
    controller = CameraController(cameras[0], ResolutionPreset.max);
    controller.initialize().then((_) {
      setState(() {});
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (!controller.value.isInitialized) {
      return Container();
    }
    return Scaffold(
      body: SizedBox(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: CameraPreview(controller),
      ),
    );
  }
}
```

## Taking a picture

Now that the application shows the current result of the view of the camera. Let’s take a picture. Before I take a picture, let’s first add a button to the Widget.

```dart
return Scaffold(
  body: SizedBox(
    width: MediaQuery.of(context).size.width,
    height: MediaQuery.of(context).size.height,
    child: CameraPreview(controller),
  ),
  floatingActionButton: FloatingActionButton(
    child: const Icon(Icons.camera),
    onPressed: () {
        // todo
    },
  ),
  floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
```

On the pressing of the button, I can take a picture. I can take a picture by calling the corresponding method on the controller. This method returns a file. This file can be used for different methods, but I provide the file to the display widget to show the result of the camera.

```dart
controller.takePicture().then((value) {
  Navigator.of(context).push(
    MaterialPageRoute(
      builder: (context) => ImageDisplayWidget(
        imagePath: value.path,
      ),
    ),
  );
});

```

The **ImageDisplayWidget** simply shows the image by displaying the file in the Image Widget.

```dart
class ImageDisplayWidget extends StatelessWidget {
  final String imagePath;

  const ImageDisplayWidget({Key? key, required this.imagePath}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Image.file(File(imagePath))),
    );
  }
}
```

This is all that is necessary to take a picture in a Flutter application! The code of this example can be found here on [Github](https://github.com/Barttje/flutter-camera-example).  If you still have any questions, comments, suggestions or other remarks feel free to let me know!

The post [Taking pictures with Flutter](https://bartvwezel.nl/flutter/taking-pictures-with-flutter/) appeared first on [Barttje](https://bartvwezel.nl).