This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Add byte streaming capability for the camera #900
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3f703e
Start of Android side of byte stream passing
bparrishMines f310580
dart side of byte streaming
bparrishMines 87fdb60
Fix android streaming
bparrishMines 673b003
Merge branch 'master' of github.com:flutter/plugins into camera_android
bparrishMines 8d353ad
Add ios byte streaming
bparrishMines 0291a29
Convert buffer to uiimage to pass over
bparrishMines 31f746a
formatting
bparrishMines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,6 +203,26 @@ public void onMethodCall(MethodCall call, final Result result) { | |
camera.stopVideoRecording(result); | ||
break; | ||
} | ||
case "startByteStream": | ||
{ | ||
try { | ||
camera.startPreviewWithByteStream(); | ||
result.success(null); | ||
} catch (CameraAccessException e) { | ||
result.error("CameraAccess", e.getMessage(), null); | ||
} | ||
break; | ||
} | ||
case "stopByteStream": | ||
{ | ||
try { | ||
camera.startPreview(); | ||
result.success(null); | ||
} catch (CameraAccessException e) { | ||
result.error("CameraAccess", e.getMessage(), null); | ||
} | ||
break; | ||
} | ||
case "dispose": | ||
{ | ||
if (camera != null) { | ||
|
@@ -248,7 +268,8 @@ private class Camera { | |
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. | ||
private int sensorOrientation; | ||
private boolean isFrontFacing; | ||
private String cameraName; | ||
|
@@ -453,9 +474,13 @@ private void open(@Nullable final Result result) { | |
if (result != null) result.error("cameraPermission", "Camera permission not granted", null); | ||
} else { | ||
try { | ||
imageReader = | ||
pictureImageReader = | ||
ImageReader.newInstance( | ||
captureSize.getWidth(), captureSize.getHeight(), ImageFormat.JPEG, 2); | ||
byteImageReader = | ||
ImageReader.newInstance( | ||
previewSize.getWidth(), previewSize.getHeight(), ImageFormat.YUV_420_888, 2); | ||
|
||
cameraManager.openCamera( | ||
cameraName, | ||
new CameraDevice.StateCallback() { | ||
|
@@ -548,7 +573,7 @@ private void takePicture(String filePath, @NonNull final Result result) { | |
return; | ||
} | ||
|
||
imageReader.setOnImageAvailableListener( | ||
pictureImageReader.setOnImageAvailableListener( | ||
new ImageReader.OnImageAvailableListener() { | ||
@Override | ||
public void onImageAvailable(ImageReader reader) { | ||
|
@@ -566,7 +591,7 @@ public void onImageAvailable(ImageReader reader) { | |
try { | ||
final CaptureRequest.Builder captureBuilder = | ||
cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); | ||
captureBuilder.addTarget(imageReader.getSurface()); | ||
captureBuilder.addTarget(pictureImageReader.getSurface()); | ||
int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); | ||
int displayOrientation = ORIENTATIONS.get(displayRotation); | ||
if (isFrontFacing) displayOrientation = -displayOrientation; | ||
|
@@ -696,7 +721,7 @@ private void startPreview() throws CameraAccessException { | |
surfaces.add(previewSurface); | ||
captureRequestBuilder.addTarget(previewSurface); | ||
|
||
surfaces.add(imageReader.getSurface()); | ||
surfaces.add(pictureImageReader.getSurface()); | ||
|
||
cameraDevice.createCaptureSession( | ||
surfaces, | ||
|
@@ -726,6 +751,107 @@ public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession | |
null); | ||
} | ||
|
||
private void startPreviewWithByteStream() throws CameraAccessException { | ||
closeCaptureSession(); | ||
|
||
SurfaceTexture surfaceTexture = textureEntry.surfaceTexture(); | ||
surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight()); | ||
|
||
captureRequestBuilder = | ||
cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); | ||
|
||
List<Surface> surfaces = new ArrayList<>(); | ||
|
||
Surface previewSurface = new Surface(surfaceTexture); | ||
surfaces.add(previewSurface); | ||
captureRequestBuilder.addTarget(previewSurface); | ||
|
||
surfaces.add(byteImageReader.getSurface()); | ||
captureRequestBuilder.addTarget(byteImageReader.getSurface()); | ||
|
||
cameraDevice.createCaptureSession( | ||
surfaces, | ||
new CameraCaptureSession.StateCallback() { | ||
@Override | ||
public void onConfigured(@NonNull CameraCaptureSession session) { | ||
if (cameraDevice == null) { | ||
sendErrorEvent("The camera was closed during configuration."); | ||
return; | ||
} | ||
try { | ||
cameraCaptureSession = session; | ||
captureRequestBuilder.set( | ||
CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); | ||
cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null); | ||
} catch (CameraAccessException e) { | ||
sendErrorEvent(e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { | ||
sendErrorEvent("Failed to configure the camera for streaming bytes."); | ||
} | ||
}, | ||
null); | ||
|
||
registerByteStreamEventChannel(); | ||
} | ||
|
||
private void registerByteStreamEventChannel() { | ||
final EventChannel cameraChannel = | ||
new EventChannel(registrar.messenger(), "plugins.flutter.io/camera/bytes"); | ||
|
||
cameraChannel.setStreamHandler( | ||
new EventChannel.StreamHandler() { | ||
@Override | ||
public void onListen(Object o, EventChannel.EventSink eventSink) { | ||
setByteStreamImageAvailableListener(eventSink); | ||
} | ||
|
||
@Override | ||
public void onCancel(Object o) { | ||
byteImageReader.setOnImageAvailableListener(null, null); | ||
} | ||
}); | ||
} | ||
|
||
private void setByteStreamImageAvailableListener(final EventChannel.EventSink eventSink) { | ||
byteImageReader.setOnImageAvailableListener( | ||
new ImageReader.OnImageAvailableListener() { | ||
@Override | ||
public void onImageAvailable(final ImageReader reader) { | ||
Image img = reader.acquireLatestImage(); | ||
if (img == null) return; | ||
|
||
eventSink.success(YUV_420_888toNV21(img)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conversion to NV21 to turn planar image bytebuffer to a single dimension image. |
||
img.close(); | ||
} | ||
}, | ||
null); | ||
} | ||
|
||
private byte[] YUV_420_888toNV21(Image image) { | ||
byte[] nv21; | ||
|
||
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer(); | ||
ByteBuffer uBuffer = image.getPlanes()[1].getBuffer(); | ||
ByteBuffer vBuffer = image.getPlanes()[2].getBuffer(); | ||
|
||
int ySize = yBuffer.remaining(); | ||
int uSize = uBuffer.remaining(); | ||
int vSize = vBuffer.remaining(); | ||
|
||
nv21 = new byte[ySize + uSize + vSize]; | ||
|
||
//U and V are swapped | ||
yBuffer.get(nv21, 0, ySize); | ||
vBuffer.get(nv21, ySize, vSize); | ||
uBuffer.get(nv21, ySize + vSize, uSize); | ||
|
||
return nv21; | ||
} | ||
|
||
private void sendErrorEvent(String errorDescription) { | ||
if (eventSink != null) { | ||
Map<String, String> event = new HashMap<>(); | ||
|
@@ -749,9 +875,13 @@ private void close() { | |
cameraDevice.close(); | ||
cameraDevice = null; | ||
} | ||
if (imageReader != null) { | ||
imageReader.close(); | ||
imageReader = null; | ||
if (pictureImageReader != null) { | ||
pictureImageReader.close(); | ||
pictureImageReader = null; | ||
} | ||
if (byteImageReader != null) { | ||
byteImageReader.close(); | ||
byteImageReader = null; | ||
} | ||
if (mediaRecorder != null) { | ||
mediaRecorder.reset(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 use
ImageFormat.YUV_420_888
because it is commonly supported on Android devices.