Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Orientation Change

Re-Initiating Custom Camera, while doing change in Preview modes, from Landscape to Portrait or Portrait to Landscape, my Surface class code looks like this:

PreviewSurface.java:-

public class PreviewSurface extends SurfaceView implements
SurfaceHolder.Callback {

    public static final String LOG_TAG = "CameraPreview";
    private SurfaceHolder mSurfaceHolder;

    private Camera mCamera;

    // Constructor that obtains context and camera
    @SuppressWarnings("deprecation")
    public PreviewSurface(Context context, Camera camera) {
        super(context);
        this.mCamera = camera;

        this.mSurfaceHolder = this.getHolder();
        this.mSurfaceHolder.addCallback(this);
        this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        this.mSurfaceHolder.setFixedSize(100, 100);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        try {       
            Camera.Parameters parameters = mCamera.getParameters();
            if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) 
            {
                 parameters.set("orientation", "portrait");
                 mCamera.setDisplayOrientation(90);
                 parameters.setRotation(90);
                 mCamera.setPreviewDisplay(surfaceHolder);
                 mCamera.startPreview();
            }
            else 
            {
                 // This is an undocumented although widely known feature
                 parameters.set("orientation", "landscape");
                 // For Android 2.2 and above
                 mCamera.setDisplayOrientation(0);
                 // Uncomment for Android 2.0 and above
                 parameters.setRotation(0);
            }
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();

        } catch (IOException e) {
            // left blank for now
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mCamera.stopPreview();
        mCamera.release();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
            int width, int height) {

        try {       
            Camera.Parameters parameters = mCamera.getParameters();
            if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                 parameters.set("orientation", "portrait");
                 mCamera.setDisplayOrientation(90);
                 parameters.setRotation(90);

            }
                 else {
                      // This is an undocumented although widely known feature
                      parameters.set("orientation", "landscape");
                      // For Android 2.2 and above
                      mCamera.setDisplayOrientation(0);
                      // Uncomment for Android 2.0 and above
                      parameters.setRotation(0);
            }
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();

        } catch (IOException e) {
            // left blank for now
        }           
    }

}

May i know where i am doing wrong, what i missed in my code ?

like image 316
Sun Avatar asked Dec 16 '13 04:12

Sun


People also ask

How do I lock my screen orientation?

Whether you are holding your device upwards or sideways, swipe down from the top of the screen. Tap the Auto rotate icon to lock your device in your desired position. Remember, Portrait is when the device is upwards, and Landscape is when the device is sideways.

How do I stop my screen from automatically rotating?

Tap the Settings icon to open the Settings app. Scroll down and tap Accessibility. Scroll down to Interaction controls and tap Auto-rotate screen to turn it off.

How do I lock my app rotation?

If you want to lock the screen orientation in your app, you can set it individually on each platform. On Android: You could open MainActivity. cs under Platforms/Android, then change the the orientation attribute.


1 Answers

You have to handle configuration change for your app .

Add this line to your AndroidManifest.xml.

android:configChanges="keyboardHidden|orientation|screenSize"

This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.

Hope it helps ツ

like image 172
SweetWisher ツ Avatar answered Sep 19 '22 14:09

SweetWisher ツ