获取Android相机预览

时间:2014-07-13 16:26:29

标签: android android-camera

我试图制作一个非常简单的相机应用程序,我在这里跑进了一堵砖墙,因为相机预览没有显示出来。我遵循了开发者指南:http://developer.android.com/training/camera/cameradirect.html

这是我的代码:

package com.zygotelabs.gallileo;

import android.app.Activity;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity{

    private Camera mCamera;
    private Preview mPreview;

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        mPreview = new Preview(this);
        //task.execute();
        if (safeCameraOpen()){
             Log.e(getString(R.string.app_name), "Camera object is valid, starting preview");
             mPreview.setCamera(mCamera);
        }
    }


     private boolean safeCameraOpen() {
            boolean qOpened = false;

            try {

                releaseCameraAndPreview();
                mCamera = Camera.open();
                qOpened = (mCamera != null);
                Log.e(getString(R.string.app_name), "Camera has been opened");

            } catch (Exception e) {
                Log.e(getString(R.string.app_name), "failed to open Camera");
                e.printStackTrace();
            }

            return qOpened;    
        }

        private void releaseCameraAndPreview() {
            mPreview.setCamera(null);
            if (mCamera != null) {
                mCamera.release();
                mCamera = null;
            }
        }

}

和PreviewClass

public class Preview extends ViewGroup implements SurfaceHolder.Callback {

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;
    Camera mCamera;
    Size mPreviewSize;
    List<Size> mSupportedPreviewSizes;

    Preview(Context context) {
        super(context);

        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);
        //mSurfaceView = (SurfaceView) findViewById(R.id.surfaceViewCameraPreview);
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
        requestLayout();
        mCamera.setParameters(parameters);

        // Important: Call startPreview() to start updating the preview surface.
        // Preview must be started before you can take a picture.
        mCamera.startPreview();
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }


    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
        // TODO Auto-generated method stub

    }

    public void setCamera(Camera camera) {
        if (mCamera == camera) { return; }

        stopPreviewAndFreeCamera();

        mCamera = camera;

        if (mCamera != null) {
            List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();
            mSupportedPreviewSizes = localSizes;
            requestLayout();

            try {
                mCamera.setPreviewDisplay(mHolder);
            } catch (IOException e) {
                Log.e("CameraPreview", "Error setting up the preview display");
                e.printStackTrace();
            }

            // Important: Call startPreview() to start updating the preview
            // surface. Preview must be started before you can take a picture.
            Log.e("CameraPreview", "Camera preview starting");
            mCamera.startPreview();
        }
    }


    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        if (mCamera != null) {
            // Call stopPreview() to stop updating the preview surface.
            mCamera.stopPreview();
        }
    }

    /**
     * When this function returns, mCamera will be null.
     */
    private void stopPreviewAndFreeCamera() {

        if (mCamera != null) {
            // Call stopPreview() to stop updating the preview surface.
            mCamera.stopPreview();

            // Important: Call release() to release the camera for use by other
            // applications. Applications should release the camera immediately
            // during onPause() and re-open() it during onResume()).
            mCamera.release();

            mCamera = null;
        }
    }

}

现在,我对SurfaceHolder感到有点困惑。目前我刚刚创建了一个非常简单的布局,我在包含surfaceView的活动中使用。但是我目前的代码似乎添加了一个新的SurfaceView,但它应该出现在哪里?如果我尝试分配我已经存在的surfaceView,我会得到一个nullpointer异常,但是使用我正在使用的代码我得到&#34; app传递了NULL surface&#34;。所以我猜这里的surfaceView有什么问题吗?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

Here,使用此Preview extends ViewGroup模式。但请注意:他们将Preview设置为Activity的主要(内容)视图:

onCreate() {
   ...
   setContentView(mPreview);
}

您也可以这样做,或addContentView(mPreview)。是的,在这种情况下,您不需要在布局XML中定义SurfaceView

或者,您可以使用属于布局的SurfaceView。但在这种情况下,Preview类不应扩展 ViewGroup,而只能实现 SurfaceHolder.Callback

在这种情况下,您仍然拥有mSurfaceView;成员,构造函数如下:

Preview(Activity parentActivity) {

    mSurfaceView = (SurfaceView) parentActivity.findViewById(R.id.surfaceViewCameraPreview);

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

此处surfaceViewCameraPreview应该是main_layout的孩子。