title: android - front camera preview stretched in Samsung galaxy s4 mini
body: <p>We implemented the custom camera functionality in my app. This was working fine in all the devices like nexus4,5,samsung s3,s4 etc except in Samsung galaxy S4 mini. The issue is image was stretching in front camera only but it's working good in back camera of that device.</p>
  
  <p>For Samsung galaxy s4 mini, we are getting the screen size is 540X960 and we got supported preview size is 1280x720. The aspect ratio also same but we are facing image stretching issue. I was tried a lot but i am unable to solve this issue especially in front camera of samsung galaxy s4 mini. Please can anyone help me.</p>
  
  <pre><code>Code
  
  int currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
  Camera camera = Camera.open(currentCameraId);
  try {
        camera.setPreviewDisplay(surfaceHolder);
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPictureFormat(PixelFormat.JPEG);
        parameters.set("orientation", "portrait");
  
        camera.setDisplayOrientation(90);
        parameters.setRotation(270);
        Display display = getWindowManager().getDefaultDisplay();
        int widthd = display.getWidth(); // deprecated
        int heightd = display.getHeight();
  
        List&lt;Camera.Size&gt; previewSizes = parameters.getSupportedPreviewSizes();
  
        List&lt;Camera.Size&gt; pictureSizes = parameters.getSupportedPictureSizes();
  
        Camera.Size mPreviewSize = getOptimalPreviewSize(previewSizes, widthd, heightd);
  
        Camera.Size mpicSize = getOptimalPreviewSize(pictureSizes, widthd, heightd);
  
  
        parameters.setPreviewSize(mPreviewSize.width,mPreviewSize.height);
        parameters.setPictureSize(mpicSize.width,mpicSize.height);
        camera.setParameters(parameters);
     } catch (IOException exception) {
         camera.release();
     }
     camera.startPreview();
  </code></pre>
  
  <p>Please refer the following code for computing the optimal size.</p>
  
  <pre><code>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>