-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Add byte streaming capability for the camera #965
Conversation
@@ -258,7 +278,8 @@ public boolean onRequestPermissionsResult(int id, String[] permissions, int[] gr | |||
private CameraDevice cameraDevice; | |||
private CameraCaptureSession cameraCaptureSession; | |||
private EventChannel.EventSink eventSink; | |||
private ImageReader imageReader; | |||
private ImageReader pictureImageReader; | |||
private ImageReader byteImageReader; // Used to pass bytes to dart side. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to separate readers because they use different formats.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be explained in a comment in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
- (void)captureToFile:(NSString *)filename result:(FlutterResult)result; | ||
@end | ||
|
||
@implementation FLTCam | ||
FourCharCode const videoFormat = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Format that I believe is recommended for iOS 10+, which is minimum requirement for this plugin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add this explanation as a comment for future readers to understand the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
de28941
to
4cbb8b2
Compare
@@ -269,7 +337,66 @@ - (CVPixelBufferRef)copyPixelBuffer { | |||
while (!OSAtomicCompareAndSwapPtrBarrier(pixelBuffer, nil, (void **)&_latestPixelBuffer)) { | |||
pixelBuffer = _latestPixelBuffer; | |||
} | |||
return pixelBuffer; | |||
|
|||
return [self convertYUVImageTOBGRA:pixelBuffer]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since video format was changed to kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
we have to convert image to a usable format for flutter textures. Which is kCVPixelFormatType_32BGRA
.
4cbb8b2
to
d0f173b
Compare
d0f173b
to
bdd9007
Compare
@bparrishMines |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
I would like for some of the other current plugin-maintainers to also review this.
I worry about the efficiency of transferring the data across the messageChannel.
I guess we do not have a lot of choice currently - but it seems very heavy, and not the intended use.
A future development would be to have a platform-image plugin (or image-stream plugin) that allows the images to stay on the native side, and be referenced from dart. It could then provide a number of compositions of images, and the ability to project an image into a Texture, or - if really needed - transfer the bytes to dart.
The camera-plugin would know about the image-plugin and be able to send the images there...
@@ -258,7 +278,8 @@ public boolean onRequestPermissionsResult(int id, String[] permissions, int[] gr | |||
private CameraDevice cameraDevice; | |||
private CameraCaptureSession cameraCaptureSession; | |||
private EventChannel.EventSink eventSink; | |||
private ImageReader imageReader; | |||
private ImageReader pictureImageReader; | |||
private ImageReader byteImageReader; // Used to pass bytes to dart side. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be explained in a comment in the code.
@@ -183,7 +182,7 @@ | |||
TargetAttributes = { | |||
97C146ED1CF9000F007C117D = { | |||
CreatedOnToolsVersion = 7.3.1; | |||
DevelopmentTeam = EQHXZ8M8AV; | |||
DevelopmentTeam = S8QB4VV633; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think changes to this file should be checked in.
- (void)captureToFile:(NSString *)filename result:(FlutterResult)result; | ||
@end | ||
|
||
@implementation FLTCam | ||
FourCharCode const videoFormat = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add this explanation as a comment for future readers to understand the code.
/// | ||
/// Although not all image formats are planar on iOS, we treat 1-dimensional | ||
/// images as single planar images. | ||
class CameraImage { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add a helper to make it easy to turn this into a https://docs.flutter.io/flutter/painting/ImageProvider-class.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation doesn't include a compatible image format with 'ImageProvider'. This impl uses yuv420.
I also include in CameraImage
comments that The [CameraImage] is not directly usable as a UI resource.
packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java
Outdated
Show resolved
Hide resolved
packages/camera/lib/src/camera.dart
Outdated
/// | ||
/// Throws a [CameraException] if byte streaming or video recording has | ||
/// already started. | ||
Future<void> startByteStream(OnLatestImageAvailable onAvailable) async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also take a resolution, and a fps-setting?
It seems many times you would want a lower resolution setting for the bytestream
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to a limitation I ran into on iOS. I wouldn't be able to have a different resolution for image streaming and the [CameraPreview] widget.
Although, we could scale the image on the platform side before we send it over the platform channel. I think this additional code would be best in a separate PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FPS Setting could be added as well. I added a TODO
to add these options.
Need this for barcode scanning! |
@bparrishMines Do you know when this is going to be merged? |
@bparrishMines I am getting |
Need this for mustache filters ! |
@MichaelPriebe I wasn't running into any issues for with the example camera app. What do you mean by 'open the camera the second time'. Are you creating a new |
@bparrishMines Thanks for this! I was wondering if there's a way to convert the
Alternatively if I could save the image to a file that would also be great! Thanks |
This reverts commit b40042f.
Hey guys, the image stream is not sufficient for many use cases and doesn't include audio sources by default (like live RMTP/HLS streams). Is there any possibility to get |
You could encode the camera image to an image like jpg and send the bytes as blob or encode it to base64 and send it Repo: https://pub.dev/packages/image Example code: |
No description provided.