2011-07-28 72 views
9

我見過類似的問題,我有,但我仍然無法找到解決方案。我對Android開發非常陌生,所以我無法跟蹤我的問題。無論如何,我正嘗試使用我創建的CameraSurfaceView類創建相機預覽,該類可以擴展SurfaceView並實現SurfaceHolder.Callback。無論我在StartCamera類中嘗試什麼,都不會調用surfaceCreated()方法,因此我的相機永遠不會啓動。任何幫助將是偉大的,謝謝!surfaceCreated()從來沒有叫

StartCamera.java

import net.peterd.zombierun.R; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.widget.FrameLayout; 

public class StartCamera extends BaseActivity { 

    private Camera mCamera; 
    private CameraSurfaceView mView; 

    public void onCreate(Bundle state) { 
     // TODO Auto-generated constructor stub 
     super.onCreate(state); 
     setContentView(R.layout.start_camera); 
    } 

    public void onResume() { 
     super.onResume(); 

     mView = new CameraSurfaceView(this); 
     FrameLayout preview = (FrameLayout) findViewById(R.id.cPreview); 
     preview.addView(mView); 
    } 

    public void onPause() { 
     mCamera.stopPreview(); 
     mCamera.release(); 
    } 

} 

CameraSurfaceView.java

import java.io.IOException; 
import android.content.Context; 
import android.hardware.Camera; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback 
{ 
     private SurfaceHolder holder; 
     private Camera camera; 

     public CameraSurfaceView(Context context) 
     { 
       super(context); 

       //Initiate the Surface Holder properly 
       this.holder = this.getHolder(); 
       this.holder.addCallback(this); 
       this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     } 

     @Override 
     public void surfaceCreated(SurfaceHolder holder) 
     { 
       try 
       { 
         //Open the Camera in preview mode 
         this.camera = Camera.open(); 
         this.camera.setPreviewDisplay(this.holder); 
       } 
       catch(IOException ioe) 
       { 
         ioe.printStackTrace(System.out); 
       } 
     } 

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

     @Override 
     public void surfaceDestroyed(SurfaceHolder holder) 
     { 
       // Surface will be destroyed when replaced with a new screen 
       //Always make sure to release the Camera instance 
       camera.stopPreview(); 
       camera.release(); 
       camera = null; 
     } 

     public Camera getCamera() { 
      return camera; 
     } 
} 

start_camera.xml

<?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/cPreview" 
     android:layout_weight="1" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    </FrameLayout> 

</LinearLayout> 

回答

6

顯然,這是由於FrameLayout裏是空的,因此不會產生任何SurfaceView的。您可以嘗試手動強制設置父LinearLayout的高度和寬度,或者只需在FrameLayout中添加一個imageView。由於FrameLayout可能包含很多小部件,但它只顯示最後一個小部件,所以您可以將一個ImageView放入FrameLayout中,只顯示一個小圖像用於創建SurfaceView。

<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/cPreview" 
    android:layout_weight="1" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<!-- Adding small dummy image --> 
<ImageView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    src="@drawable/small_picture"> 

</FrameLayout> 

</LinearLayout> 
+1

我遇到同樣的問題。有誰能解決這個問題嗎 –

0

我有錯誤,從該線路CameraSurfaceView方法,surfaceChanged

camera.setParameters(parameters); 

它工作得很好,一旦我評論說出來。

1

@jvnane我試過你的代碼,它的工作原理和小錯誤。

surfaceChanged被調用沒有問題。你認爲它不起作用的方式?嘗試添加調試消息,並看到它在logcat中:

@Override 
public void surfaceCreated(final SurfaceHolder holder) 
{ 
    Log.i("Camera Test", "It works!"); 
    try 
    { 
     //Open the Camera in preview mode 
     this.camera = Camera.open(); 
     this.camera.setPreviewDisplay(this.holder); 
    } 
    catch(final IOException ioe) 
    { 
     ioe.printStackTrace(System.out); 
    } 
} 

小錯誤:Camera對象是在surfaceDestroyed釋放,所以你並不需要在活動的再次釋放。另外,重寫onPause()方法時需要撥打super.onPause()

5

我有同樣的問題。對我而言,這是因爲視圖大小被設置爲0,因此表面從未被創建過。我通過設置一個特定的大小來檢查它,並且突然間調用了回調函數。 (同樣,如果視圖是在屏幕外佈置的話,顯然也是如此)。