Android Camera.setPreviewDisplay() 抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1048739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:38:33  来源:igfitidea点击:

Camera.setPreviewDisplay() throws Exception

androidcamera

提问by Niko Gamulin

Possible Duplicate:
Android Camera will not work. startPreview fails

可能重复:
Android 相机将无法工作。开始预览失败

I am trying to set a camera preview in a custom SurfaceView but I get an exception each time I execute the initialization method.

我正在尝试在自定义 SurfaceView 中设置相机预览,但每次执行初始化方法时都会出现异常。

Below is the code for camera preview initialization:

下面是相机预览初始化的代码:

private void init(Context context)
{
    setFocusable(true);
    mRecording = false;
    fileRW = new FileReaderWriter();
    frameCount = 0;
    if(mCamera == null)
    {
        mCamera = Camera.open();
    }
    Parameters parameters = mCamera.getParameters();
    parameters.setPictureFormat(PixelFormat.JPEG);
    mCamera.setParameters(parameters);
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mCamera.startPreview();

}

the line mCamera.setPreviewDisplay(surfaceHolder);throws an exception (setPreviewDisplay failed) each time I try to execute the method.

每次我尝试执行该方法时,该行都会mCamera.setPreviewDisplay(surfaceHolder);引发异常 ( setPreviewDisplay failed)。

Does anyone know what could be wrong? I would really appreciate any of your help.

有谁知道可能有什么问题?我真的很感激你的任何帮助。

Thanks!`

谢谢!`

回答by Syndacate

I completely agree with Jon Bright

我完全同意乔恩·布莱特

I couldn't figure out what was going on for a week, I ignored the setType on the surface holder because the SDK said it was deprecated, ie.

我无法弄清楚发生了什么一周,我忽略了表面支架上的 setType 因为 SDK 说它已被弃用,即。

"This method is deprecated. this is ignored, this value is set automatically when needed."

“此方法已弃用。此方法被忽略,此值在需要时自动设置。”

But if you don't do that, it will crash on setPreview. This is running 1.5 SDK (I need it to be backwards compatible to that) on a Galaxy S with 2.1. So make sure you set the type. Not quite as automatic as the documentation makes it sound.

但是如果你不这样做,它会在 setPreview 上崩溃。这是在具有 2.1 的 Galaxy S 上运行 1.5 SDK(我需要它向后兼容)。所以一定要设置好类型。不像文档所说的那样自动。

回答by Garfield

The best place to call setPreviewDisplay()is in surfaceChanged()If the surface is just created, surfaceChanged will be called at least once and you can startPreview()and setPreviewDisplay there. If the surface changes and the preview already starts, you can stopPreview/setPreviewDisplay/startPreviewthere. Even if your app does not change the size of the surface, the framework may still unexpectedly call surfaceChanged()when the app starts or exits due to orientation changes. So your app really needs to handle surfaceChanged properly. You can trace the source code of camera application in Android for reference.

调用的最佳位置setPreviewDisplay()是在surfaceChanged() 中。如果刚刚创建了表面,则至少会调用一次startPreview()SurfaceChanged ,您可以在那里 setPreviewDisplay。如果表面发生变化并且预览已经开始,您可以stopPreview/setPreviewDisplay/startPreview在那里。即使你的应用没有改变表面的大小,surfaceChanged()由于方向改变,框架仍然可能在应用启动或退出时意外调用。所以你的应用真的需要正确处理surfaceChanged。您可以在Android中跟踪相机应用程序的源代码以供参考。

The code snippet in another answer works if surfaceChanged()is only called once in the app lifecycle.

如果surfaceChanged()在应用程序生命周期中仅调用一次,则另一个答案中的代码段有效。