Disable orientation change

Re-initializing the user’s camera when changing the view mode , from Landscape to portrait or Portrait to strong> album , 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 } } } 

Can I find out where I am doing wrong, what I missed in my code?

+6
source share
2 answers

You need to make a configuration change for your application.

Add this line to your AndroidManifest.xml.

 android:configChanges="keyboardHidden|orientation|screenSize" 

This tells the system which configuration changes you intend to process yourself - in this case, do nothing.

Hope this helps ツ

+12
source

It is not clear from your question what you are complaining about, but you seem to only manipulate the camera parameters without ever calling mCamera.setParameters(parameters) . This in itself can cause confusion.

Using parameters.set("orientation", "landscape"); on devices that do not support this parameter, may raise a RuntimeException in mCamera.setParameters(parameters) . Therefore, we usually use a separate attempt ... bypass this setting.

0
source

All Articles