OnScreenshotAvailable implements ImageReader.OnImageAvailableListener { public synchronized void onImageAvailable(ImageReader imageReader) { if (imageAlreadyProcessed) { return; } Image image = imageReader.acquireLatestImage(); Bitmap screenshotBitmap = null; try { screenshotBitmap = getBitmapFromImage(image); } catch (ImageFormatException e) { Logger.logError(TAG, "ImageFormatException: " + e.toString()); } finally { image.close(); } if (screenshotBitmap != null) { imageAlreadyProcessed = true; bitmapConsumer.accept(screenshotBitmap); } } OnScreenshotAvailable(
      Consumer<Bitmap> bitmapConsumer, DisplayMetrics metrics, BitmapProvider bitmapProvider); synchronized void onImageAvailable(ImageReader imageReader);  }
@Test public void onImageAvailableWithUnpaddedImageBufferCreatesBitmapDirectlyFromSourceBuffer() { setupMocksToCreateBitmap(); testSubject.onImageAvailable(imageReaderMock); verify(bitmapMock, times(1)).copyPixelsFromBuffer(imagePlaneStubBuffer); verify(bitmapConsumerMock, times(1)).accept(bitmapMock); }
@Test public void onImageAvailableWithPaddedImageBufferStripsPaddingBeforeCopyingPixels() { widthStub = 2; heightStub = 2; int rowPaddingBytes = 1; rowStrideStub = (pixelStrideStub * widthStub + rowPaddingBytes); imagePlaneStubBuffer = ByteBuffer.wrap(new byte[] {1, 1, 1, 1, 2, 2, 2, 2, 0, 3, 3, 3, 3, 4, 4, 4, 4, 0}); ByteBuffer bufferWithPaddingRemoved = ByteBuffer.wrap(new byte[] {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4}); setupMocksToCreateBitmap(); testSubject.onImageAvailable(imageReaderMock); verify(bitmapMock, times(1)).copyPixelsFromBuffer(eq(bufferWithPaddingRemoved)); verify(bitmapConsumerMock, times(1)).accept(bitmapMock); }
@Test public void onImageAvailableProcessesImageOnlyOnce() { setupMocksToCreateBitmap(); testSubject.onImageAvailable(imageReaderMock); reset( imageReaderMock, bitmapConsumerMock, imageMock, imagePlaneMock, bitmapProviderMock, bitmapMock); testSubject.onImageAvailable(imageReaderMock); verify(bitmapMock, times(0)).copyPixelsFromBuffer(any()); verify(bitmapConsumerMock, times(0)).accept(any()); }
@Test public void onImageAvailableIgnoresImagesWithInvalidPixelStrides() { pixelStrideStub = 5; setupMocksToCreateBitmap(); testSubject.onImageAvailable(imageReaderMock); verify(bitmapMock, times(0)).copyPixelsFromBuffer(imagePlaneStubBuffer); verify(bitmapConsumerMock, times(0)).accept(bitmapMock); }
@Test public void onImageAvailableIgnoresImagesWithInvalidRowStrides() { rowStrideStub = widthStub * pixelStrideStub - 1; setupMocksToCreateBitmap(); testSubject.onImageAvailable(imageReaderMock); verify(bitmapMock, times(0)).copyPixelsFromBuffer(imagePlaneStubBuffer); verify(bitmapConsumerMock, times(0)).accept(bitmapMock); }