Skip to content

Commit

Permalink
[firebase_ml_vision] Fix crash when passing contact info from barcode (
Browse files Browse the repository at this point in the history
  • Loading branch information
bparrishMines authored May 30, 2019
1 parent 66f1174 commit 60d6f7c
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 18 deletions.
4 changes: 4 additions & 0 deletions packages/firebase_ml_vision/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.0+2

* Fix crash when passing contact info from barcode.

## 0.8.0+1

* Update the sample to use the new ImageStreamListener API introduced in https://github.com/flutter/flutter/pull/32936.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.flutter.plugin.common.MethodChannel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -138,7 +139,9 @@ public void onSuccess(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {
List<Map<String, Object>> addresses = new ArrayList<>();
for (FirebaseVisionBarcode.Address address : contactInfo.getAddresses()) {
Map<String, Object> addressMap = new HashMap<>();
addressMap.put("addressLines", address.getAddressLines());
if (address.getAddressLines() != null) {
addressMap.put("addressLines", Arrays.asList(address.getAddressLines()));
}
addressMap.put("type", address.getType());

addresses.add(addressMap);
Expand Down Expand Up @@ -180,7 +183,9 @@ public void onSuccess(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {
}
typeValue.put("phones", phones);

typeValue.put("urls", contactInfo.getUrls());
if (contactInfo.getUrls() != null) {
typeValue.put("urls", Arrays.asList(contactInfo.getUrls()));
}
typeValue.put("jobTitle", contactInfo.getTitle());
typeValue.put("organization", contactInfo.getOrganization());

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/firebase_ml_vision/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ flutter:
assets:
- assets/test_face.jpg
- assets/test_barcode.jpg
- assets/test_contact_barcode.png
- assets/test_text.png
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,51 @@ void main() {

expect(barcodes.length, 1);
});

test('detectInImage contactInfo', () async {
final String tmpFilename = await _loadImage(
'assets/test_contact_barcode.png',
);

final FirebaseVisionImage visionImage =
FirebaseVisionImage.fromFilePath(
tmpFilename,
);

final BarcodeDetector detector = vision.barcodeDetector();
final List<Barcode> barcodes = await detector.detectInImage(
visionImage,
);

expect(barcodes, hasLength(1));
final BarcodeContactInfo info = barcodes[0].contactInfo;

final BarcodePersonName name = info.name;
expect(name.first, 'John');
expect(name.last, 'Doe');
expect(name.formattedName, 'John Doe');
expect(name.middle, anyOf(isNull, isEmpty));
expect(name.prefix, anyOf(isNull, isEmpty));
expect(name.pronunciation, anyOf(isNull, isEmpty));
expect(name.suffix, anyOf(isNull, isEmpty));

expect(info.jobTitle, anyOf(isNull, isEmpty));
expect(info.organization, anyOf(isNull, isEmpty));
expect(info.urls, <String>['http://www.example.com']);
expect(info.addresses, anyOf(isNull, isEmpty));

expect(info.emails, hasLength(1));
final BarcodeEmail email = info.emails[0];
expect(email.address, '[email protected]');
expect(email.body, anyOf(isNull, isEmpty));
expect(email.subject, anyOf(isNull, isEmpty));
expect(email.type, BarcodeEmailType.unknown);

expect(info.phones, hasLength(1));
final BarcodePhone phone = info.phones[0];
expect(phone.number, '555-555-5555');
expect(phone.type, BarcodePhoneType.unknown);
});
});

group('$FaceDetector', () {
Expand Down
30 changes: 15 additions & 15 deletions packages/firebase_ml_vision/ios/Classes/BarcodeDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ + (void)handleDetection:(FIRVisionImage *)image
[contact.emails enumerateObjectsUsingBlock:^(FIRVisionBarcodeEmail *_Nonnull email,
NSUInteger idx, BOOL *_Nonnull stop) {
[emails addObject:@{
@"address" : email.address,
@"body" : email.body,
@"subject" : email.subject,
@"address" : email.address ? email.address : [NSNull null],
@"body" : email.body ? email.body : [NSNull null],
@"subject" : email.subject ? email.subject : [NSNull null],
@"type" : @(email.type),
}];
}];
Expand All @@ -136,7 +136,7 @@ + (void)handleDetection:(FIRVisionImage *)image
[contact.phones enumerateObjectsUsingBlock:^(FIRVisionBarcodePhone *_Nonnull phone,
NSUInteger idx, BOOL *_Nonnull stop) {
[phones addObject:@{
@"number" : phone.number,
@"number" : phone.number ? phone.number : [NSNull null],
@"type" : @(phone.type),
}];
}];
Expand All @@ -149,19 +149,19 @@ + (void)handleDetection:(FIRVisionImage *)image
return @{
@"addresses" : addresses,
@"emails" : emails,
@"name" : @{
@"formattedName" : contact.name.formattedName,
@"first" : contact.name.first,
@"last" : contact.name.last,
@"middle" : contact.name.middle,
@"prefix" : contact.name.prefix,
@"pronunciation" : contact.name.pronounciation,
@"suffix" : contact.name.suffix,
},
@"phones" : phones,
@"urls" : urls,
@"jobTitle" : contact.jobTitle,
@"organization" : contact.organization,
@"name" : @{
@"formattedName" : contact.name.formattedName ? contact.name.formattedName : [NSNull null],
@"first" : contact.name.first ? contact.name.first : [NSNull null],
@"last" : contact.name.last ? contact.name.last : [NSNull null],
@"middle" : contact.name.middle ? contact.name.middle : [NSNull null],
@"prefix" : contact.name.prefix ? contact.name.prefix : [NSNull null],
@"pronunciation" : contact.name.pronounciation ? contact.name.pronounciation : [NSNull null],
@"suffix" : contact.name.suffix ? contact.name.suffix : [NSNull null],
},
@"jobTitle" : contact.jobTitle ? contact.jobTitle : [NSNull null],
@"organization" : contact.organization ? contact.jobTitle : [NSNull null],
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/firebase_ml_vision/lib/src/barcode_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class BarcodeDetectorOptions {
final BarcodeFormat barcodeFormats;
}

// TODO(bparrishMines): Normalize default string values. Some values return null on iOS while Android returns empty string.
/// Represents a single recognized barcode and its value.
class Barcode {
Barcode._(Map<dynamic, dynamic> _data)
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_ml_vision/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: firebase_ml_vision
description: Flutter plugin for Firebase machine learning vision services.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_ml_vision
version: 0.8.0+1
version: 0.8.0+2

dependencies:
flutter:
Expand Down

0 comments on commit 60d6f7c

Please sign in to comment.