|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:flutter/foundation.dart'; |
| 8 | + |
| 9 | +import 'common/camera_interface.dart'; |
| 10 | + |
| 11 | +/// Controls a device camera. |
| 12 | +/// |
| 13 | +/// Use [CameraController.availableCameras] to get a list of available cameras. |
| 14 | +/// |
| 15 | +/// This class is used as a simple interface that works for Android and iOS. |
| 16 | +/// |
| 17 | +/// When using iOS, simultaneously calling [start] on two [CameraController]s |
| 18 | +/// will throw a [PlatformException]. |
| 19 | +/// |
| 20 | +/// When using Android, simultaneously calling [start] on two |
| 21 | +/// [CameraController]s may throw a [PlatformException] depending on the |
| 22 | +/// hardware resources of the device. |
| 23 | +class CameraController { |
| 24 | + /// Default constructor. |
| 25 | + /// |
| 26 | + /// Use [CameraController.availableCameras] to get a list of available |
| 27 | + /// cameras. |
| 28 | + /// |
| 29 | + /// This will choose the best [CameraConfigurator] for the current device. |
| 30 | + factory CameraController({@required CameraDescription description}) { |
| 31 | + assert(description != null); |
| 32 | + return CameraController._( |
| 33 | + description: description, |
| 34 | + configurator: _createDefaultConfigurator(description), |
| 35 | + api: _getCameraApi(description), |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + CameraController._({ |
| 40 | + @required this.description, |
| 41 | + @required this.configurator, |
| 42 | + @required this.api, |
| 43 | + }) : assert(description != null), |
| 44 | + assert(configurator != null), |
| 45 | + assert(api != null); |
| 46 | + |
| 47 | + /// Constructor for defining your own [CameraConfigurator]. |
| 48 | + /// |
| 49 | + /// Use [CameraController.availableCameras] to get a list of available |
| 50 | + /// cameras. |
| 51 | + factory CameraController.customConfigurator({ |
| 52 | + @required CameraDescription description, |
| 53 | + @required CameraConfigurator configurator, |
| 54 | + }) { |
| 55 | + return CameraController._( |
| 56 | + description: description, |
| 57 | + configurator: configurator, |
| 58 | + api: _getCameraApi(description), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /// Details for the camera this controller accesses. |
| 63 | + final CameraDescription description; |
| 64 | + |
| 65 | + /// Configurator used to control the camera. |
| 66 | + final CameraConfigurator configurator; |
| 67 | + |
| 68 | + /// Api used by the [configurator]. |
| 69 | + final CameraApi api; |
| 70 | + |
| 71 | + /// Retrieves a list of available cameras for the current device. |
| 72 | + /// |
| 73 | + /// This will choose the best [CameraAPI] for the current device. |
| 74 | + static Future<List<CameraDescription>> availableCameras() async { |
| 75 | + throw UnimplementedError('$defaultTargetPlatform not supported'); |
| 76 | + } |
| 77 | + |
| 78 | + /// Begins the flow of data between the inputs and outputs connected the camera instance. |
| 79 | + Future<void> start() => configurator.start(); |
| 80 | + |
| 81 | + /// Stops the flow of data between the inputs and outputs connected the camera instance. |
| 82 | + Future<void> stop() => configurator.stop(); |
| 83 | + |
| 84 | + /// Deallocate all resources and disables further use of the controller. |
| 85 | + Future<void> dispose() => configurator.dispose(); |
| 86 | + |
| 87 | + static CameraConfigurator _createDefaultConfigurator( |
| 88 | + CameraDescription description, |
| 89 | + ) { |
| 90 | + final CameraApi api = _getCameraApi(description); |
| 91 | + switch (api) { |
| 92 | + case CameraApi.android: |
| 93 | + throw UnimplementedError(); |
| 94 | + case CameraApi.iOS: |
| 95 | + throw UnimplementedError(); |
| 96 | + case CameraApi.supportAndroid: |
| 97 | + throw UnimplementedError(); |
| 98 | + } |
| 99 | + |
| 100 | + return null; // Unreachable code |
| 101 | + } |
| 102 | + |
| 103 | + static CameraApi _getCameraApi(CameraDescription description) { |
| 104 | + throw ArgumentError.value( |
| 105 | + description.runtimeType, |
| 106 | + 'description.runtimeType', |
| 107 | + 'Failed to get $CameraApi from', |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
0 commit comments