CodeBlanks Logo Icon CodeBlanks

Camera plugin

Build a live camera preview with the camera package.

The camera package

The camera package provides direct access to the device camera and lets you build a live preview. You initialize the camera with CameraController and display the feed with the CameraPreview widget. Unlike image_picker, it lets the user see the camera stream before capturing.

final controller = CameraController(camera, ResolutionPreset.high);
await controller.initialize();

The CameraPreview widget

CameraPreview is the widget that displays the controller's live feed on screen. It is used as CameraPreview(controller). Using this widget before the controller is initialized throws an error.

CameraPreview(controller);

Practice Initialize the controller and check it

Create a CameraController, initialize it with initialize and print controller.value.isInitialized.

Starter code

main.dart
class CameraDescription {
  final String name;
  CameraDescription(this.name);
}

enum ResolutionPreset { low, medium, high }

class CameraValue {
  final bool isInitialized;
  CameraValue(this.isInitialized);
}

class CameraController {
  final CameraDescription camera;
  final ResolutionPreset resolution;
  bool _initialized = false;
  CameraController(this.camera, this.resolution);
  CameraValue get value => CameraValue(_initialized);
  Future<void> initialize() async {
    _initialized = true;
  }
}

void main() async {
  // controller oluştur
  // initialize çağır
  // isInitialized yazdır
}

Sample solution

main.dart
class CameraDescription {
  final String name;
  CameraDescription(this.name);
}

enum ResolutionPreset { low, medium, high }

class CameraValue {
  final bool isInitialized;
  CameraValue(this.isInitialized);
}

class CameraController {
  final CameraDescription camera;
  final ResolutionPreset resolution;
  bool _initialized = false;
  CameraController(this.camera, this.resolution);
  CameraValue get value => CameraValue(_initialized);
  Future<void> initialize() async {
    _initialized = true;
  }
}

void main() async {
  final controller = CameraController(
    CameraDescription('0'),
    ResolutionPreset.high,
  );
  await controller.initialize();
  print(controller.value.isInitialized);
}

Expected output

true

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main difference between the camera package and image_picker?

  • They are completely identical
  • The camera package only picks from the gallery
  • The camera package provides a live camera preview Correct answer
  • image_picker is faster

Explanation: The camera package provides a live camera stream and preview; image_picker only opens the system picker.

2 Initialize the controller

Question
await controller.____;
Answer
await controller.initialize();

Explanation: Before using the CameraController you call the initialize method; it is asynchronous so await is required.

3 camera package parts

Term Meaning
camera Camera access package
CameraController Camera controller
CameraPreview Live preview widget
initialize Initialization method

All lessons in Advanced