android - 禁用方向更改

标签 android camera android-camera

重新启动自定义相机,同时更改预览模式,从风景到肖像肖像到风景,我的 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"

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

希望能帮到你

关于android - 禁用方向更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20603635/

相关文章:

android - 如何在我的应用程序中使用 Google map 获取路线

java - 在 Android 中的 AlertDialog 中按下 OK 时返回 MainActivity

android - Android 上的 Google+ 登录按钮在点击时没有任何反应

python - Pygame 相机跟随 2d 平铺游戏

如果手机处于横向模式,Android 相机预览方向不正确

android - Android TV 方向键的 KeyEvent

javascript - 在 Cordova 使用相机?

ios - 使用 Swift 在 iOS 中远程控制事件

java - 打开带有相机选项的图像选择器

java - View 太大,无法放入绘图缓存