protected Size findMatchingSize(CameraManager mgr,String camID,Class tClass,Size refSize,float targetAR) throws CameraAccessException {
  CameraCharacteristics camtype=mgr.getCameraCharacteristics(camID);
  StreamConfigurationMap streams=camtype.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
  if ((streams != null) && (streams.getOutputSizes(tClass) != null)) {
    Size bestmatch=null;
    float penalty=Float.MAX_VALUE;
    for (    Size size : streams.getOutputSizes(tClass)) {
      float ar=(float)size.getWidth() / (float)size.getHeight();
      float wd=(float)(size.getWidth() - refSize.getWidth()) / (float)refSize.getWidth();
      float hd=(float)(size.getHeight() - refSize.getHeight()) / (float)refSize.getHeight();
      wd=(wd < 0) ? (-2.0f * wd) : wd;
      hd=(hd < 0) ? (-2.0f * hd) : hd;
      float par=(Math.abs(ar - targetAR) < 1.0E-3f) ? 0.0f : 10.0f;
      float p=par + wd + hd;
      if (p < penalty) {
        bestmatch=size;
        penalty=p;
      }
    }
    return bestmatch;
  }
  return null;
}

protected Size findMatchingSize ( CameraManager mgr , String camID , Class tClass , Size refSize , float targetAR ) throws CameraAccessException { CameraCharacteristics camtype = mgr . getCameraCharacteristics ( camID ) ; StreamConfigurationMap streams = camtype . get ( CameraCharacteristics . SCALER_STREAM_CONFIGURATION_MAP ) ; if ( ( streams != null ) && ( streams . getOutputSizes ( tClass ) != null ) ) { Size bestmatch = null ; float penalty = Float . MAX_VALUE ; for ( Size size : streams . getOutputSizes ( tClass ) ) { float ar = ( float ) size . getWidth ( ) / ( float ) size . getHeight ( ) ; float wd = ( float ) ( size . getWidth ( ) - refSize . getWidth ( ) ) / ( float ) refSize . getWidth ( ) ; float hd = ( float ) ( size . getHeight ( ) - refSize . getHeight ( ) ) / ( float ) refSize . getHeight ( ) ; wd = ( wd < 0 ) ? ( - 2.0f * wd ) : wd ; hd = ( hd < 0 ) ? ( - 2.0f * hd ) : hd ; float par = ( Math . abs ( ar - targetAR ) < 1.0E-3f ) ? 0.0f : 10.0f ; float p = par + wd + hd ; if ( p < penalty ) { bestmatch = size ; penalty = p ; } } return bestmatch ; } return null ; }
find the best - suited size that is close to the supplied reference size ( and its aspect ratio ) which the camera can deliver for the selected image format .
