title: Camera Preview Stretched on Few Android Devices
body: <p>I am making custom Camera like "SnapChat" for a Android app but camera preview is stretched on Few devices like(Moto g second generation , one+ one) but not on(Samsung s3, Samsung s4). I have use the following reference  <a href="https://stackoverflow.com/questions/16727836/camera-display-preview-in-full-screen-does-not-maintain-aspect-ratio-image-i">Camera display / preview in full screen does not maintain aspect ratio - image is skewed, stretched in order to fit on the screen</a>. But this does not help me 100%. I am sharing the screen .</p>
  
  <p><strong>Stretched image on Samsung Moto G second generation is .</strong>
  <img src="https://i.stack.imgur.com/J20bf.jpg" alt="enter image description here">
  <img src="https://i.stack.imgur.com/uDWkc.jpg" alt="enter image description here"></p>
  
  <p><strong>Samsung S3 images which does not stretched is Above</strong> </p>
  
  <pre><code>private void setPreviewLayout() {
      if (null == mCamera) {
          return;
      }
      Camera.Parameters parameters = null;
      Camera.Size size = null;
      try {
          int screenWidth = (int) getResources().getDisplayMetrics().widthPixels;
          int screenHeight = (int) getResources().getDisplayMetrics().heightPixels;
          parameters = mCamera.getParameters();
          size = getOptimalPreviewSize(mCamera.getParameters().getSupportedPreviewSizes(), screenWidth, screenHeight);
          if (size != null) {
  
              parameters.setPreviewSize(size.width, size.height);
  
          }
  
          parameters.setPictureSize(screenHeight, screenWidth);
          ;
          mCamera.setParameters(parameters);
          if (on &amp;&amp; currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
              parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
          } else {
              parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
          }
          parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
          parameters.setExposureCompensation(0);
          parameters.setPictureFormat(ImageFormat.JPEG);
          parameters.setJpegQuality(100);
          List&lt;String&gt; focusModes = parameters.getSupportedFocusModes();
          if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
              parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
          } else if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
              parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
          }
          mCamera.setParameters(parameters);
          /*
           * camera.setPreviewDisplay(surfaceHolder); camera.startPreview();
           */
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  private Camera.Size getOptimalPreviewSize(List&lt;Camera.Size&gt; sizes, int w, int h) {
      final double ASPECT_TOLERANCE = 0.1;
      double targetRatio = (double) h / w;
  
      if (sizes == null)
          return null;
  
      Camera.Size optimalSize = null;
      double minDiff = Double.MAX_VALUE;
  
      int targetHeight = h;
  
      for (Camera.Size size : sizes) {
          double ratio = (double) size.width / size.height;
          if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE)
              continue;
          if (Math.abs(size.height - targetHeight) &lt; minDiff) {
              optimalSize = size;
              minDiff = Math.abs(size.height - targetHeight);
          }
      }
  
      if (optimalSize == null) {
          minDiff = Double.MAX_VALUE;
          for (Camera.Size size : sizes) {
              if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                  optimalSize = size;
                  minDiff = Math.abs(size.height - targetHeight);
              }
          }
      }
      return optimalSize;
  }
  </code></pre>