Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
37be5c5
Fix padding issue
acoutts Jan 12, 2023
45d2c58
New approach that is more efficient
acoutts Jan 13, 2023
d2492c8
Refactored image reader to enable testing
acoutts Jan 13, 2023
bd55965
Added tests for image stream reader
acoutts Jan 17, 2023
88079a9
Added tests for buffer trimming
acoutts Jan 17, 2023
fee7979
Formatter
acoutts Jan 17, 2023
f0f0640
Bump pubspec version
acoutts Jan 17, 2023
df99b02
Optimize imports
acoutts Jan 17, 2023
90b2993
Cleanup
acoutts Jan 17, 2023
f5c7f8f
Imports cleanup
acoutts Jan 17, 2023
fd858eb
Imports
acoutts Jan 17, 2023
b1f292e
Imports
acoutts Jan 17, 2023
e6e1f58
Update Camera.java
acoutts Jan 17, 2023
800878f
Update CameraRegionUtils.java
acoutts Jan 17, 2023
56995bb
Update ImageStreamReader.java
acoutts Jan 17, 2023
5efd3e6
Format imports on tests
acoutts Jan 17, 2023
6667183
Formatters
acoutts Jan 18, 2023
b7dec7f
Licenses
acoutts Jan 18, 2023
0b8d812
Merge remote-tracking branch 'upstream/main' into fix-handle-padding
acoutts Jan 18, 2023
ce2a3b9
Added support for NV21 streaming on android as a new image format
acoutts Jan 20, 2023
404a954
Remove unused parameter
acoutts Jan 20, 2023
3ec602e
Add NV21 type conversion
acoutts Jan 20, 2023
a010585
Add legacy code path for nv21 to camera package
acoutts Jan 22, 2023
8bd8710
Clean up log messages
acoutts Feb 17, 2023
7b7708b
Formatters
acoutts Feb 17, 2023
d18a997
Dependency overrides as per contributing guidelines
acoutts Feb 17, 2023
37fa30d
Fix tests for image stream reader
acoutts Feb 17, 2023
c4c1719
Test cleanup
acoutts Feb 17, 2023
bf18e06
Added tests for removing padding
acoutts Feb 17, 2023
628a16a
Formatter
acoutts Feb 17, 2023
0ddc5d4
Merge remote-tracking branch 'upstream/main' into fix-handle-padding
acoutts Feb 17, 2023
651d202
Update CHANGELOG.md
acoutts Feb 17, 2023
cbfb447
Changelogs
acoutts Feb 17, 2023
1d1ecbf
Update CHANGELOG.md
acoutts Feb 17, 2023
804ddc7
Update ImageStreamReader.java
acoutts Feb 17, 2023
5a10847
Comments
acoutts Feb 17, 2023
198c55d
comments
acoutts Feb 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored image reader to enable testing
  • Loading branch information
acoutts committed Jan 13, 2023
commit d2492c86feb87e424395112380a4bf3976dae75d
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import io.flutter.plugins.camera.features.sensororientation.DeviceOrientationManager;
import io.flutter.plugins.camera.features.sensororientation.SensorOrientationFeature;
import io.flutter.plugins.camera.features.zoomlevel.ZoomLevelFeature;
import io.flutter.plugins.camera.media.ImageStreamReader;
import io.flutter.plugins.camera.media.MediaRecorderBuilder;
import io.flutter.plugins.camera.types.CameraCaptureProperties;
import io.flutter.plugins.camera.types.CaptureTimeoutsWrapper;
Expand Down Expand Up @@ -136,7 +137,7 @@ class Camera
private CameraDeviceWrapper cameraDevice;
private CameraCaptureSession captureSession;
private ImageReader pictureImageReader;
private ImageReader imageStreamReader;
private ImageStreamReader imageStreamReader;
/** {@link CaptureRequest.Builder} for the camera preview */
private CaptureRequest.Builder previewRequestBuilder;

Expand Down Expand Up @@ -304,12 +305,11 @@ public void open(String imageFormatGroup) throws CameraAccessException {
Log.w(TAG, "The selected imageFormatGroup is not supported by Android. Defaulting to yuv420");
imageFormat = ImageFormat.YUV_420_888;
}
imageStreamReader =
ImageReader.newInstance(
imageStreamReader = new ImageStreamReader(
resolutionFeature.getPreviewSize().getWidth(),
resolutionFeature.getPreviewSize().getHeight(),
imageFormat,
1);
imageFormat
);

// Open the camera.
CameraManager cameraManager = CameraUtils.getCameraManager(activity);
Expand Down Expand Up @@ -1141,79 +1141,13 @@ public void onListen(Object o, EventChannel.EventSink imageStreamSink) {

@Override
public void onCancel(Object o) {
imageStreamReader.setOnImageAvailableListener(null, backgroundHandler);
imageStreamReader.removeListener(backgroundHandler);
}
});
}

private void setImageStreamImageAvailableListener(final EventChannel.EventSink imageStreamSink) {
imageStreamReader.setOnImageAvailableListener(
reader -> {
Image img = reader.acquireNextImage();

// Use acquireNextImage since image reader is only for one image.
if (img == null) return;


List<Map<String, Object>> planes = new ArrayList<>();
for (int i=0; i<img.getPlanes().length; i++) {
// Current plane
Image.Plane plane = img.getPlanes()[i];

// The metadata to be returned to dart
Map<String, Object> planeBuffer = new HashMap<>();
planeBuffer.put("bytesPerPixel", plane.getPixelStride());

// Sometimes YUV420 has additional padding that must be removed. This is only the case if we are
// streaming YUV420, the row stride does not match the image width, and the pixel stride is 1.
if (reader.getImageFormat() == ImageFormat.YUV_420_888 &&
plane.getRowStride() != img.getWidth() &&
plane.getPixelStride() == 1) {
// The ordering of planes is guaranteed by Android. It always goes Y, U, V.
int planeWidth;
int planeHeight;
if (i == 0) {
// Y is the image size
planeWidth = img.getWidth();
planeHeight = img.getHeight();
} else {
// U and V are guaranteed to be the same size and are half of the image height/width
// in YUV420
planeWidth = img.getWidth() / 2;
planeHeight = img.getHeight() / 2;
}

planeBuffer.put("bytes", removePlaneBufferPadding(plane, planeWidth, planeHeight));

// Make sure the bytesPerRow matches the image width now that we've removed the padding
planeBuffer.put("bytesPerRow", img.getWidth());
} else {
// Just use the data as-is
ByteBuffer buffer = plane.getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes, 0, bytes.length);
planeBuffer.put("bytes", bytes);
planeBuffer.put("bytesPerRow", plane.getRowStride());
}
planes.add(planeBuffer);
}

Map<String, Object> imageBuffer = new HashMap<>();
imageBuffer.put("width", img.getWidth());
imageBuffer.put("height", img.getHeight());
imageBuffer.put("format", img.getFormat());
imageBuffer.put("planes", planes);
imageBuffer.put("lensAperture", this.captureProps.getLastLensAperture());
imageBuffer.put("sensorExposureTime", this.captureProps.getLastSensorExposureTime());
Integer sensorSensitivity = this.captureProps.getLastSensorSensitivity();
imageBuffer.put(
"sensorSensitivity", sensorSensitivity == null ? null : (double) sensorSensitivity);

final Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> imageStreamSink.success(imageBuffer));
img.close();
},
backgroundHandler);
imageStreamReader.subscribeListener(this.captureProps, imageStreamSink, backgroundHandler);
}

// Copyright (c) 2019 Dmitry Gordin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package io.flutter.plugins.camera.media;

import android.graphics.ImageFormat;
import android.media.Image;
import android.media.ImageReader;
import android.os.Handler;
import android.os.Looper;
import android.view.Surface;

import androidx.annotation.NonNull;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.EventChannel;
import io.flutter.plugins.camera.types.CameraCaptureProperties;

// Wraps an ImageReader to allow for testing of the image handler.
public class ImageStreamReader {
// The actual im
private final ImageReader imageReader;

/**
* Creates a new instance of the {@link ImageStreamReader}.
*
* @param width is the integer width of the image stream (preview image)
* @param height is the integer height of the image stream (preview image)
* @param imageFormatGroup is the integer image format group, as a valid {@link ImageFormat}.
*/
public ImageStreamReader(
int width,
int height,
int imageFormatGroup
) {
this.imageReader = ImageReader.newInstance(
width,
height,
imageFormatGroup,
1
);
}

/**
* Processes a new frame (image) from the image reader, remove padding if necessary,
* and send the frame to Dart.
*
* @param image is the image which needs processed as an {@link Image}
* @param imageFormat is the image format from the image reader as an int, a valid {@link ImageFormat}
* @param captureProps is the capture props from the camera class as {@link CameraCaptureProperties}
* @param imageStreamSink is the image stream sink from dart as a dart {@link EventChannel.EventSink}
*/
private static void onImageAvailable(
@NonNull Image image,
int imageFormat,
CameraCaptureProperties captureProps,
EventChannel.EventSink imageStreamSink
) {
List<Map<String, Object>> planes = new ArrayList<>();
for (int i=0; i<image.getPlanes().length; i++) {
// Current plane
Image.Plane plane = image.getPlanes()[i];

// The metadata to be returned to dart
Map<String, Object> planeBuffer = new HashMap<>();
planeBuffer.put("bytesPerPixel", plane.getPixelStride());

// Sometimes YUV420 has additional padding that must be removed. This is only the case if we are
// streaming YUV420, the row stride does not match the image width, and the pixel stride is 1.
if (imageFormat == ImageFormat.YUV_420_888 &&
plane.getRowStride() != image.getWidth() &&
plane.getPixelStride() == 1) {
// The ordering of planes is guaranteed by Android. It always goes Y, U, V.
int planeWidth;
int planeHeight;
if (i == 0) {
// Y is the image size
planeWidth = image.getWidth();
planeHeight = image.getHeight();
} else {
// U and V are guaranteed to be the same size and are half of the image height/width
// in YUV420
planeWidth = image.getWidth() / 2;
planeHeight = image.getHeight() / 2;
}

planeBuffer.put("bytes", removePlaneBufferPadding(plane, planeWidth, planeHeight));

// Make sure the bytesPerRow matches the image width now that we've removed the padding
planeBuffer.put("bytesPerRow", image.getWidth());
} else {
// Just use the data as-is
ByteBuffer buffer = plane.getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes, 0, bytes.length);
planeBuffer.put("bytes", bytes);
planeBuffer.put("bytesPerRow", plane.getRowStride());
}
planes.add(planeBuffer);
}

Map<String, Object> imageBuffer = new HashMap<>();
imageBuffer.put("width", image.getWidth());
imageBuffer.put("height", image.getHeight());
imageBuffer.put("format", image.getFormat());
imageBuffer.put("planes", planes);
imageBuffer.put("lensAperture", captureProps.getLastLensAperture());
imageBuffer.put("sensorExposureTime", captureProps.getLastSensorExposureTime());
Integer sensorSensitivity = captureProps.getLastSensorSensitivity();
imageBuffer.put(
"sensorSensitivity", sensorSensitivity == null ? null : (double) sensorSensitivity);

final Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> imageStreamSink.success(imageBuffer));
image.close();
}

/**
* Copyright (c) 2019 Dmitry Gordin
* Based on:
* https://github.com/gordinmitya/yuv2buf/blob/master/yuv2buf/src/main/java/ru/gordinmitya/yuv2buf/Yuv.java
*
* Will remove the padding from a given image plane and return the fixed buffer.
*
* @param plane is the image plane (buffer) that will be processed as an {@link Image.Plane}
* @param planeWidth is the width of the plane as an int
* @param planeHeight is the height of the plane as an int
*/
private static byte[] removePlaneBufferPadding(Image.Plane plane, int planeWidth, int planeHeight) {
if (plane.getPixelStride() != 1) {
throw new IllegalArgumentException("it's only valid to remove padding when pixelStride == 1");
}

ByteBuffer dst = ByteBuffer.allocate(planeWidth * planeHeight);
ByteBuffer src = plane.getBuffer();
int rowStride = plane.getRowStride();
ByteBuffer row;
for (int i = 0; i < planeHeight; i++) {
row = clipBuffer(src, i * rowStride, planeWidth);
dst.put(row);
}

return dst.array();
}

/**
* Copyright (c) 2019 Dmitry Gordin
* Based on:
* https://github.com/gordinmitya/yuv2buf/blob/master/yuv2buf/src/main/java/ru/gordinmitya/yuv2buf/Yuv.java
*
* Copies part of a buffer to a new buffer, used to trim the padding.
*
* @param buffer is the source buffer to be modified as a {@link ByteBuffer}
* @param start is the starting offset to read from as an int
* @param size is the length of data to read as an int
*/
private static ByteBuffer clipBuffer(ByteBuffer buffer, int start, int size) {
ByteBuffer duplicate = buffer.duplicate();
duplicate.position(start);
duplicate.limit(start + size);
return duplicate.slice();
}

/**
* Returns the image reader surface.
*/
public Surface getSurface() {
return imageReader.getSurface();
}

/**
* Subscribes the image stream reader to handle incoming images using onImageAvailable().
*
* @param captureProps is the capture props from the camera class as {@link CameraCaptureProperties}
* @param imageStreamSink is the image stream sink from dart as {@link EventChannel.EventSink}
* @param handler is generally the background handler of the camera as {@link Handler}
*/
public void subscribeListener(
CameraCaptureProperties captureProps,
EventChannel.EventSink imageStreamSink,
Handler handler
) {
imageReader.setOnImageAvailableListener(reader -> {
Image image = reader.acquireNextImage();
if (image == null) return;

onImageAvailable(image, imageReader.getImageFormat(), captureProps, imageStreamSink);
}, handler);
}

/**
* Removes the listener from the image reader.
*
* @param handler is generally the background handler of the camera
*/
public void removeListener(Handler handler) {
imageReader.setOnImageAvailableListener(null, handler);
}

/**
* Closes the image reader.
*/
public void close() {
imageReader.close();
}
}