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
Formatter
  • Loading branch information
acoutts committed Feb 17, 2023
commit 628a16ad2736bc1461b8c88c73cd5b51d99662f1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class ImageStreamReader {
* @param imageStreamReaderUtils is an instance of {@link ImageStreamReaderUtils}
*/
@VisibleForTesting
public ImageStreamReader(ImageReader imageReader, int dartImageFormat, ImageStreamReaderUtils imageStreamReaderUtils) {
public ImageStreamReader(
ImageReader imageReader, int dartImageFormat, ImageStreamReaderUtils imageStreamReaderUtils) {
this.imageReader = imageReader;
this.dartImageFormat = dartImageFormat;
this.imageStreamReaderUtils = imageStreamReaderUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static boolean areUVPlanesNV21(@NonNull Image.Plane[] planes, int width,
* <p>https://github.com/googlesamples/mlkit/blob/master/android/vision-quickstart/app/src/main/java/com/google/mlkit/vision/demo/BitmapUtils.java
*/
private static void unpackPlane(
@NonNull Image.Plane plane, int width, int height, byte[] out, int offset, int pixelStride)
@NonNull Image.Plane plane, int width, int height, byte[] out, int offset, int pixelStride)
throws IllegalStateException {
ByteBuffer buffer = plane.getBuffer();
buffer.rewind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugins.camera.types.CameraCaptureProperties;
import java.nio.ByteBuffer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ImageStreamReaderTest {
/**
* If we request YUV42 we should stream in YUV420.
*/
/** If we request YUV42 we should stream in YUV420. */
@Test
public void computeStreamImageFormat_computesCorrectStreamFormatYuv() {
int requestedStreamFormat = ImageFormat.YUV_420_888;
Expand All @@ -36,8 +33,8 @@ public void computeStreamImageFormat_computesCorrectStreamFormatYuv() {
}

/**
* When we want to stream in NV21, we should still request YUV420 from the
* camera because we will convert it to NV21 before sending it to dart.
* When we want to stream in NV21, we should still request YUV420 from the camera because we will
* convert it to NV21 before sending it to dart.
*/
@Test
public void computeStreamImageFormat_computesCorrectStreamFormatNv21() {
Expand All @@ -47,9 +44,8 @@ public void computeStreamImageFormat_computesCorrectStreamFormatNv21() {
}

/**
* If we are requesting NV21, then the planes should be processed and
* converted to NV21 before being sent to dart. We make sure yuv420ThreePlanesToNV21
* is called when we are requesting
* If we are requesting NV21, then the planes should be processed and converted to NV21 before
* being sent to dart. We make sure yuv420ThreePlanesToNV21 is called when we are requesting
*/
@Test
public void onImageAvailable_parsesPlanesForNv21() {
Expand All @@ -59,10 +55,11 @@ public void onImageAvailable_parsesPlanesForNv21() {
ImageReader mockImageReader = mock(ImageReader.class);
ImageStreamReaderUtils mockImageStreamReaderUtils = mock(ImageStreamReaderUtils.class);
ImageStreamReader imageStreamReader =
new ImageStreamReader(mockImageReader, dartImageFormat, mockImageStreamReaderUtils);
new ImageStreamReader(mockImageReader, dartImageFormat, mockImageStreamReaderUtils);

ByteBuffer mockBytes = ByteBuffer.allocate(0);
when(mockImageStreamReaderUtils.yuv420ThreePlanesToNV21(any(), anyInt(), anyInt())).thenReturn(mockBytes);
when(mockImageStreamReaderUtils.yuv420ThreePlanesToNV21(any(), anyInt(), anyInt()))
.thenReturn(mockBytes);

// The image format as streamed from the camera
int imageFormat = ImageFormat.YUV_420_888;
Expand Down Expand Up @@ -107,9 +104,7 @@ public void onImageAvailable_parsesPlanesForNv21() {
verify(mockImageStreamReaderUtils).yuv420ThreePlanesToNV21(any(), anyInt(), anyInt());
}

/**
* If we are requesting YUV420, then we should send the 3-plane image as it is.
*/
/** If we are requesting YUV420, then we should send the 3-plane image as it is. */
@Test
public void onImageAvailable_parsesPlanesForYuv420() {
// Dart wants NV21 frames
Expand All @@ -118,10 +113,11 @@ public void onImageAvailable_parsesPlanesForYuv420() {
ImageReader mockImageReader = mock(ImageReader.class);
ImageStreamReaderUtils mockImageStreamReaderUtils = mock(ImageStreamReaderUtils.class);
ImageStreamReader imageStreamReader =
new ImageStreamReader(mockImageReader, dartImageFormat, mockImageStreamReaderUtils);
new ImageStreamReader(mockImageReader, dartImageFormat, mockImageStreamReaderUtils);

ByteBuffer mockBytes = ByteBuffer.allocate(0);
when(mockImageStreamReaderUtils.yuv420ThreePlanesToNV21(any(), anyInt(), anyInt())).thenReturn(mockBytes);
when(mockImageStreamReaderUtils.yuv420ThreePlanesToNV21(any(), anyInt(), anyInt()))
.thenReturn(mockBytes);

// The image format as streamed from the camera
int imageFormat = ImageFormat.YUV_420_888;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,96 @@

package io.flutter.plugins.camera.media;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.graphics.ImageFormat;
import android.media.Image;
import java.nio.ByteBuffer;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.nio.ByteBuffer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(RobolectricTestRunner.class)
public class ImageStreamReaderUtilsTest {
private ImageStreamReaderUtils imageStreamReaderUtils;

@Before
public void setUp() {
this.imageStreamReaderUtils = new ImageStreamReaderUtils();
}

Image getImage(int imageWidth, int imageHeight, int padding) {
int rowStride =imageWidth + padding;

int ySize = (rowStride * imageHeight) - padding;
int uSize = (ySize / 2) - (padding / 2);
int vSize = uSize;

// Mock YUV image
Image mockImage = mock(Image.class);
when(mockImage.getWidth()).thenReturn(imageWidth);
when(mockImage.getHeight()).thenReturn(imageHeight);
when(mockImage.getFormat()).thenReturn(ImageFormat.YUV_420_888);



// Mock planes. YUV images have 3 planes (Y, U, V).
Image.Plane planeY = mock(Image.Plane.class);
Image.Plane planeU = mock(Image.Plane.class);
Image.Plane planeV = mock(Image.Plane.class);

// Y plane is width*height
// Row stride is generally == width but when there is padding it will
// be larger.
// Here we are adding 256 padding.
when(planeY.getBuffer()).thenReturn(ByteBuffer.allocate(ySize));
when(planeY.getRowStride()).thenReturn(rowStride);
when(planeY.getPixelStride()).thenReturn(1);

// U and V planes are always the same sizes/values.
// https://developer.android.com/reference/android/graphics/ImageFormat#YUV_420_888
when(planeU.getBuffer()).thenReturn(ByteBuffer.allocate(uSize));
when(planeV.getBuffer()).thenReturn(ByteBuffer.allocate(vSize));
when(planeU.getRowStride()).thenReturn(rowStride);
when(planeV.getRowStride()).thenReturn(rowStride);
when(planeU.getPixelStride()).thenReturn(2);
when(planeV.getPixelStride()).thenReturn(2);

// Add planes to image
Image.Plane[] planes = {planeY, planeU, planeV};
when(mockImage.getPlanes()).thenReturn(planes);

return mockImage;
}

/**
* Ensure that passing in an image with padding returns one without padding
*/
@Test
public void yuv420ThreePlanesToNV21_trimsPaddingWhenPresent() {
Image mockImage = getImage(640, 480, 256);
int imageWidth = mockImage.getWidth();
int imageHeight = mockImage.getHeight();

ByteBuffer result = imageStreamReaderUtils.yuv420ThreePlanesToNV21(mockImage.getPlanes(), mockImage.getWidth(), mockImage.getHeight());
Assert.assertEquals(((long) imageWidth * imageHeight) + (2 * ((long) (imageWidth / 2) * (imageHeight / 2))), result.limit());
}

/**
* Ensure that passing in an image without padding returns the same size
*/
@Test
public void yuv420ThreePlanesToNV21_trimsPaddingWhenAbsent() {
Image mockImage = getImage(640, 480, 0);
int imageWidth = mockImage.getWidth();
int imageHeight = mockImage.getHeight();

ByteBuffer result = imageStreamReaderUtils.yuv420ThreePlanesToNV21(mockImage.getPlanes(), mockImage.getWidth(), mockImage.getHeight());
Assert.assertEquals(((long) imageWidth * imageHeight) + (2 * ((long) (imageWidth / 2) * (imageHeight / 2))), result.limit());
}
private ImageStreamReaderUtils imageStreamReaderUtils;

@Before
public void setUp() {
this.imageStreamReaderUtils = new ImageStreamReaderUtils();
}

Image getImage(int imageWidth, int imageHeight, int padding) {
int rowStride = imageWidth + padding;

int ySize = (rowStride * imageHeight) - padding;
int uSize = (ySize / 2) - (padding / 2);
int vSize = uSize;

// Mock YUV image
Image mockImage = mock(Image.class);
when(mockImage.getWidth()).thenReturn(imageWidth);
when(mockImage.getHeight()).thenReturn(imageHeight);
when(mockImage.getFormat()).thenReturn(ImageFormat.YUV_420_888);

// Mock planes. YUV images have 3 planes (Y, U, V).
Image.Plane planeY = mock(Image.Plane.class);
Image.Plane planeU = mock(Image.Plane.class);
Image.Plane planeV = mock(Image.Plane.class);

// Y plane is width*height
// Row stride is generally == width but when there is padding it will
// be larger.
// Here we are adding 256 padding.
when(planeY.getBuffer()).thenReturn(ByteBuffer.allocate(ySize));
when(planeY.getRowStride()).thenReturn(rowStride);
when(planeY.getPixelStride()).thenReturn(1);

// U and V planes are always the same sizes/values.
// https://developer.android.com/reference/android/graphics/ImageFormat#YUV_420_888
when(planeU.getBuffer()).thenReturn(ByteBuffer.allocate(uSize));
when(planeV.getBuffer()).thenReturn(ByteBuffer.allocate(vSize));
when(planeU.getRowStride()).thenReturn(rowStride);
when(planeV.getRowStride()).thenReturn(rowStride);
when(planeU.getPixelStride()).thenReturn(2);
when(planeV.getPixelStride()).thenReturn(2);

// Add planes to image
Image.Plane[] planes = {planeY, planeU, planeV};
when(mockImage.getPlanes()).thenReturn(planes);

return mockImage;
}

/** Ensure that passing in an image with padding returns one without padding */
@Test
public void yuv420ThreePlanesToNV21_trimsPaddingWhenPresent() {
Image mockImage = getImage(640, 480, 256);
int imageWidth = mockImage.getWidth();
int imageHeight = mockImage.getHeight();

ByteBuffer result =
imageStreamReaderUtils.yuv420ThreePlanesToNV21(
mockImage.getPlanes(), mockImage.getWidth(), mockImage.getHeight());
Assert.assertEquals(
((long) imageWidth * imageHeight) + (2 * ((long) (imageWidth / 2) * (imageHeight / 2))),
result.limit());
}

/** Ensure that passing in an image without padding returns the same size */
@Test
public void yuv420ThreePlanesToNV21_trimsPaddingWhenAbsent() {
Image mockImage = getImage(640, 480, 0);
int imageWidth = mockImage.getWidth();
int imageHeight = mockImage.getHeight();

ByteBuffer result =
imageStreamReaderUtils.yuv420ThreePlanesToNV21(
mockImage.getPlanes(), mockImage.getWidth(), mockImage.getHeight());
Assert.assertEquals(
((long) imageWidth * imageHeight) + (2 * ((long) (imageWidth / 2) * (imageHeight / 2))),
result.limit());
}
}