2013-03-23 100 views
1

我正在嘗試爲Android編寫相機應用程序,但我無法使預覽工作。我試圖按照Android給出的步驟here,但它不起作用,應用程序不斷崩潰。我不確定我做錯了什麼,但我很確定我是按照正確的步驟行事的。Android - 相機預覽不起作用

這是我到目前爲止有: 相機Activity類

public class CameraActivity extends Activity 
{ 
    private CameraPreview mPreview = null; 
    private Camera mCamera = null; 


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

     mCamera = getCameraInstance(); 

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

    } 
    public static Camera getCameraInstance() 
    { 
     Camera c = null; 

     try 
     { 
      //attempt to get a Camera instance 
      c = Camera.open(); 
     } 
     catch(Exception e) 
     { 
      //Camera is not available(in use or does not exist) 
     } 
     return c;//returns null if camera is unavailable 
    } 


    public void onPause() 
    { 
     super.onPause(); 
     mCamera.stopPreview(); 
     releaseCamera(); 
    } 


    private void releaseCamera() 
    { 
     if(mCamera != null) 
     { 
      mCamera.release(); 
      mCamera = null; 
     } 
    } 

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

攝像頭預覽類

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback 
{ 
    private final String TAG = "CameraPreview"; 
    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(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 width, int height) 
    { 
     //if your preview can change or rotate, take care of those events here. 
     //make sure to stop the preview before resizing of 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 
     try 
     { 
      //start preview with new settings 
      mCamera.setPreviewDisplay(mHolder); 
      mCamera.startPreview(); 
     } 
     catch(Exception e) 
     { 
      Log.d(TAG, "Error starting camera preview: " + e.getMessage()); 
     } 
    } 
} 

,這是我的相機活動的.xml:

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


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

據我所知,我正確地按照指南,它應該工作。由於下一部分與錄音有關,我想到了所有步驟,直到此時應該產生工作預覽,但它不起作用。任何人都可以看到我需要改變的錯誤嗎? 感謝

編輯:增加了我的清單 的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.ics466.project.warpwalker" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<uses-feature android:name="android.hardware.camera" /> 
<uses-permission android:name="android.permission.CAMERA" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.ics466.project.warpwalker.WarpIntro" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
      </activity> 
      <activity 
      android:name="com.ics466.project.warpwalker.CameraActivity" 
      android:label="@string/title_activity_camera" > 
     </activity> 
    </application> 

</manifest> 
+0

你說崩潰。怎麼樣?發佈你的logcat – ElefantPhace 2013-03-23 04:25:01

+0

添加清單http://developer.android.com/reference/android/hardware/Camera.html – 2013-03-23 04:28:45

+0

什麼是你的logcat說?? – Shiv 2013-03-23 04:50:10

回答

0

android:minSdkVersion="8"應該android:minSdkVersion="9"

,如果你已經使用這些API中的9

增加了一些功能和閱讀此鏈接How To Enable Camera in Android Emulator

或者在Rea中測試這個應用程序l帶攝像頭的設備。

+0

好了,在將最低版本更改爲9之後,它已停止崩潰,但現在屏幕只是黑色。我有虛擬設備,我正在測試它模擬相機,所以我認爲它不應該是黑色的...? – Kamakana 2013-03-23 04:52:47

+0

如果你正在模擬器中運行它....模擬器中的相機和預覽在哪裏,所以它不應該是黑色? – Shiv 2013-03-23 04:58:19

+0

整個屏幕是黑色的或只是camera_preview framelayout是黑色的? – Shiv 2013-03-23 05:09:51

0

在大多數設備上,相機預覽的默認方向是橫向。

嘗試將android:orientation="horizontal"添加到您的.xml文件的LinearLayout中。