Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7837299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:34:01+00:00 2026-06-02T14:34:01+00:00

I have this app that is running in portrait mode and as a part

  • 0

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.

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.

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.

Is there anyway this can be fixed so the picture taken is the same as the one you see in the preview?

        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");

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.

   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;

        }
    }

This is called when you switch between cameras(back facing to front facing vice-versa). Could this be interfering with the orientation event listener?

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?

  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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:34:03+00:00Added an answer on June 2, 2026 at 2:34 pm

    Well well. It seems I have kind of taken care of it. I used the advice from this: Android, front and back camera Orientation , Landscape

    And changed my MakeSquare function to this:

    public static Bitmap MakeSquare(byte[] data, int cameraID) {
        int width;
        int height;
        Matrix matrix = new Matrix();
        Camera.CameraInfo info = new Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraID, info);
        // Convert ByteArray to Bitmap
        Bitmap bitPic = BitmapFactory.decodeByteArray(data, 0, data.length);
        width = bitPic.getWidth();
        height = bitPic.getHeight();
    
        // Perform matrix rotations/mirrors depending on camera that took the photo
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
        {
            float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
            Matrix matrixMirrorY = new Matrix();
            matrixMirrorY.setValues(mirrorY);
    
            matrix.postConcat(matrixMirrorY);
        }
    
        matrix.postRotate(90);
    
    
        // 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;
    }
    

    This seems to work and do the trick. Although I am not sure this was the best way, I am content with it. Now all I have to do is figure out why its not taking in the proper aspect ratio when using the front camera.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this RoR app that calls to RAILS_ROOT. When running it using rails
I have an app that uploads an image to Facebook. this is the part
I have this web app that is running ASP .NET MVC 1.0 with LINQ
This morning I tried running a Silverlight 5 App that we have been building
I copied this class from a similar class I have in this app that
I have this c# web app that relies heavily on two dll files that
I have this method in App Delegate that makes a window and content view
I have a simple GAE app that includes a login/logout link. This app is
I have this iPad app using Storyboard. There are some file that are marked
I have an App that downloads and displays images from URL's. This works fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.