camera_controller.dart
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'camera_args.dart';
import 'camera_facing.dart';
import 'objects/barcode.dart';
import 'torch_state.dart';
import 'util.dart';
/// A camera controller.
abstract class CameraController {
/// Arguments for [CameraView].
ValueNotifier<CameraArgs?> get args;
/// Torch state of the camera.
ValueNotifier<TorchState> get torchState;
/// A stream of barcodes.
Stream<Barcode> get barcodes;
/// Create a [CameraController].
///
/// [facing] target facing used to select camera.
///
/// [formats] the barcode formats for image analyzer.
factory CameraController([CameraFacing facing = CameraFacing.back]) =>
_CameraController(facing);
/// Start the camera asynchronously.
Future<void> startAsync();
/// Switch the torch's state.
void torch();
/// Release the resources of the camera.
void dispose();
}
class _CameraController implements CameraController {
static const MethodChannel method =
MethodChannel('dev.steenbakker.mobile_scanner/scanner/method');
static const EventChannel event =
EventChannel('dev.steenbakker.mobile_scanner/scanner/event');
static const undetermined = 0;
static const authorized = 1;
static const denied = 2;
static const analyze_none = 0;
static const analyze_barcode = 1;
static int? id;
static StreamSubscription? subscription;
final CameraFacing facing;
@override
final ValueNotifier<CameraArgs?> args;
@override
final ValueNotifier<TorchState> torchState;
bool torchable;
late StreamController<Barcode> barcodesController;
@override
Stream<Barcode> get barcodes => barcodesController.stream;
_CameraController(this.facing)
: args = ValueNotifier(null),
torchState = ValueNotifier(TorchState.off),
torchable = false {
// In case new instance before dispose.
if (id != null) {
stop();
}
id = hashCode;
// Create barcode stream controller.
barcodesController = StreamController.broadcast(
onListen: () => tryAnalyze(analyze_barcode),
onCancel: () => tryAnalyze(analyze_none),
);
startAsync();
// Listen event handler.
subscription =
event.receiveBroadcastStream().listen((data) => handleEvent(data));
}
void handleEvent(Map<dynamic, dynamic> event) {
final name = event['name'];
final data = event['data'];
switch (name) {
case 'torchState':
final state = TorchState.values[data];
torchState.value = state;
break;
case 'barcode':
final barcode = Barcode.fromNative(data);
barcodesController.add(barcode);
break;
default:
throw UnimplementedError();
}
}
void tryAnalyze(int mode) {
if (hashCode != id) {
return;
}
method.invokeMethod('analyze', mode);
}
@override
Future<void> startAsync() async {
ensure('startAsync');
// Check authorization state.
var state = await method.invokeMethod('state');
if (state == undetermined) {
final result = await method.invokeMethod('request');
state = result ? authorized : denied;
}
if (state != authorized) {
throw PlatformException(code: 'NO ACCESS');
}
// Start camera.
final answer =
await method.invokeMapMethod<String, dynamic>('start', facing.index);
final textureId = answer?['textureId'];
final size = toSize(answer?['size']);
args.value = CameraArgs(textureId, size);
torchable = answer?['torchable'];
}
@override
void torch() {
ensure('torch');
if (!torchable) {
return;
}
var state =
torchState.value == TorchState.off ? TorchState.on : TorchState.off;
method.invokeMethod('torch', state.index);
}
@override
void dispose() {
if (hashCode == id) {
stop();
subscription?.cancel();
subscription = null;
id = null;
}
barcodesController.close();
}
void stop() => method.invokeMethod('stop');
void ensure(String name) {
final message =
'CameraController.$name called after CameraController.dispose\n'
'CameraController methods should not be used after calling dispose.';
assert(hashCode == id, message);
}
}