title: Picture distorted with Camera and getOptimalPreviewSize
body: <p>I am on a Camera App which taking basic pictures. I have an issue when I get the best optimal preview size.</p>
  
  <p>In fact, with this first code :</p>
  
  <pre><code>public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
      if (isPreviewRunning) {
          mCamera.stopPreview();
      }
      Camera.Parameters parameters = mCamera.getParameters();
      mCamera.setParameters(parameters);
      mCamera.startPreview();
      isPreviewRunning = true;
  }
  </code></pre>
  
  <p>The picture has a good quality :</p>
  
  <p><a href="http://img689.imageshack.us/i/04042011172937.jpg/" rel="nofollow noreferrer">http://img689.imageshack.us/i/04042011172937.jpg/</a></p>
  
  <p>But, with this code :</p>
  
  <pre><code>private Size getOptimalPreviewSize(List&lt;Size&gt; sizes, int w, int h) {
      final double ASPECT_TOLERANCE = 0.05;
      double targetRatio = (double) w / h;
      if (sizes == null) return null;
  
      Size optimalSize = null;
      double minDiff = Double.MAX_VALUE;
  
      int targetHeight = h;
  
      // Try to find an size match aspect ratio and size
      for (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);
          }
      }
  
      // Cannot find the one match the aspect ratio, ignore the requirement
      if (optimalSize == null) {
          minDiff = Double.MAX_VALUE;
          for (Size size : sizes) {
              if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                  optimalSize = size;
                  minDiff = Math.abs(size.height - targetHeight);
              }
          }
      }
      return optimalSize;
  }
  
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
      // Now that the size is known, set up the camera parameters and begin
      // the preview.
      if (isPreviewRunning) {
          mCamera.stopPreview();
      }
      Camera.Parameters parameters = mCamera.getParameters();
  
      List&lt;Size&gt; sizes = parameters.getSupportedPreviewSizes();
      Size optimalSize = getOptimalPreviewSize(sizes, w, h);
      parameters.setPreviewSize(optimalSize.width, optimalSize.height);
  
      mCamera.setParameters(parameters);
      mCamera.startPreview();
      isPreviewRunning =true;
  }
  </code></pre>
  
  <p>The picture is totally distorted :</p>
  
  <p><a href="http://img97.imageshack.us/i/04042011173220.jpg/" rel="nofollow noreferrer">http://img97.imageshack.us/i/04042011173220.jpg/</a></p>
  
  <p>How can I resolve this problem??</p>
  
  <p>I am using a HTC Desire HD.</p>
  
  <p>It is probably my saving method too ? :</p>
  
  <pre><code>PictureCallback mPictureCallbackJpeg = new PictureCallback() {      
          public void onPictureTaken(byte[] imageData, Camera camera) {
      ....
  
      BitmapFactory.Options options=new BitmapFactory.Options();
                              options.inSampleSize = 5; 
                              Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,imageData.length,options);
                              FileOutputStream fOut = new FileOutputStream(path + "/"+fl);
                              BufferedOutputStream bos = new BufferedOutputStream(fOut);
  
  
      myImage.compress(CompressFormat.JPEG, 100, bos);
      bos.flush();
      bos.close();
      ....
  }
  </code></pre>
  
  <p>Thanks</p>