title: Camera : setDisplayOrientation function is not working for Samsung Galaxy ACE with Android 2.3.6
body: <p>I was trying to create a simple camera application for research.
  I Read <a href="http://developer.android.com/guide/topics/media/camera.html#custom-camera" rel="nofollow noreferrer">Android Camera Official Document</a> and then started coding.
  so I did some steps to get it work</p>
  
  <p>1.Added required permissions for Camera feature in app.</p>
  
  <p>2.locked my activity to PORTRAIT mode only.</p>
  
  <blockquote>
    <p>setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);</p>
  </blockquote>
  
  <p>3.Added several camera callbacks to get my camera works.</p>
  
  <ul>
  <li>SurfaceHolder.Callback,</li>
  <li>Camera.PreviewCallback</li>
  <li>AutoFocusCallback</li>
  <li>ShutterCallback</li>
  <li>PictureCallback for RAW image data</li>
  <li>PictureCallback for JPG image data</li>
  </ul>
  
  <p>4.In <strong>surfaceChanged</strong> method I am customizing camera settings.
  and so far I got working this for almost all android devices</p>
  
  <ul>
  <li>LG </li>
  <li>Spice </li>
  <li>Samsung </li>
  <li>HTC </li>
  <li>Micromax</li>
  <li>Sony </li>
  <li>Motorola</li>
  <li>Google Nexus series.</li>
  </ul>
  
  <p><strong>But</strong> then I tested on <strong>Samsung Galaxy ACE</strong> with Android Version 2.3.6
  and found Camera Display Preview is Rotated to landscape mode.</p>
  
  <p>So after putting log-cat/break points I come to know that below methods are not working for this particular model</p>
  
  <pre><code>//This method is not working for Samsung Galaxy ACE
   camera.setDisplayOrientation(90); 
  //or 
  parameters.set("orientation", "portrait");
  //or
  parameters.setRotation(90);
  </code></pre>
  
  <p><strong>Note:</strong> <em>I Hunted bunch of solutions over Google and SO as well but so far didn't get any luck in this</em></p>
  
  <ul>
  <li>Issus reported <a href="http://code.google.com/p/android/issues/detail?id=1193" rel="nofollow noreferrer">here</a> </li>
  <li><a href="https://stackoverflow.com/questions/5157984/android-camera-surfaceview-orientation">android camera surfaceview orientation</a></li>
  <li><a href="https://stackoverflow.com/questions/13251984/android-portrait-locked-camera-preview">Android portrait locked Camera Preview</a></li>
  <li><a href="https://stackoverflow.com/questions/3841122/android-camera-preview-is-sideways">Android - Camera preview is sideways</a></li>
  <li><a href="https://stackoverflow.com/questions/5309029/android-camera-rotate">Android camera rotate</a></li>
  <li><a href="https://stackoverflow.com/questions/10278155/android-camera-preview-rotated">Android: Camera preview Rotated</a></li>
  <li><a href="https://stackoverflow.com/questions/4645960/how-to-set-android-camera-orientation-properly">How to set Android camera orientation properly?</a></li>
  </ul>
  
  <p>For your reference My sample code is below</p>
  
  <pre><code>@Override
      public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
          Log.v(TAG, "surfaceChanged get called");
  
          if (previewing) {
              camera.stopPreview();
              previewing = false;
          }
  
          if (camera != null) {
              try {
                  Camera.Parameters parameters = camera.getParameters();
                  List&lt;Size&gt; sizes = parameters.getSupportedPictureSizes();
  
                  Camera.Size result = null;
                  for (int i = 0; i &lt; sizes.size(); i++) {
                      result = (Size) sizes.get(i);
                      Log.i("PictureSize", "Supported Size. Width: "
                              + result.width + "height : " + result.height);
  
                      if (result.width == 640) {
                          parameters.setPreviewSize(result.width, result.height);// 640*480
                          parameters.setPictureSize(result.width, result.height);
                      } else {
  
                      }
                  }
  
  
                  //**************************************************************//
  
                  /* 
                   * Here is the logic I have added to Manage Camera Display Orientation
                   *  It checks Current Orientation and SDK and then rotate display to make it Portrait view.
                   */
                  int currentSDKVersion = android.os.Build.VERSION.SDK_INT;
                  Log.d(TAG, "currentVersion " + currentSDKVersion);
  
                  if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                      if (currentSDKVersion != 7) {
  
                          camera.setDisplayOrientation(90);
                           parameters.setRotation(90);
                      } else {
                              parameters.setRotation(90);
  
                          /*
                           * params.set("orientation", "portrait");
                           * params.set("rotation",90);
                           */
                      }
                  } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                      if (currentSDKVersion != 7) {
  
                          camera.setDisplayOrientation(0);
                      } else {
  
                          parameters.set("orientation", "landscape");
                          parameters.set("rotation", 90);
  
                      }
                  }
  
  
                  //**************************************************************//
  
                  camera.setParameters(parameters);
                  camera.setPreviewDisplay(surfaceHolder);
                  camera.startPreview();
                  camera.autoFocus(myAutoFocusCallback);
                  camera.setOneShotPreviewCallback(cameraPreviewCallback);
                  previewing = true;
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  </code></pre>
  
  <p>My Camera Activity (MainActivity.java) Full code is:</p>
  
  <pre><code>public class MainActivity extends Activity implements SurfaceHolder.Callback,
          OnClickListener {
  
      public static final String TAG = "CameraActivity";
      private Context context = null;
      Camera camera = null;
      private SurfaceView surfaceView = null;
      private SurfaceHolder surfaceHolder = null;
      boolean previewing = false;
      private LayoutInflater controlInflater = null;
      private Button buttonTakePicture = null;
      //private TextView textViewResultInfo = null;
  
      @SuppressWarnings("deprecation")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          Log.v(TAG, "onCreate get called");
  
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
          //textViewResultInfo = (TextView) findViewById(R.id.textViewResultInfo);
          context = this;
  
          getWindow().setFormat(PixelFormat.UNKNOWN);
          surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
          surfaceHolder = surfaceView.getHolder();
          surfaceHolder.addCallback(this);
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
          controlInflater = LayoutInflater.from(getBaseContext());
          View viewControl = controlInflater.inflate(R.layout.control, null);
          LayoutParams layoutParamsControl = new LayoutParams(
                  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
          this.addContentView(viewControl, layoutParamsControl);
  
          buttonTakePicture = (Button) findViewById(R.id.takepicture);
          buttonTakePicture.setOnClickListener(this);
  
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder arg0) {
  
          Log.v(TAG, "surfaceCreated get called");
          camera = Camera.open();
  
          if (camera != null) {
              try {
                  camera.setPreviewDisplay(surfaceHolder);
                  camera.startPreview();
                  previewing = true;
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
          Log.v(TAG, "surfaceChanged get called");
  
          if (previewing) {
              camera.stopPreview();
              previewing = false;
          }
  
          if (camera != null) {
              try {
  
                   //new MainActivity().setCameraDisplayOrientation();
  
                  Camera.Parameters parameters = camera.getParameters();
                  // List&lt;String&gt; focusModes =
                  // parameters.getSupportedFocusModes();
                  // if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
                  // {
                  // parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                  // }
  
                  List&lt;Size&gt; sizes = parameters.getSupportedPictureSizes();
  
                  Camera.Size result = null;
                  for (int i = 0; i &lt; sizes.size(); i++) {
                      result = (Size) sizes.get(i);
                      Log.i("PictureSize", "Supported Size. Width: "
                              + result.width + "height : " + result.height);
  
                      if (result.width == 640) {
                          parameters.setPreviewSize(result.width, result.height);// 640*480
                          parameters.setPictureSize(result.width, result.height);
                      } else {
  
                      }
                  }
  
  
                  //**************************************************************//
  
                  Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                  int rotation = display.getRotation();
                  Log.v(TAG, "Current Device Orientation is ="+rotation);
  
                  /* 
                   * Here is the logic I have added to Manage Camera Display Orientation
                   *  It checks Current Orientation and SDK and then rotate display to make it Portrait view.
                   */
                  int currentSDKVersion = android.os.Build.VERSION.SDK_INT;
                  Log.d(TAG, "currentVersion " + currentSDKVersion);
  
                  if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                      if (currentSDKVersion != 7) {
                          Log.i(TAG, "ORIENTATION_PORTRAIT +SDK is: " + currentSDKVersion
                                  + "rotate 90");
                          camera.setDisplayOrientation(90);
                           parameters.setRotation(90);
                      } else {
                          Log.i(TAG, "ORIENTATION_PORTRAIT +SDK is: " + currentSDKVersion
                                  + "rotate 90");
                          parameters.setRotation(90);
  
                          /*
                           * params.set("orientation", "portrait");
                           * params.set("rotation",90);
                           */
                      }
                  } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                      if (currentSDKVersion != 7) {
                          Log.i(TAG, "ORIENTATION_LANDSCAPE +SDK is: " + currentSDKVersion
                                  + "rotate 90");
  
                          camera.setDisplayOrientation(0);
                      } else {
  
                          Log.i(TAG, "ORIENTATION_LANDSCAPE +SDK is: " + currentSDKVersion
                                  + "rotate 90");
  
                          parameters.set("orientation", "landscape");
                          parameters.set("rotation", 90);
  
                      }
                  }
  
  
                  //**************************************************************//
  
  
                  camera.setParameters(parameters);
                  camera.setPreviewDisplay(surfaceHolder);
                  camera.startPreview();
                  camera.autoFocus(myAutoFocusCallback);
                  camera.setOneShotPreviewCallback(cameraPreviewCallback);
                  previewing = true;
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder arg0) {
          Log.v(TAG, "surfaceDestroyed get called");
          camera.stopPreview();
          camera.release();
          camera = null;
          previewing = false;
  
      }
  
  
      public void setCameraDisplayOrientation() 
      {        
          Log.v(TAG, "setCameraDisplayOrientation get called");
  
           if (camera == null)
           {
               Log.d(TAG,"setCameraDisplayOrientation - camera null");
               return;             
           }
  
           Camera.CameraInfo info = new Camera.CameraInfo();
           Camera.getCameraInfo(1, info);
  
           WindowManager winManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
           int rotation = winManager.getDefaultDisplay().getRotation();
  
           int degrees = 0;
  
           switch (rotation) 
           {
               case Surface.ROTATION_0: degrees = 0; break;
               case Surface.ROTATION_90: degrees = 90; break;
               case Surface.ROTATION_180: degrees = 180; break;
               case Surface.ROTATION_270: degrees = 270; break;
           }
  
           int result;
           if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) 
           {
               result = (info.orientation + degrees) % 360;
               result = (360 - result) % 360;  // compensate the mirror
           } else {  // back-facing
               result = (info.orientation - degrees + 360) % 360;
           }
           camera.setDisplayOrientation(result);
      }
  
  
      @Override
      public void onClick(View v) {
          Log.v(TAG, "onClick get called");
  
          if (v == buttonTakePicture) {
              camera.takePicture(myShutterCallback, myPictureCallback_RAW,
                      myPictureCallback_JPG);
          }
  
      }
  
      private Camera.PreviewCallback cameraPreviewCallback = new Camera.PreviewCallback() {
          @Override
          public void onPreviewFrame(byte[] data, Camera camera) {
              Log.i(TAG, "onPreviewFrame size=" + data.length);
          }
      };
  
      AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {
  
          @Override
          public void onAutoFocus(boolean arg0, Camera arg1) {
              Log.v(TAG, "onAutoFocus get called");
              buttonTakePicture.setEnabled(true);
          }
      };
  
      ShutterCallback myShutterCallback = new ShutterCallback() {
  
          @Override
          public void onShutter() {
              Log.v(TAG, "onShutter get called");
          }
      };
  
      PictureCallback myPictureCallback_RAW = new PictureCallback() {
  
          @Override
          public void onPictureTaken(byte[] arg0, Camera arg1) {
              Log.v(TAG, "onPictureTaken-RAW get called");
  
          }
      };
  
      public static Bitmap RotateBitmap(Bitmap source, float angle) {
          Matrix matrix = new Matrix();
          matrix.postRotate(angle);
          return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                  source.getHeight(), matrix, true);
      }
  
      PictureCallback myPictureCallback_JPG = new PictureCallback() {
  
          @Override
          public void onPictureTaken(byte[] arg0, Camera arg1) {
              Bitmap rawImage = BitmapFactory.decodeByteArray(arg0, 0,
                      arg0.length);
  
              if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                  Log.v(TAG, "#####  ORIENTATION_PORTRAIT   ####");
  
                  rawImage = MainActivity.RotateBitmap(rawImage, 90);
  
                  ByteArrayOutputStream stream = new ByteArrayOutputStream();
                  rawImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                  arg0 = stream.toByteArray();
  
              } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                  Log.v(TAG, "#####   ORIENTATION_LANDSCAPE  #####");
  
              }
  
              Intent intent = new Intent(MainActivity.this, ResultActivity.class);
              intent.putExtra("picture", arg0);
              startActivity(intent);
  
              Log.v(TAG, "onPictureTaken-JPG get called");
  
          }
      };
  
      /**
       * Get the size in bitmap.
       * 
       * @param bitmap
       * @return size in bytes
       */
      @TargetApi(12)
      public static int getBitmapSize(Bitmap bitmap) {
          if (MainActivity.hasHoneycombMR1()) {
              return bitmap.getByteCount();
          }
          // Pre HC-MR1
          return bitmap.getRowBytes() * bitmap.getHeight();
      }
  
      public static boolean hasHoneycombMR1() {
          return Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB_MR1;
      }
  
  }
  </code></pre>
  
  <p><strong>Edit</strong> : I have posted Comment on <a href="https://code.google.com/p/android/issues/detail?can=2&amp;start=0&amp;num=100&amp;q=&amp;colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&amp;groupby=&amp;sort=&amp;id=1193" rel="nofollow noreferrer">Developer forum</a> but no response.</p>
  
  <p>Please!! Someone have any Idea regarding this problem.</p>
  
  <p>I would really appreciate for any kind of suggestion.</p>