相机预览显示黑屏

时间:2013-12-16 11:49:36

标签: android android-camera surfaceview

您好我正在开发一款试用自定义相机和视频功能的Android应用。问题是当我在Videoview中尝试返回Cameraview onClick按钮时,不会显示预览。它只显示黑屏。

在Videoview中点击ont

       try 
        {
            Thread.sleep(1000);
            recorder.stop();
            recorder.reset();
            recorder.release();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        camera.release();
        startActivity(new Intent(Video.this,Camera.class));
        finish();

从Videoview返回时,相机在Cameraview的SurfaceChanged上为null。

@Override
public void surfaceChanged(SurfaceHolder sHolder, int format, int width, int height)
{
    Log.e("surfaceChanged","camera");
    if(camera != null)
    {
        initPreview(width, height);
        camera.startPreview();
    }


    else if(camera == null)
    {
      try
        {
           Camera camera = Camera.open(currentCameraId);

            if (holder.getSurface() == null)
            {
                return;
            }

            try
            {
                     //camera.stopPreview();

                    //camera.reconnect();
                    //camera.lock();
                    //camera.unlock();
                camera.setPreviewCallback(this);
                camera.setPreviewDisplay(holder);

            }
            catch (Exception e)
            {
            }

            initPreview(width, height);
            camera.startPreview();

        }

        catch (Exception e)
        {
            releaseCamera();
        }

不确定我哪里出错了。我无法打开相机或重新连接。请帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

1)为SurfaceView设置callBack。

public CameraView(Context ctx, AttributeSet attrSet) {
        super(ctx, attrSet);
        getHolder().addCallback(cameraPreviewCallback);
        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

Camera.PreviewCallback cameraPreviewCallback = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {

        }
    };

2)启动相机

camera = Camera.open();
camera.setPreviewCallback(cameraPreviewCallback);   
camera.setPreviewDisplay(getHolder());
camera.startPreview();