title: Android Camera image distorted - SurfaceHolder -
body: <p>I have a weird problem with my Camera-SurfaceHolder.</p>
  
  <p>I want to show the image of the camera in my activity.
  It all works greatly on my GalaxyS1 (CyanogenMod - Android 4.4)
  On my S3 (also CyanogenMod - Android 4.4) on the other hand it looks weird.</p>
  
  <p>The landscape image without title bar is okay but when I show the title bar or turn it into portrait mode it looks distorted:</p>
  
  <p><img src="https://i.stack.imgur.com/tMupq.png" alt="enter image description here"></p>
  
  <p><strong>--- EDIT ---</strong></p>
  
  <p>Thank you Alex Cohn for your help. It looks like you are right. The preview for the portraid mode is now working nicely. But still it looks distorted on the landscape view. I checked the preview scale and it looks okay. As far as I can see I set the preview size properly, too. So what is wrong with it?</p>
  
  <p>Here is the current code:</p>
  
  <pre><code>    private static final String TAG = CameraView.class.getSimpleName();
  
        private SurfaceHolder surfaceHolder;
        private Camera camera;
        private List&lt;Size&gt; mSupportedPreviewSizes;
        private Size mPreviewSize;
  
        public CameraView(Activity activity) {
          super(activity);
          surfaceHolder = getHolder();
          surfaceHolder.addCallback(this);
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
          camera = Camera.open();
          mSupportedPreviewSizes = camera.getParameters()
                  .getSupportedPreviewSizes();
          setCameraDisplayOrientation(activity, 0, camera);
        }
  
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
          Camera.Parameters parameters = camera.getParameters();
          parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
          camera.setParameters(parameters);
          camera.startPreview();
        }
  
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
         try {
           camera.setPreviewDisplay(holder);
         } catch (IOException exception) {
               camera.release();
               camera = null;
         }
        }
  
       @Override
         public void surfaceDestroyed(SurfaceHolder arg0) {
         camera.stopPreview();
         camera.release();
         camera = null;
       }
  
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
      final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
                  setMeasuredDimension(width, height);
  
      if (mSupportedPreviewSizes != null) {
          mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
      }
    }
  
    private Camera.Size getOptimalPreviewSize(List&lt;Camera.Size&gt; sizes, int w,
                  int h) {...}
  
    public static void setCameraDisplayOrientation(Activity activity,
                  int cameraId, Camera camera) {...}
  </code></pre>
  
  <p>--- EDIT ---</p>
  
  <p>I logged the values for the onMeasure and surface Changed. They seem to be ok:</p>
  
  <blockquote>
    <p><strong>Portrait:</strong> </p>
    
    <p>SURFACE CHANGED: Witdh:720 Height:1134</p>
    
    <p>onMeasure: width:720 height:1134</p>
    
    <p><strong>Landscape:</strong> </p>
    
    <p>SURFACE CHANGED: Witdh:1280 Height:590</p>
    
    <p>onMeasure: width:1280 height:590</p>
    
    <p>Preview Size: Width: 704 Height:576</p>
  </blockquote>
  
  <p>I get the following possible preview sizes.</p>
  
  <p>There are 8 elements in the array:</p>
  
  <p>960 x 720,
  1280 x 720,
  1184 x 666,
  960 x 640,
  <strong>704 x 576</strong>,
  640 x 480,
  352 x 288,
  320 x 240</p>
  
  <p>Hopefully anyone can help!</p>
  
  <p>Thanks,
   Tobias</p>