2011-06-04 36 views
2

我在Android上遇到了相機預覽的一些問題。無法在onCreate中創建相機預覽?

現在我有一個按鈕,你需要按下之前,你得到任何預覽的相機。 但編號喜歡有一個預覽,只要你啓動應用程序。

如果我嘗試從按鈕開始預覽部分,並將其放入onCreate,它不會工作,預覽不會啓動。

如何在沒有用戶觸摸按鈕的情況下創建預覽?

此外,我有一個HTC Desire HD,預覽是轉90度爲我。 這是怎麼回事?

Camera cam; 
SurfaceView surf; 
SurfaceHolder holder; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button btnStart= (Button)findViewById(R.id.startCamPrev); 

    getWindow().setFormat(PixelFormat.UNKNOWN); 

    surf = (SurfaceView)findViewById(R.id.surfaceView); 

    holder = surf.getHolder(); 
    holder.addCallback(this); 
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    btnStart.setOnClickListener(new Button.OnClickListener() { 

     public void onClick(View arg0) { 
      cam = Camera.open(); 
      if(cam != null) 
      { 
       try 
       { 
        cam.setPreviewDisplay(holder); 
        cam.startPreview(); 
       } 
       catch(IOException e) 
       { 
       } 
      } 
     } 
    }); 
} 

清單文件:

 <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="LSAB.CamTest" 
     android:versionCode="1" 
     android:versionName="1.0"> 
     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
      <activity android:name=".CameraTest" 
        android:label="@string/app_name"> 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
      </activity> 

     </application> 
     <uses-sdk android:minSdkVersion="4" /> 

    <uses-permission android:name="android.permission.CAMERA"></uses-permission> 
    </manifest> 

的main.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" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, CameraTest" 
    /> 
<Button 
    android:id = "@+id/startCamPrev" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="- Start Preview -" 
    /> 
<SurfaceView 
    android:id="@+id/surfaceView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

回答

0

你檢查的權限在清單?另外,儘量不要在OnCreate中做那麼多事,這是一個糟糕的做法,請爲上帝的愛做好準備,如果你需要幫助,請評論一下代碼。

好的,嘗試改變佈局的方向並告訴我會發生什麼。

+0

對不起,大部分只是做一些測試atm,所以我想我一直懶得註釋。 – Tistatos 2011-06-04 21:53:52

+0

我堅持,請張貼清單和main.xml,也許這是問題,因爲你的方向問題。 – AlfredoVR 2011-06-04 22:09:55

+0

主要和mainfest張貼 – Tistatos 2011-06-04 22:38:25

2

除了加入setPreviewDisplayonCreate嘗試做 - >

落實SurfaceHolder.Callback

onSurfaceChanged方法與傳遞給方法的新surfaceHolder重置setPreviewDisplay

這對我有用..