Camera和SurfaceView的奇怪行为

时间:2013-12-29 10:16:34

标签: android camera surfaceview

我对使用SurfaceView感到疯狂。

我开发了一款以固定时间间隔拍照的应用。它适用于我的Androd 2.3设备。以下是示例代码:

public class MainActivity extends Activity 
{
    private Camera mCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                 takeAPicture();
              }
            }
        );
    }


    private PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) 
        {
            // ... save image ...

            camera.release();
            camera = null;
        }
    };

    private void takeAPicture()
    {
        mCamera = getCameraInstance();

        mCamera.takePicture(null, null, mPicture);        
    }
}

相同的代码不适用于Android 4.1。谷歌搜索了一下我发现"大多数现代Android设备现在检查SurfaceView是否存在并且具有非零的宽度和高度"。所以我修改了上面的代码如下:

public class MainActivity extends Activity 
{
    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                takeAPicture();
                }
            }
        );
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) 
        {
            // ... save image ...

            camera.release();
            camera = null;
        }
    };

    private void takeAPicture()
    {
        mCamera = getCameraInstance();

        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);

        mCamera.takePicture(null, null, mPicture);        
    } 
}

CameraPreview是:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) 
    {
        super(context);

        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) 
    {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(MainActivity.TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

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

    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 (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

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

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e){
            Log.d(MainActivity.TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

但是没有调用PictureCallback并且应用程序的用户界面消失了。用户界面只是一个FrameLayout和一个按钮,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="100dp"
    android:layout_height="100dp" />

  <Button
    android:id="@+id/button_capture"
    android:text="Capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

出了什么问题?

请注意,如果我这样做:

public class MainActivity extends Activity 
{
private Camera mCamera;
private CameraPreview mPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCamera = getCameraInstance();

    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 mCamera.takePicture(null, null, mPicture);
            }
        }
    );
}

当然从PictureCallback中删除mCamera.release(),它在使用Android 4.1的设备上完美运行。任何意见?

1 个答案:

答案 0 :(得分:0)

点击后,你创建一个CameraPreview的实例 - 这很好,但需要时间和一些回调。在 SurfaceView 准备就绪之前,您无法致电mCamera.takePicture() - 这不会花费很长时间,但您仍需要延迟。最简单的更改是从CameraPreview.surfaceCreated()调用 mCamera.takePicture()