禁用方向改变

2024-01-12

重新启动定制Camera,同时进行更改预览模式, from 风景转肖像 or 纵向转横向,我的 Surface 类代码如下所示:

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
        }           
    }

}

我可以知道我做错了什么,我在代码中错过了什么吗?


您必须处理应用程序的配置更改。

将此行添加到您的 AndroidManifest.xml 中。

android:configChanges="keyboardHidden|orientation|screenSize"

这告诉系统您将自己处理哪些配置更改 - 在本例中什么都不做。

希望有帮助ツ

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

禁用方向改变 的相关文章

随机推荐