|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugins.camera.media; |
| 6 | + |
| 7 | +import android.graphics.ImageFormat; |
| 8 | +import android.media.Image; |
| 9 | +import android.media.ImageReader; |
| 10 | +import android.os.Handler; |
| 11 | +import android.os.Looper; |
| 12 | +import android.view.Surface; |
| 13 | +import androidx.annotation.NonNull; |
| 14 | +import androidx.annotation.VisibleForTesting; |
| 15 | +import io.flutter.plugin.common.EventChannel; |
| 16 | +import io.flutter.plugins.camera.types.CameraCaptureProperties; |
| 17 | +import java.nio.ByteBuffer; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +// Wraps an ImageReader to allow for testing of the image handler. |
| 24 | +public class ImageStreamReader { |
| 25 | + |
| 26 | + /** |
| 27 | + * The image format we are going to send back to dart. Usually it's the same as streamImageFormat |
| 28 | + * but in the case of NV21 we will actually request YUV frames but convert it to NV21 before |
| 29 | + * sending to dart. |
| 30 | + */ |
| 31 | + private final int dartImageFormat; |
| 32 | + |
| 33 | + private final ImageReader imageReader; |
| 34 | + private final ImageStreamReaderUtils imageStreamReaderUtils; |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new instance of the {@link ImageStreamReader}. |
| 38 | + * |
| 39 | + * @param imageReader is the image reader that will receive frames |
| 40 | + * @param imageStreamReaderUtils is an instance of {@link ImageStreamReaderUtils} |
| 41 | + */ |
| 42 | + @VisibleForTesting |
| 43 | + public ImageStreamReader( |
| 44 | + @NonNull ImageReader imageReader, |
| 45 | + int dartImageFormat, |
| 46 | + @NonNull ImageStreamReaderUtils imageStreamReaderUtils) { |
| 47 | + this.imageReader = imageReader; |
| 48 | + this.dartImageFormat = dartImageFormat; |
| 49 | + this.imageStreamReaderUtils = imageStreamReaderUtils; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a new instance of the {@link ImageStreamReader}. |
| 54 | + * |
| 55 | + * @param width is the image width |
| 56 | + * @param height is the image height |
| 57 | + * @param imageFormat is the {@link ImageFormat} that should be returned to dart. |
| 58 | + * @param maxImages is how many images can be acquired at one time, usually 1. |
| 59 | + */ |
| 60 | + public ImageStreamReader(int width, int height, int imageFormat, int maxImages) { |
| 61 | + this.dartImageFormat = imageFormat; |
| 62 | + this.imageReader = |
| 63 | + ImageReader.newInstance(width, height, computeStreamImageFormat(imageFormat), maxImages); |
| 64 | + this.imageStreamReaderUtils = new ImageStreamReaderUtils(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the image format to stream based on a requested input format. Usually it's the same |
| 69 | + * except when dart is requesting NV21. In that case we stream YUV420 and process it into NV21 |
| 70 | + * before sending the frames over. |
| 71 | + * |
| 72 | + * @param dartImageFormat is the image format dart is requesting. |
| 73 | + * @return the image format that should be streamed from the camera. |
| 74 | + */ |
| 75 | + @VisibleForTesting |
| 76 | + public static int computeStreamImageFormat(int dartImageFormat) { |
| 77 | + if (dartImageFormat == ImageFormat.NV21) { |
| 78 | + return ImageFormat.YUV_420_888; |
| 79 | + } else { |
| 80 | + return dartImageFormat; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Processes a new frame (image) from the image reader and send the frame to Dart. |
| 86 | + * |
| 87 | + * @param image is the image which needs processed as an {@link Image} |
| 88 | + * @param captureProps is the capture props from the camera class as {@link |
| 89 | + * CameraCaptureProperties} |
| 90 | + * @param imageStreamSink is the image stream sink from dart as a dart {@link |
| 91 | + * EventChannel.EventSink} |
| 92 | + */ |
| 93 | + @VisibleForTesting |
| 94 | + public void onImageAvailable( |
| 95 | + @NonNull Image image, |
| 96 | + @NonNull CameraCaptureProperties captureProps, |
| 97 | + @NonNull EventChannel.EventSink imageStreamSink) { |
| 98 | + try { |
| 99 | + Map<String, Object> imageBuffer = new HashMap<>(); |
| 100 | + |
| 101 | + // Get plane data ready |
| 102 | + if (dartImageFormat == ImageFormat.NV21) { |
| 103 | + imageBuffer.put("planes", parsePlanesForNv21(image)); |
| 104 | + } else { |
| 105 | + imageBuffer.put("planes", parsePlanesForYuvOrJpeg(image)); |
| 106 | + } |
| 107 | + |
| 108 | + imageBuffer.put("width", image.getWidth()); |
| 109 | + imageBuffer.put("height", image.getHeight()); |
| 110 | + imageBuffer.put("format", dartImageFormat); |
| 111 | + imageBuffer.put("lensAperture", captureProps.getLastLensAperture()); |
| 112 | + imageBuffer.put("sensorExposureTime", captureProps.getLastSensorExposureTime()); |
| 113 | + Integer sensorSensitivity = captureProps.getLastSensorSensitivity(); |
| 114 | + imageBuffer.put( |
| 115 | + "sensorSensitivity", sensorSensitivity == null ? null : (double) sensorSensitivity); |
| 116 | + |
| 117 | + final Handler handler = new Handler(Looper.getMainLooper()); |
| 118 | + handler.post(() -> imageStreamSink.success(imageBuffer)); |
| 119 | + image.close(); |
| 120 | + |
| 121 | + } catch (IllegalStateException e) { |
| 122 | + // Handle "buffer is inaccessible" errors that can happen on some devices from ImageStreamReaderUtils.yuv420ThreePlanesToNV21() |
| 123 | + final Handler handler = new Handler(Looper.getMainLooper()); |
| 124 | + handler.post( |
| 125 | + () -> |
| 126 | + imageStreamSink.error( |
| 127 | + "IllegalStateException", |
| 128 | + "Caught IllegalStateException: " + e.getMessage(), |
| 129 | + null)); |
| 130 | + image.close(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Given an input image, will return a list of maps suitable to send back to dart where each map |
| 136 | + * describes the image plane. |
| 137 | + * |
| 138 | + * <p>For Yuv / Jpeg, we do no further processing on the frame so we simply send it as-is. |
| 139 | + * |
| 140 | + * @param image - the image to process. |
| 141 | + * @return parsed map describing the image planes to be sent to dart. |
| 142 | + */ |
| 143 | + @NonNull |
| 144 | + public List<Map<String, Object>> parsePlanesForYuvOrJpeg(@NonNull Image image) { |
| 145 | + List<Map<String, Object>> planes = new ArrayList<>(); |
| 146 | + |
| 147 | + // For YUV420 and JPEG, just send the data as-is for each plane. |
| 148 | + for (Image.Plane plane : image.getPlanes()) { |
| 149 | + ByteBuffer buffer = plane.getBuffer(); |
| 150 | + |
| 151 | + byte[] bytes = new byte[buffer.remaining()]; |
| 152 | + buffer.get(bytes, 0, bytes.length); |
| 153 | + |
| 154 | + Map<String, Object> planeBuffer = new HashMap<>(); |
| 155 | + planeBuffer.put("bytesPerRow", plane.getRowStride()); |
| 156 | + planeBuffer.put("bytesPerPixel", plane.getPixelStride()); |
| 157 | + planeBuffer.put("bytes", bytes); |
| 158 | + |
| 159 | + planes.add(planeBuffer); |
| 160 | + } |
| 161 | + return planes; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Given an input image, will return a single-plane NV21 image. Assumes YUV420 as an input type. |
| 166 | + * |
| 167 | + * @param image - the image to process. |
| 168 | + * @return parsed map describing the image planes to be sent to dart. |
| 169 | + */ |
| 170 | + @NonNull |
| 171 | + public List<Map<String, Object>> parsePlanesForNv21(@NonNull Image image) { |
| 172 | + List<Map<String, Object>> planes = new ArrayList<>(); |
| 173 | + |
| 174 | + // We will convert the YUV data to NV21 which is a single-plane image |
| 175 | + ByteBuffer bytes = |
| 176 | + imageStreamReaderUtils.yuv420ThreePlanesToNV21( |
| 177 | + image.getPlanes(), image.getWidth(), image.getHeight()); |
| 178 | + |
| 179 | + Map<String, Object> planeBuffer = new HashMap<>(); |
| 180 | + planeBuffer.put("bytesPerRow", image.getWidth()); |
| 181 | + planeBuffer.put("bytesPerPixel", 1); |
| 182 | + planeBuffer.put("bytes", bytes.array()); |
| 183 | + planes.add(planeBuffer); |
| 184 | + return planes; |
| 185 | + } |
| 186 | + |
| 187 | + /** Returns the image reader surface. */ |
| 188 | + @NonNull |
| 189 | + public Surface getSurface() { |
| 190 | + return imageReader.getSurface(); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Subscribes the image stream reader to handle incoming images using onImageAvailable(). |
| 195 | + * |
| 196 | + * @param captureProps is the capture props from the camera class as {@link |
| 197 | + * CameraCaptureProperties} |
| 198 | + * @param imageStreamSink is the image stream sink from dart as {@link EventChannel.EventSink} |
| 199 | + * @param handler is generally the background handler of the camera as {@link Handler} |
| 200 | + */ |
| 201 | + public void subscribeListener( |
| 202 | + @NonNull CameraCaptureProperties captureProps, |
| 203 | + @NonNull EventChannel.EventSink imageStreamSink, |
| 204 | + @NonNull Handler handler) { |
| 205 | + imageReader.setOnImageAvailableListener( |
| 206 | + reader -> { |
| 207 | + Image image = reader.acquireNextImage(); |
| 208 | + if (image == null) return; |
| 209 | + |
| 210 | + onImageAvailable(image, captureProps, imageStreamSink); |
| 211 | + }, |
| 212 | + handler); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Removes the listener from the image reader. |
| 217 | + * |
| 218 | + * @param handler is generally the background handler of the camera |
| 219 | + */ |
| 220 | + public void removeListener(@NonNull Handler handler) { |
| 221 | + imageReader.setOnImageAvailableListener(null, handler); |
| 222 | + } |
| 223 | + |
| 224 | + /** Closes the image reader. */ |
| 225 | + public void close() { |
| 226 | + imageReader.close(); |
| 227 | + } |
| 228 | +} |
0 commit comments