对于Camera App,SurfaceView在前景时会变黑

时间:2012-05-30 07:32:19

标签: android android-camera android-capture

我在我的应用中使用相机功能。一切正常但是当设备被锁定/睡眠时,在返回应用程序时,相机部分(SurfaceView)只是黑色。以下是我的代码。有人可以确定问题吗?

public class Activity_Camera extends Activity implements SurfaceHolder.Callback, Camera.AutoFocusCallback, Observer
{
    // Surface vars
    private SurfaceView preview;
    private SurfaceHolder previewHolder;

    // Camera vars
    private Camera mCamera;
    private boolean mPreviewRunning = false;
    private boolean mCaptureFrame = false;
    private int frame_number = 1;
    private byte[] frame = new byte[1];

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        preview = (SurfaceView) findViewById(R.id.preview);
        previewHolder = preview.getHolder();
        previewHolder.addCallback(this);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (mPreviewRunning)
                 {
                     mCamera.stopPreview();
                     mPreviewRunning = false;
                 }
                 mCamera.setPreviewCallback(null); 
                 mCamera.release();
    }



    // implements SurfaceHolder.Callback
    public void surfaceCreated(SurfaceHolder holder)
    {
        mCamera = Camera.open();
    }

    // implements SurfaceHolder.Callback
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        mPreviewRunning = false;
    }

    // implements Camera.AutoFocusCallback
    public void onAutoFocus(boolean success, Camera camera)
    {

    }

    PreviewCallback previewCallback = new PreviewCallback()
    {
        public void onPreviewFrame(byte[] data, Camera camera)
        {

        }
    };

    // implements SurfaceHolder.Callback
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        if (mPreviewRunning)
        {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
        p.setPreviewSize(640, 480);
        mCamera.setParameters(p);

        try
        {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        mCamera.setPreviewCallback(previewCallback);

        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation)
        {
        case Surface.ROTATION_0:
            degrees = 90;
            break;
        case Surface.ROTATION_90:
            degrees = 0;
            break;
        case Surface.ROTATION_180:
            degrees = 270;
            break;
        case Surface.ROTATION_270:
            degrees = 180;
            break;

        }
        Log.i("DEGREES ARE WHAT??", Integer.toString(degrees));

//      mCamera.setDisplayOrientation(degrees);
        mCamera.setDisplayOrientation(90);

        mCamera.startPreview();
        mPreviewRunning = true;

    }

    // implements Observer
    // captures a frame when the compass listener says it is appropriate
    public void update(Observable observable, Object data)
    {
        mCaptureFrame = true;
    }
}

请确定问题,因为我怀疑onPause()有问题,当我在黑色相机屏幕后按回按钮时,它也会让我异常:

java.lang.RuntimeException: Method called after release()

在这一行:

 mCamera.setPreviewCallback(null); // (in onPause())

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#E6E5E6">
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal">
        <TextView 
            android:id="@+id/tv_camera_url"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:textColor="@android:color/black"
            android:text="URL goes here........"
            android:layout_marginTop="3dp"
            android:textStyle="bold"/>
        <TextView 
            android:id="@+id/tv_speaknow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:background="#ffffff"
            android:layout_marginTop="3dp"
            android:text="Speak Now"
            android:textColor="#ff0000"
            android:textStyle="bold"/>
    </LinearLayout>
    <RelativeLayout
        android:id="@+id/rl_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <SurfaceView
            android:id="@+id/preview"
            android:layout_width="480px"
            android:layout_height="640px"
            android:layout_alignParentBottom="true"/>
        <Button
            android:id="@+id/btn_take_picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/camera"
            android:layout_alignBottom="@+id/preview"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp"/>
    </RelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

如果是第一次启动应用程序,onPause真的不应该是问题。 但我会将创作转移到onResume而不是onCreate。

我可以看到你的表面视图的xml吗?

你想要一张照片吗?

if(mPreviewRunning)     {         mCamera.stopPreview();     }

大多数人从onResume开始预览。试着把它移到那里..