我的相机应用预览喜欢风景

时间:2016-07-12 20:08:54

标签: android

我正在使用简单的相机预览作为我的一项活动的背景。我的问题是相机预览总是风景。我有以下内容,似乎没有任何效果。

清单文件

android:configChanges="orientation"

Actitity_menu布局文件

<LinearLayout
...
    <SurfaceView
        android:screenOrientation="portrait"

菜单Activity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
                           int height) {
    if(previewing){
        camera.stopPreview();
        previewing = false;
    }

    if (camera != null){
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
            previewing = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
    previewing = false;
}

0 个答案:

没有答案