This PR is a full rework of the android plugin. There were a number of issues before, including crashes on some legacy devices and some features just didn't work. I found that the camera API wasn't quite implemented correctly, and it wasn't transitioning between the states / modes as it should have. This led to instabilities on some devices.
I've tested this PR on the following devices and every feature in the plugin now works as expected:
- Pixel 4
- LG k50s
- Samsung Galaxy S7
- Sony XZ (F8331)
Summary of major changes / improvements:
- Added comments throughout the plugin detailing what each part is doing.
- Fixed capture timeout on devices, related to auto exposure / focus.
- Added correct handling for starting/stopping auto exposure, to not crash on cameras with unsupported modes.
- Added correct handling to stop/start autofocus when switching between focus lock and autofocus. Tapping the lock button now triggers a one-time autofocus of the current point/scene, and then locks it until later unlocked.
- Added support for
NV21
image streams in Android for more efficient use with Firebase ML Vision- no need to concatenate the YUV420 planes anymore, just feed the one plane from NV21 directly to ML Vision.
- Fixed flickering issue on Sony front facing cameras when capturing photos and changing AE mode.
- Moved capture callbacks / image processing to run on a background handler/thread.
- Instead of managing the camera state in
PictureCaptureRequest
, we now have a new CameraState
as well as the PictureCaptureRequestState
. The PictureCaptureRequestState
handles timing out captures while the CameraState
tracks the state of the camera as it's processing the current request. This made it easier to keep track of how the camera is moving between states during capture.
Testing this PR
In order to use this PR before the update to camera
and camera_platform_interface
are published, you need to include the following in your pubspec.yaml
:
camera:
git:
url: https://github.com/bottlepay/plugins.git
path: packages/camera/camera
ref: android-rework
Future improvements
There is still a delay when capturing images on some devices. I believe it's because we are blocking the UI thread and freezing flutter, so the image preview freezes. I think what we can do to improve this is when calling take picture from android, we need to return a completer future so we can immediately call the result function to unblock flutter, and then once the image is processed we can complete it to return the image path back to dart.
The same could be applied to starting/stopping video capture too.
Related issues:
- https://github.com/flutter/flutter/issues/76782 (potentailly)
- https://github.com/flutter/flutter/issues/76498
- https://github.com/flutter/flutter/issues/76311
- https://github.com/flutter/flutter/issues/76305
- https://github.com/flutter/flutter/issues/75641
- https://github.com/flutter/flutter/issues/75506
- https://github.com/flutter/flutter/issues/75133
- https://github.com/flutter/flutter/issues/75208
- https://github.com/flutter/flutter/issues/73561
- https://github.com/flutter/flutter/issues/73504
Pre-launch Checklist
- [x] The title of the PR starts with the name of the plugin surrounded by square brackets, e.g.
[shared_preferences]
- [x] I read the Contributor Guide and followed the process outlined there for submitting PRs.
- [x] I read the Tree Hygiene wiki page, which explains my responsibilities.
- [x] I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
- [x] I listed at least one issue that this PR fixes in the description above.
- [x] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
- [x] I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
- [x] I updated CHANGELOG.md to add a description of the change.
- [x] I updated/added relevant documentation (doc comments with
///
).
- [x] I signed the CLA.
- [x] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel on Discord.
cla: no p: camera platform-android