Android相机:应用程序传递NULL表面

时间:2015-05-19 21:16:34

标签: java android android-camera surfaceview surfaceholder

我发现了几个问题,但没有答案,所以希望有人可能有一些见解。当我尝试交换相机时,我调用下面的swapCamera功能。然而,相机预览只是冻结(虽然只是实时相机预览,但应用程序并未冻结。)

当我第一次打开应用程序时,一切正常。但是我注意到一些有趣的事当我注销_surfaceHolder对象的memoryaddress(即我的SurfaceHolder对象)时,它给了我一个值,但每当我在应用程序完成启动后查询该值时,该内存地址都已更改。

此外,当我换掉相机时,它给我的错误非常混乱。我将_surfaceHolder注销到_camera.setPreviewDisplay(_surfaceHolder);中的相机之前 并且在传入之前它不为空。

非常感谢任何帮助。

我注意到了一些有趣的行为

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
    private SurfaceHolder _surfaceHolder;
    private Camera _camera;
    boolean _isBackFacing;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        _camera = camera;
        _isBackFacing = true;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        _surfaceHolder = getHolder();
        _surfaceHolder.addCallback(this);
    }

    void refreshCamera()
    {
        try {
            _camera.setPreviewDisplay(_surfaceHolder);
            _camera.startPreview();
        } catch (IOException e) {
            Log.d("iCamera", "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceCreated(SurfaceHolder holder)
    {
//        The Surface has been created, now tell the camera where to draw the preview.
        refreshCamera();
    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
        // empty. Take care of releasing the Camera preview in your activity.
        _surfaceHolder.removeCallback(this);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
    {
         // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (_surfaceHolder.getSurface() == null){
            // preview surface does not exist
            return;
        }

        try {
            _camera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes her
        _camera.setDisplayOrientation(90);

        // _startPoint preview with new settings
        refreshCamera();
    }

    public void swapCamera()
    {
        Camera cam = null;
        int cameraCount = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        _camera.stopPreview();
        _camera.release();
        for (int i = 0; i < cameraCount; i++)
        {
            Camera.getCameraInfo(i,cameraInfo);
            if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT && _isBackFacing == true)
            {
                try
                {
                    _camera = Camera.open(i);

                }catch (RuntimeException e)
                {
                    Log.e("Error","Camera failed to open: " + e.getLocalizedMessage());
                }
            }

            if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK && _isBackFacing == false)
            {
                try
                {
                    _camera = Camera.open(i);
                }catch (RuntimeException e)
                {
                    Log.e("Error","Camera failed to open: " + e.getLocalizedMessage());
                }
            }
        }

        _isBackFacing = !_isBackFacing;
        refreshCamera();
    }
}

1 个答案:

答案 0 :(得分:2)

因此经过多次调试和挖掘我发现的罪魁祸首是onResume函数。

在其中,我正在“刷新”相机变量,以防它在上下文切换之间丢失。

public void onResume()
{
    super.onResume();
    _cameraPreview = new CameraPreview(getActivity());
}

这导致我的surfaceHolder重新创建。我不确定为什么会导致null,但我认为因为我创建了一个SurfaceHolder的新实例,内部Android代码保留了对旧(现在为空)SurfaceHolder的引用。通过从onResume删除我的'刷新'(即重新实例化)调用,问题得到解决。

该错误具有误导性,我认为因为它表示传递了null表面,但那是因为我认为它保留了对null surfaceHolder的引用,即使你创建了一个新的并传递了它(它似乎使用OLD现在为null一个反正)。因此,如果您收到此错误,请检查您是否正在重新创建surfaceHolder并将其传入。