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 8558353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:47:33+00:00 2026-06-11T15:47:33+00:00

I am facing an issue in my app as when I start recording through

  • 0

I am facing an issue in my app as when I start recording through camera it tilts to landscape which is not the requirement. The requirement is Recording should start according the the orientation of the Device. Means if device is in portrait mode then recording should start in Portrait and If camera in landscape mode then recording will atart in landscape.

But in our case when we click on start recording button then camera orientation will automaticaly changed to Landscape but Device still with the portrait orientation.

So, Please suggest any solution regarding that.

Code:

package com.irant.cameraApplication;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.media.MediaRecorder.VideoEncoder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.Toast;

import com.irant.a1techno.CameraPlay;
import com.irant.a1techno.R;

/***
 *  TODO: 1. sound on/off
 *  2. resolution change
 * @author roman10
 *
 */

public class VideoCapture_New extends Activity implements SurfaceHolder.Callback {
    private SurfaceView prSurfaceView;
    private ImageButton prStartBtn, prFrontBackCamera;
    private Button prSettingsBtn;
    public String TAG = "IRANT";
    private boolean prRecordInProcess;
    private SurfaceHolder prSurfaceHolder;
    private Camera prCamera;
    private final String cVideoFilePath = "/sdcard/";
    Chronometer cm_VideoCapture;
    private Context prContext;
    public static boolean frontCamera= false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        prContext = this.getApplicationContext();
        setContentView(R.layout.videocapture_new);
        Utils.createDirIfNotExist(cVideoFilePath);

        cm_VideoCapture = (Chronometer)findViewById(R.id.cm_VideoCapture);
        prSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        prStartBtn = (ImageButton) findViewById(R.id.main_btn1);
        prFrontBackCamera = (ImageButton)findViewById(R.id.btn_frontBackCamera);

       // prSettingsBtn = (Button) findViewById(R.id.main_btn2);
        prRecordInProcess = false;
        prStartBtn.setOnClickListener(new View.OnClickListener() {
            //@Override
            public void onClick(View v) {
                if (prRecordInProcess == false) {
                    startRecording();
                    cm_VideoCapture.start();
                } else {
                    stopRecording();
                    cm_VideoCapture.stop();
                    Intent intent = new Intent(VideoCapture_New.this, CameraPlay.class);
                    startActivity(intent);
                }
            }
        });

        prFrontBackCamera.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //prCamera = openFrontFacingCameraGingerbread();
                try{
            if(prCamera.getNumberOfCameras()==2)
            {
                if(frontCamera)
                {
                    frontCamera = false;
                    prCamera.stopPreview();
                    prMediaRecorder.release();
                    prMediaRecorder = null;
                    prCamera.release();
                    prCamera = null;
                }
                else
                {

                    frontCamera = true;
                    prCamera.stopPreview();
                    prMediaRecorder.release();
                    prMediaRecorder = null;
                    prCamera.release();
                    prCamera = null;
                }
                Intent intent = new Intent(VideoCapture_New.this, VideoCapture_New.class);
                startActivity(intent);
            }
            else
            {
                Toast.makeText(VideoCapture_New.this, "Your device doesn't contain Front Camera.", Toast.LENGTH_SHORT).show();
            }
                }catch(Exception e)
                {
                    e.printStackTrace();
                    Toast.makeText(VideoCapture_New.this, "Your device is not compatible for Front Camera.", Toast.LENGTH_SHORT).show();

                }

            }
        });

        prSurfaceHolder = prSurfaceView.getHolder();
        prSurfaceHolder.addCallback(this);
        prSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        prMediaRecorder = new MediaRecorder();
    }

    private Camera openFrontFacingCameraGingerbread() 
    {
         Camera camera = null;

            // Look for front-facing camera, using the Gingerbread API.
            // Java reflection is used for backwards compatibility with pre-Gingerbread APIs.
            try {
                Class<?> cameraClass = Class.forName("android.hardware.Camera");
                Object cameraInfo = null;
                Field field = null;
                int cameraCount = 0;
                Method getNumberOfCamerasMethod = cameraClass.getMethod( "getNumberOfCameras" );
                if ( getNumberOfCamerasMethod != null ) {
                    cameraCount = (Integer) getNumberOfCamerasMethod.invoke( null, (Object[]) null );
                }
                Class<?> cameraInfoClass = Class.forName("android.hardware.Camera$CameraInfo");
                if ( cameraInfoClass != null ) {
                    cameraInfo = cameraInfoClass.newInstance();
                }
                if ( cameraInfo != null ) {
                    field = cameraInfo.getClass().getField( "facing" );
                }
                Method getCameraInfoMethod = cameraClass.getMethod( "getCameraInfo", Integer.TYPE, cameraInfoClass );
                if ( getCameraInfoMethod != null && cameraInfoClass != null && field != null ) {
                    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
                        getCameraInfoMethod.invoke( null, camIdx, cameraInfo );
                        int facing = field.getInt( cameraInfo );
                        if ( facing == 1 ) { // Camera.CameraInfo.CAMERA_FACING_FRONT 
                            try {
                                Method cameraOpenMethod = cameraClass.getMethod( "open", Integer.TYPE );
                                if ( cameraOpenMethod != null ) {
                                    camera = (Camera) cameraOpenMethod.invoke( null, camIdx );
                                }
                            } catch (RuntimeException e) {
                                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                                camera= prCamera;
                            }
                        }
                    }
                }
            }
            // Ignore the bevy of checked exceptions the Java Reflection API throws - if it fails, who cares.
            catch ( ClassNotFoundException e        ) {Log.e(TAG, "ClassNotFoundException" + e.getLocalizedMessage());}
            catch ( NoSuchMethodException e         ) {Log.e(TAG, "NoSuchMethodException" + e.getLocalizedMessage());}
            catch ( NoSuchFieldException e          ) {Log.e(TAG, "NoSuchFieldException" + e.getLocalizedMessage());}
            catch ( IllegalAccessException e        ) {Log.e(TAG, "IllegalAccessException" + e.getLocalizedMessage());}
            catch ( InvocationTargetException e     ) {Log.e(TAG, "InvocationTargetException" + e.getLocalizedMessage());}
            catch ( InstantiationException e        ) {Log.e(TAG, "InstantiationException" + e.getLocalizedMessage());}
            catch ( SecurityException e             ) {Log.e(TAG, "SecurityException" + e.getLocalizedMessage());}
            catch ( NullPointerException e             ) {Log.e(TAG, "NullPointerException" + e.getLocalizedMessage());}

            if ( camera == null ) {
                // Try using the pre-Gingerbread APIs to open the camera.
                try {
                    camera = Camera.open();
                } catch (RuntimeException e) {
                    Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                }
            }

            return camera;
    }

    @Override
    public void surfaceChanged(SurfaceHolder _holder, int _format, int _width, int _height) {
        Camera.Parameters lParam = prCamera.getParameters();

        prCamera.setParameters(lParam);
        try {
            prCamera.setPreviewDisplay(_holder);
            prCamera.startPreview();
            //prPreviewRunning = true;
        } catch (IOException _le) {
            _le.printStackTrace();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        if(frontCamera == false)
        {
        prCamera = Camera.open();
        if (prCamera == null) {
            Toast.makeText(this.getApplicationContext(), "Camera is not available!", Toast.LENGTH_SHORT).show();
            finish();
        }
        }
        else if(frontCamera == true)
        {
            try{
         int cameraCount = 0;
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            cameraCount = Camera.getNumberOfCameras();
            for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
                Camera.getCameraInfo( camIdx, cameraInfo );
                if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
                    try {
                        prCamera = Camera.open( camIdx );
                    } catch (RuntimeException e) {
                        Log.i("Camera failed to open: ",e.getLocalizedMessage());
                    }
                }
            }
            }catch(Exception e)
            {
                Toast.makeText(VideoCapture_New.this, "Your Device doesn't compatible for Fron Camera.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        try {
            if (prRecordInProcess) {
                stopRecording();
            } else {
                prCamera.stopPreview();
            }
            prMediaRecorder.release();
            prMediaRecorder = null;
            prCamera.release();
            prCamera = null;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    private MediaRecorder prMediaRecorder;
    private final int cMaxRecordDurationInMs = 30000;
    private final long cMaxFileSizeInBytes = 5000000;
    private final int cFrameRate = 20;
    private File prRecordedFile;

    private void updateEncodingOptions() {
        if (prRecordInProcess) {
            stopRecording();
            startRecording();
            Toast.makeText(prContext, "Recording restarted with new options!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(prContext, "Recording options updated!", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean startRecording() {
        prCamera.stopPreview();
        try {
            prCamera.unlock();
            prMediaRecorder.setCamera(prCamera);

            prMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            String lVideoFileFullPath=".mp4";
            prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            String lDisplayMsg = "Current container format: ";
            prMediaRecorder.setVideoEncoder(VideoEncoder.H263);


            lDisplayMsg += "Current encoding format: ";

            lVideoFileFullPath = cVideoFilePath + "myvideo" + lVideoFileFullPath;
            //prMediaRecorder.setOrientationHint(360);
            prMediaRecorder.setVideoSize(176, 144); 
            prMediaRecorder.setVideoFrameRate(12);
            prRecordedFile = new File(lVideoFileFullPath);
            prMediaRecorder.setOutputFile(prRecordedFile.getPath());


            prMediaRecorder.setVideoFrameRate(cFrameRate);
            prMediaRecorder.setPreviewDisplay(prSurfaceHolder.getSurface());
            prMediaRecorder.setMaxDuration(cMaxRecordDurationInMs);
            prMediaRecorder.setMaxFileSize(cMaxFileSizeInBytes);
            //prepare for capturing
            //state: DataSourceConfigured => prepared
            prMediaRecorder.prepare();
            //start recording
            //state: prepared => recording
            prMediaRecorder.start();
            //prStartBtn.setText("Stop");
            prRecordInProcess = true;
            return true;
        } catch (IOException _le) {
            _le.printStackTrace();
            return false;
        }
    }

    private void stopRecording() {
        prMediaRecorder.stop();
        prMediaRecorder.reset();
        try {
            prCamera.reconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //prStartBtn.setText("Start");
        prRecordInProcess = false;
        prCamera.startPreview();
    }

    private static final int REQUEST_DECODING_OPTIONS = 0;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        switch (requestCode) {
        case REQUEST_DECODING_OPTIONS:
            if (resultCode == RESULT_OK) {
                updateEncodingOptions();
            }
            break;
        }
    }
}
  • 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-11T15:47:34+00:00Added an answer on June 11, 2026 at 3:47 pm

    I had the same problem and this little snipped fixed it

            Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
            Camera.getCameraInfo(0, info);
            int rotation = getWindowManager().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;
            }
    
            this.mRotation = result;
    
            mCamera.setDisplayOrientation(result);
    

    I am not sure why it doesn’t start at the current phone orientation but I am sure this fixes it.

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

Sidebar

Related Questions

I'm facing an issue which is caused by orientation magic I don't understand. First,
I'm currently facing the following issue: My app dynamically creates images (320 x 480
We have implemented webservice which generates xml response. I am facing issue while invoking
I am facing very strange issue. When I was running my app in iPhone
I am facing some strange issue in my app. in my ViewDidLoad() method I
I am facing a strange issue. I create a simple PhoenGap app, that loads
I am facing an strange issue. I developed java web app in java 6
I am developing a android app, i am facing force close issue when i
I have implemented ActionBar Tab in my app. But I am facing one issue
I am facing crash issue in native of my android code. My app is

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.