Skip to content

Commit

Permalink
fix android crash on app lifecycle
Browse files Browse the repository at this point in the history
expose camera controller
bump deps
  • Loading branch information
jaumard committed Feb 12, 2020
1 parent f71dece commit 54cb5e8
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 51 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [2.2.5] - 12/02/2020

- fix crash on android app lifecycle
- expose camera controller
- pass default resolution to high to improve reading of barcode
- update deps

## [2.2.4] - 04/11/2019

- simplify pause stream when not on screen by using VisibilityDetector widget
Expand Down
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
/ios/GoogleService-Info.plist
/.flutter-plugins-dependencies
3 changes: 2 additions & 1 deletion example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableJetifier=true
android.useAndroidX=true
android.useAndroidX=true
android.enableR8=true
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ DEPENDENCIES:
- path_provider (from `.symlinks/plugins/path_provider/ios`)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
https://github.com/CocoaPods/Specs.git:
- Firebase
- FirebaseAnalytics
- FirebaseCore
Expand Down Expand Up @@ -145,7 +145,7 @@ SPEC CHECKSUMS:
camera: 38cc83ae9a5667bb5a71c7d9edaf60a91920fd4e
device_info: cbf09d2ec12aa7110e0b09fabe54b5bd6c8efe74
Firebase: fa80b9d987ca014a1ba9357496ef2a0178b28b12
firebase_ml_vision: d09b1eb3c242cf8124db4d51c0927376e3dbd9f4
firebase_ml_vision: a40524ef47717f380239e19567c9dc050d8cc1de
FirebaseAnalytics: 1743c5f4de3687d0745709dfdc4b1dea1484f44c
FirebaseCore: e38f025287b413255a53acc1945d048a112047f7
FirebaseInstanceID: 0e0348a3c00a734fa376a070f5ad4533ad975cb5
Expand All @@ -165,4 +165,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 389c8036e38c7090e87c697e550157f4e57648d9

COCOAPODS: 1.6.0
COCOAPODS: 1.8.4
14 changes: 8 additions & 6 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
/* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
2409E7A613325A687028AE0F /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
Expand All @@ -57,6 +58,8 @@
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BD05840A228E98DD00D7684A /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
CE99F0092DC98CB10C7128A7 /* libPods-Runner.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Runner.a"; sourceTree = BUILT_PRODUCTS_DIR; };
F51EA479259F9D93D435315C /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
FDC4259C7A63125CD0C3D54C /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -76,6 +79,9 @@
5BABECA882D889E73F189ACB /* Pods */ = {
isa = PBXGroup;
children = (
FDC4259C7A63125CD0C3D54C /* Pods-Runner.debug.xcconfig */,
2409E7A613325A687028AE0F /* Pods-Runner.release.xcconfig */,
F51EA479259F9D93D435315C /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
Expand Down Expand Up @@ -239,21 +245,17 @@
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
);
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
9740EEB61CF901F6004384FC /* Run Script */ = {
Expand Down
73 changes: 61 additions & 12 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,7 +35,7 @@ packages:
name: camera
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.6+3"
version: "0.5.7+3"
charcode:
dependency: transitive
description:
Expand All @@ -36,6 +50,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
csslib:
dependency: transitive
description:
Expand All @@ -49,21 +77,21 @@ packages:
name: cupertino_icons
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.2"
version: "0.1.3"
device_info:
dependency: transitive
description:
name: device_info
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.1+1"
version: "0.4.1+4"
firebase_ml_vision:
dependency: transitive
description:
name: firebase_ml_vision
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.3"
version: "0.9.3+5"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -87,28 +115,35 @@ packages:
name: flutter_widgets
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.6"
version: "0.1.11"
html:
dependency: transitive
description:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+3"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.5"
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
version: "1.1.8"
path:
dependency: transitive
description:
Expand All @@ -122,14 +157,21 @@ packages:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
version: "1.6.0"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
platform:
dependency: transitive
description:
Expand Down Expand Up @@ -190,7 +232,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.5"
version: "0.2.11"
typed_data:
dependency: transitive
description:
Expand All @@ -205,6 +247,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
sdks:
dart: ">=2.3.0 <3.0.0"
flutter: ">=1.9.1+hotfix.5 <2.0.0"
dart: ">=2.4.0 <3.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies:

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
cupertino_icons: ^0.1.3
flutter_camera_ml_vision:
path: ../

Expand Down
40 changes: 29 additions & 11 deletions lib/flutter_camera_ml_vision.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CameraMlVision<T> extends StatefulWidget {
CameraMlVisionState createState() => CameraMlVisionState<T>();
}

class CameraMlVisionState<T> extends State<CameraMlVision<T>> {
class CameraMlVisionState<T> extends State<CameraMlVision<T>> with WidgetsBindingObserver {
String _lastImage;
Key _visibilityKey = UniqueKey();
CameraController _cameraController;
Expand All @@ -72,9 +72,23 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_initialize();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// App state changed before we got the chance to initialize.
if (_cameraController == null || !_cameraController.value.isInitialized) {
return;
}
if (state == AppLifecycleState.inactive) {
_cameraController?.dispose();
} else if (state == AppLifecycleState.resumed && _isStreaming) {
_initialize();
}
}

Future<void> stop() async {
if (_cameraController != null) {
if (_lastImage != null && File(_lastImage).existsSync()) {
Expand All @@ -84,6 +98,7 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>> {
Directory tempDir = await getTemporaryDirectory();
_lastImage = '${tempDir.path}/${DateTime.now().millisecondsSinceEpoch}';
try {
await _cameraController.initialize();
await _cameraController.takePicture(_lastImage);
} on PlatformException catch (e) {
debugPrint('$e');
Expand All @@ -94,19 +109,19 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>> {
}

void _stop(bool silently) {
Future.microtask(() async {
scheduleMicrotask(() async {
if (_cameraController?.value?.isStreamingImages == true && mounted) {
await _cameraController.stopImageStream();
await _cameraController.stopImageStream().catchError((_) {});
}
});

if (silently) {
_isStreaming = false;
} else {
setState(() {
if (silently) {
_isStreaming = false;
});
}
} else {
setState(() {
_isStreaming = false;
});
}
});
}

void start() {
Expand Down Expand Up @@ -138,6 +153,8 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>> {
await _cameraController.startImageStream(_processImage);
}

CameraController get cameraController => _cameraController;

Future<void> Function(String path) get takePicture =>
_cameraController.takePicture;

Expand Down Expand Up @@ -165,11 +182,12 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>> {

return;
}
await _cameraController?.dispose();
_cameraController = CameraController(
description,
widget.resolution ??
ResolutionPreset
.low, // As the doc says, better to set low when streaming images to avoid drop frames on older devices
.high,
enableAudio: false,
);
if (!mounted) {
Expand Down

0 comments on commit 54cb5e8

Please sign in to comment.