title: Android Front Facing Camera Taking Inverted Photos
body: <p>I have this app that is running in portrait mode and as a part of one activity I have a camera object running as a fragment in it.</p>
  
  <p>I have the option to switch from front to back-facing camera and when taking photos with the back camera all is fine and well. </p>
  
  <p>When I take photos with the front-facing camera though, they get inverted 180 degrees. 
  Now I know that this is probably something to do with the orientation being in portrait mode, but having it in landscape would just kill the idea of my application.</p>
  
  <p>Is there anyway this can be fixed so the picture taken is the same as the one you see in the preview?</p>
  
  <pre><code>        listener = new OrientationEventListener(this.getActivity(),SensorManager.SENSOR_DELAY_NORMAL){
  
          @Override
          public void onOrientationChanged(int orientation) {
              // TODO Auto-generated method stub
              if (orientation == ORIENTATION_UNKNOWN) return;
               android.hardware.Camera.CameraInfo info =
                      new android.hardware.Camera.CameraInfo();
               android.hardware.Camera.getCameraInfo(mCurrentCamera, info);
               orientation = (orientation + 45) / 90 * 90;
               int rotation = 0;
               if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                   rotation = (info.orientation - orientation + 360) % 360;
               } else {  // back-facing camera
                   rotation = (info.orientation + orientation) % 360;
               }
               if (mCamera.getParameters() != null){
               Camera.Parameters mParameters = mCamera.getParameters();
  
               mParameters.setRotation(rotation);
               mCamera.setParameters(mParameters);
               }
          }
  
      };
      listener.enable();
      if (listener.canDetectOrientation())
          Log.d("Orientation Possible", "Orientation Possible");
  </code></pre>
  
  <p>The problem is, after I try an take a picture this thing crashes. Also, if it runs for a while just in preview mode it crashes again. I also should probably add, that in another method I am doing this.</p>
  
  <pre><code>   public void switchCamera(Camera camera) {
      setCamera(camera);
      try {
          camera.setPreviewDisplay(mHolder);
      } catch (IOException exception) {
          Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
      }
      Camera.Parameters parameters = camera.getParameters();
      parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
  
      requestLayout();
  
  
      try{
          camera.setParameters(parameters);
      }
      catch (RuntimeException ex){
          Log.d("Preview", "Failure to set proper parameters");
          //need to improve this method
          if (mSupportedPreviewSizes != null) {
              Camera.Size cs = mSupportedPreviewSizes.get(0);
              parameters.setPreviewSize(cs.width, cs.height);
              camera.setParameters(parameters);
              requestLayout();
              //int tempheight = mPreviewSize.height;
              //mPreviewSize.height = mPreviewSize.width;
              //mPreviewSize.width = tempheight;
  
          }
      }
  </code></pre>
  
  <p>This is called when you switch between cameras(back facing to front facing vice-versa). Could this be interfering with the orientation event listener?</p>
  
  <p>Also, when saving the picture taken, this is what I call. Before it was just taking in data as a parameter, but I tried to make it take screen orientation as well. The problem is the screen orientation is always 90, no matter what. So the bitmap will always be rotated by 90 degrees(which is great for taking photos with the back camera) but inverts it when taking it with the front camera. Could a fix be implemented in this function?</p>
  
  <pre><code>  public static Bitmap MakeSquare(byte[] data, int orientation) {
      int width;
      int height;
      // Rotate photo
      Matrix matrix = new Matrix();
      matrix.postRotate(orientation);
      // Convert ByteArray to Bitmap
      Bitmap bitPic = BitmapFactory.decodeByteArray(data, 0, data.length);
      width = bitPic.getWidth();
      height = bitPic.getHeight();
  
  
      // Create new Bitmap out of the old one
      Bitmap bitPicFinal = Bitmap.createBitmap(bitPic, 0, 0, width, height,matrix, true);
      bitPic.recycle();
      int desWidth;
      int desHeight;
      desWidth = bitPicFinal.getWidth();
      desHeight = desWidth;
      Bitmap croppedBitmap = Bitmap.createBitmap(bitPicFinal, 0,bitPicFinal.getHeight() / 2 - bitPicFinal.getWidth() / 2,desWidth, desHeight);
      croppedBitmap = Bitmap.createScaledBitmap(croppedBitmap, 528, 528, true);
      return croppedBitmap;
  }
  </code></pre>