title: Android how to fix camera orientation
body: <p><img src="https://i.stack.imgur.com/Z1nhf.jpg" width="250"/></p>
  
  <p>Notice how the view of the camera (NOT THE CAPTURED IMAGE) was flipped to left (image above), the orientation of the <code>Activity</code> is correct, but the camera view is messed up, please help me guys :) thank you.</p>
  
  <p>Here is the XML layout file: </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" &gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center|top"
              android:orientation="vertical" &gt;
  
              &lt;SurfaceView
                  android:id="@+id/camerapreview"
                  android:layout_margin="10dp"
                  android:layout_width="300dp"
                  android:layout_height="300dp" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And here is the code for the activity: </p>
  
  <pre class="lang-java prettyprint-override"><code>public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback {
  
      Camera camera;
      SurfaceView surfaceView;
      SurfaceHolder surfaceHolder;
      boolean previewing = false;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.camera);
  
          surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
          surfaceHolder = surfaceView.getHolder();
          surfaceHolder.addCallback(this);
  
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
          if(previewing){
              camera.stopPreview();
              previewing = false;
          }
  
          if (camera != null){
              try {
                  camera.setPreviewDisplay(surfaceHolder);
                  camera.startPreview();
                  previewing = true;
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
          camera = Camera.open();
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
          camera.stopPreview();
          camera.release();
          camera = null;
          previewing = false;
      }
  }
  </code></pre>