3434
3535package org .openftc .easyopencv ;
3636
37+ import android .annotation .SuppressLint ;
3738import android .graphics .ImageFormat ;
3839
3940import com .qualcomm .robotcore .util .RobotLog ;
@@ -258,10 +259,10 @@ public void startStreaming(int width, int height)
258259 @ Override
259260 public void startStreaming (int width , int height , OpenCvCameraRotation rotation )
260261 {
261- startStreaming (width , height , rotation , StreamFormat . YUY2 );
262+ startStreaming (width , height , rotation , null );
262263 }
263264
264- private int streamFormat2ImageFormat (StreamFormat streamFormat )
265+ private static int streamFormat2ImageFormat (StreamFormat streamFormat )
265266 {
266267 switch (streamFormat )
267268 {
@@ -271,9 +272,47 @@ private int streamFormat2ImageFormat(StreamFormat streamFormat)
271272 }
272273 }
273274
275+ private static boolean isSizeSupportedForFormat (CameraCharacteristics characteristics , int width , int height , StreamFormat format )
276+ {
277+ for (Size s : characteristics .getSizes (streamFormat2ImageFormat (format )))
278+ {
279+ if (s .getHeight () == height && s .getWidth () == width )
280+ {
281+ return true ;
282+ }
283+ }
284+
285+ return false ;
286+ }
287+
288+ @ SuppressLint ("DefaultLocale" )
289+ private static String getSizeAndFpsListForFormat (CameraCharacteristics characteristics , StreamFormat format )
290+ {
291+ StringBuilder builder = new StringBuilder ();
292+
293+ boolean areAnySupported = false ;
294+
295+ for (Size s : characteristics .getSizes (streamFormat2ImageFormat (format )))
296+ {
297+ int fps = characteristics .getMaxFramesPerSecond (streamFormat2ImageFormat (format ), s );
298+ builder .append (String .format ("[%dx%d @ %dFPS], " , s .getWidth (), s .getHeight (), fps ));
299+ areAnySupported = true ;
300+ }
301+
302+ return areAnySupported ? builder .toString () : "NONE" ;
303+ }
304+
305+ @ SuppressLint ("DefaultLocale" )
274306 @ Override
275307 public void startStreaming (final int width , final int height , OpenCvCameraRotation rotation , StreamFormat streamFormat )
276308 {
309+ boolean userExplicitlyRequestedFormat = streamFormat != null ;
310+
311+ if (streamFormat == null )
312+ {
313+ streamFormat = StreamFormat .YUY2 ;
314+ }
315+
277316 final int format = streamFormat2ImageFormat (streamFormat );
278317
279318 synchronized (cameraDeviceStateSync )
@@ -299,26 +338,36 @@ public void startStreaming(final int width, final int height, OpenCvCameraRotati
299338
300339 final CountDownLatch captureStartResult = new CountDownLatch (1 );
301340
302- boolean sizeSupported = false ;
303- for (Size s : cameraCharacteristics .getSizes (format ))
341+ boolean sizeSupportedForReqFormat = isSizeSupportedForFormat (cameraCharacteristics , width , height , streamFormat );
342+ boolean sizeSupportedForMjpeg = isSizeSupportedForFormat (cameraCharacteristics , width , height , StreamFormat .MJPEG );
343+
344+ if (!sizeSupportedForReqFormat )
304345 {
305- if (s .getHeight () == height && s .getWidth () == width )
306- {
307- sizeSupported = true ;
308- break ;
309- }
346+ throw new OpenCvCameraException (String .format (
347+ "Camera does not support requested resolution for format %s!" +
348+ "\n Supported resolutions for YUY2 are: %s\n " +
349+ "\n Supported resolutions for MJPEG are: %s\n " ,
350+ streamFormat ,
351+ getSizeAndFpsListForFormat (cameraCharacteristics , StreamFormat .YUY2 ),
352+ getSizeAndFpsListForFormat (cameraCharacteristics , StreamFormat .MJPEG )
353+ ));
310354 }
311355
312- if (!sizeSupported )
356+ final Size size = new Size (width , height );
357+
358+ if (!userExplicitlyRequestedFormat && streamFormat == StreamFormat .YUY2 && sizeSupportedForMjpeg )
313359 {
314- StringBuilder supportedSizesBuilder = new StringBuilder ();
360+ int maxFpsYuy2 = cameraCharacteristics .getMaxFramesPerSecond (streamFormat2ImageFormat (StreamFormat .YUY2 ), size );
361+ int maxFpsMjpeg = cameraCharacteristics .getMaxFramesPerSecond (streamFormat2ImageFormat (StreamFormat .MJPEG ), size );
315362
316- for ( Size s : cameraCharacteristics . getSizes ( format ) )
363+ if ( maxFpsMjpeg > maxFpsYuy2 )
317364 {
318- supportedSizesBuilder .append (String .format ("[%dx%d], " , s .getWidth (), s .getHeight ()));
365+ RobotLog .addGlobalWarningMessage (String .format (
366+ "You are using YUY2 image format for this camera. You could increase your maximum " +
367+ "FPS from %d to %d by choosing MJPEG format. To suppress this warning, explicitly " +
368+ "request YUY2 format." , maxFpsYuy2 , maxFpsMjpeg
369+ ));
319370 }
320-
321- throw new OpenCvCameraException ("Camera does not support requested resolution! Supported resolutions are " + supportedSizesBuilder .toString ());
322371 }
323372
324373 try
@@ -330,7 +379,6 @@ public void onConfigured( CameraCaptureSession session)
330379 {
331380 try
332381 {
333- Size size = new Size (width , height );
334382 int fps = cameraCharacteristics .getMaxFramesPerSecond (format , size );
335383
336384 //Indicate how we want to stream
0 commit comments