简体   繁体   中英

Using Camera in Landscape Mode getting Preview in Portrait

Whenever i use my Camera in Landscape Mode getting View in Portrait, I prepared two different layout for Portrait and Landscape Modes.

like still i am getting Preview in Portrait Mode not matter i am using Camera in Landscape or in Portrait Mode, but once i remove this line: mCamera.setDisplayOrientation(90); i am getting Preview in Landscape mode, either i use Camera in Landscape or in Portrait Mode

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 {       
                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) {

                // start preview with new settings
                try {
                mCamera.setDisplayOrientation(90);
                mCamera.setPreviewDisplay(surfaceHolder);
                mCamera.startPreview();
                } catch (Exception e) {
                // intentionally left blank for a test
            }
        }

I suggest you to use : Configuration.ORIENTATION_LANDSCAPE to support Landscape Preview

Edit Answer:

@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);
            }
                 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);
                 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
        }           
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM