我看不到表面视图

时间:2012-05-27 18:30:19

标签: android android-camera surfaceview

我正在尝试在Android中预览相机,但是当我运行它时,应用程序只显示一个黑色窗口,其中tittle和表面视图大小为(0px,0px)

这是我的活动:

package rolilink.test.android;
import java.io.IOException;

import android.hardware.Camera;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class CameraView extends Activity{
    private SurfaceView surface;
    private Camera camera;
    private Button stopbutton;
    protected void onCreate(Bundle SavedStateInstance){
        super.onCreate(SavedStateInstance);
        setContentView(R.layout.cameraview);
        Log.i("LogCamera", "Setteo el view");
        this.getUi();
        this.setListeners();
        try {
            this.startCameraPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private void getUi(){
        this.surface=(SurfaceView) findViewById(R.id.CameraSurface);
        this.stopbutton=(Button) findViewById(R.id.btnStopPreview);
    }

    private void setListeners(){
        this.stopbutton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
               finishWithCameraPreview();
            }
        });
    }

    private void startCameraPreview() throws IOException{
        this.camera= Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        this.camera.setParameters(parameters);
        SurfaceHolder holder = this.surface.getHolder();
        this.camera.setPreviewDisplay(holder);
        camera.startPreview();
        Log.i("LogCamera", "Size of SurfaceView:"+this.surface.getHeight()+"x"+this.surface.getWidth());
    }

    private void finishWithCameraPreview(){
        this.camera.stopPreview();
        this.camera.release();

    }

}

和我的布局相机视图:

<?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:gravity="center">
    <SurfaceView 
        android:id="@+id/CameraSurface"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/btnStopPreview"
        android:text="Stop Preview"
        android:layout_marginTop="2dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

0 个答案:

没有答案