Skip to content

Commit bafa737

Browse files
committed
updates
1 parent 1949fe4 commit bafa737

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
<dict>
55
<key>BuildSystemType</key>
66
<string>Original</string>
7+
<key>PreviewsEnabled</key>
8+
<false/>
79
</dict>
810
</plist>

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class _MyHomePageState extends State<_MyHomePage> {
2828
if (!mounted) {
2929
return;
3030
}
31+
_vision.addBarcodeDetector().forEach((action) => print(action));
3132
setState(() {});
3233
});
3334
}

ios/Classes/FirebaseMlVisionPlugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#import "Firebase/Firebase.h"
44

5-
@interface FLTFirebaseMlVisionPlugin : NSObject <FlutterPlugin>
5+
@interface FLTFirebaseMlVisionPlugin : NSObject <FlutterPlugin, FlutterStreamHandler>
66
+ (void)handleError:(NSError *)error result:(FlutterResult)result;
77
@end
88

ios/Classes/FirebaseMlVisionPlugin.m

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ - (void)setCaptureSessionPreset:(NSString *)resolutionPreset {
139139
- (void)captureOutput:(AVCaptureOutput *)output
140140
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
141141
fromConnection:(AVCaptureConnection *)connection {
142+
CVImageBufferRef newBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
142143
if (output == _captureVideoOutput) {
143144
CVPixelBufferRef newBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
144145
CFRetain(newBuffer);
@@ -210,19 +211,22 @@ @implementation FLTFirebaseMlVisionPlugin {
210211
}
211212

212213
static NSMutableDictionary<NSNumber *, id<Detector>> *detectors;
214+
FlutterEventSink resultSink;
213215

214216
+ (void)handleError:(NSError *)error result:(FlutterResult)result {
215217
result(getFlutterError(error));
216218
}
217219

218220
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
219221
detectors = [NSMutableDictionary new];
222+
FlutterEventChannel *results = [FlutterEventChannel eventChannelWithName:@"plugins.flutter.io/firebase_mlvision_results" binaryMessenger:[registrar messenger]];
220223
FlutterMethodChannel *channel =
221224
[FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/firebase_mlvision"
222225
binaryMessenger:[registrar messenger]];
223226
FLTFirebaseMlVisionPlugin *instance = [[FLTFirebaseMlVisionPlugin alloc] initWithRegistry:[registrar textures]
224227
messenger:[registrar messenger]];
225228
[registrar addMethodCallDelegate:instance channel:channel];
229+
[results setStreamHandler:instance];
226230

227231
SEL sel = NSSelectorFromString(@"registerLibrary:withVersion:");
228232
if ([FIRApp respondsToSelector:sel]) {
@@ -258,6 +262,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
258262

259263
- (void)handleMethodCallAsync:(FlutterMethodCall *)call result:(FlutterResult)result {
260264
NSString *modelName = call.arguments[@"model"];
265+
NSDictionary *options = call.arguments[@"options"];
266+
NSNumber *handle = call.arguments[@"handle"];
261267
if ([@"ModelManager#setupLocalModel" isEqualToString:call.method]) {
262268
[SetupLocalModel modelName:modelName result:result];
263269
} else if ([@"ModelManager#setupRemoteModel" isEqualToString:call.method]) {
@@ -307,7 +313,7 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call result:(FlutterResult)re
307313
int64_t textureId = [_registry registerTexture:cam];
308314
_camera = cam;
309315
cam.onFrameAvailable = ^{
310-
[_registry textureFrameAvailable:textureId];
316+
[self->_registry textureFrameAvailable:textureId];
311317
};
312318
FlutterEventChannel *eventChannel = [FlutterEventChannel
313319
eventChannelWithName:[NSString
@@ -323,6 +329,12 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call result:(FlutterResult)re
323329
});
324330
[cam start];
325331
}
332+
} else if ([@"BarcodeDetector#startDetection" isEqualToString:call.method]){
333+
id<Detector> detector = detectors[handle];
334+
if (!detector) {
335+
detector = [[BarcodeDetector alloc] initWithVision:[FIRVision vision] options:options];
336+
[FLTFirebaseMlVisionPlugin addDetector:handle detector:detector];
337+
}
326338
} else if ([@"BarcodeDetector#detectInImage" isEqualToString:call.method] ||
327339
[@"FaceDetector#processImage" isEqualToString:call.method] ||
328340
[@"ImageLabeler#processImage" isEqualToString:call.method] ||
@@ -521,4 +533,14 @@ + (void)addDetector:(NSNumber *)handle detector:(id<Detector>)detector {
521533
detectors[handle] = detector;
522534
}
523535

536+
- (FlutterError * _Nullable)onCancelWithArguments:(id _Nullable)arguments {
537+
resultSink = nil;
538+
return nil;
539+
}
540+
541+
- (FlutterError * _Nullable)onListenWithArguments:(id _Nullable)arguments eventSink:(nonnull FlutterEventSink)events {
542+
resultSink = events;
543+
return nil;
544+
}
545+
524546
@end

lib/src/barcode_detector.dart

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,24 @@ class BarcodeDetector {
192192
bool _isClosed = false;
193193

194194
/// Detects barcodes in the input image.
195-
Future<List<Barcode>> detectInImage(FirebaseVisionImage visionImage) async {
195+
Stream<List<Barcode>> startDetection() {
196196
assert(!_isClosed);
197197

198198
_hasBeenOpened = true;
199-
final List<dynamic> reply =
200-
await FirebaseVision.channel.invokeListMethod<dynamic>(
201-
'BarcodeDetector#detectInImage',
199+
Stream<dynamic> data = Stream.empty();
200+
FirebaseVision.channel.invokeListMethod<dynamic>(
201+
'BarcodeDetector#startDetection',
202202
<String, dynamic>{
203203
'handle': _handle,
204204
'options': <String, dynamic>{
205205
'barcodeFormats': options.barcodeFormats.value,
206206
},
207-
}..addAll(visionImage._serialize()),
208-
);
209-
210-
final List<Barcode> barcodes = <Barcode>[];
211-
reply.forEach((dynamic barcode) {
212-
barcodes.add(Barcode._(barcode));
207+
},
208+
).then((onValue){
209+
const EventChannel resultsChannel = EventChannel('plugins.flutter.io/firebase_mlvision_results');
210+
data = resultsChannel.receiveBroadcastStream();
213211
});
214-
215-
return barcodes;
212+
return data;
216213
}
217214

218215
/// Release resources used by this detector.

lib/src/firebase_vision.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,11 @@ class FirebaseVision extends ValueNotifier<FirebaseCameraValue> {
284284
}
285285

286286
/// Creates an instance of [BarcodeDetector].
287-
BarcodeDetector barcodeDetector([BarcodeDetectorOptions options]) {
288-
return BarcodeDetector._(options ?? const BarcodeDetectorOptions(),
287+
Stream<List<Barcode>> addBarcodeDetector([BarcodeDetectorOptions options]) {
288+
BarcodeDetector detector = BarcodeDetector._(options ?? const BarcodeDetectorOptions(),
289289
nextHandle++,
290290
);
291+
return detector.startDetection();
291292
}
292293

293294
/// Creates an instance of [VisionEdgeImageLabeler].

0 commit comments

Comments
 (0)