2013-10-02 7 views
1

在調試器中運行此我看到info.facing == 0。這意味着它是一個背面相機。相機模擬器「面對」行事奇怪

當我嘗試實例化Camrea對象時,我得到空值。

在仿真器上,我將設備設置爲禁用camrea禁用並啓用前置攝像頭。爲什麼該設備在沒有時會認爲存在背對背卡路里?

我正在使用Eclipse ADT。

這裏是我的方法。我從來沒有達到第二個循環。 getCamreaInstance返回空值爲c的c。

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

     CameraInfo info = new CameraInfo(); 
     if (info.facing == CameraInfo.CAMERA_FACING_BACK){ 

      //Calling Camera.open() throws an exception if the camera is already in use by another application, so we wrap it in a try block. 
      //Failing to check for exceptions if the camera is in use or does not exist will cause your application to be shut down by the system. 
      try { 
       c = Camera.open(); 
      } 
      catch (Exception e){ 
       // Camera is not available (in use or does not exist) 
      } 
      return c; 
     } 
     //we want the back facing, if we cant get that then we try and get the front facing 
     else if (info.facing == CameraInfo.CAMERA_FACING_FRONT){ 
      c = Camera.open(Camera.getNumberOfCameras()-1); //i should test and see if -1 is a valid value in the case that a device has no camera 
      return c; 
     } 
     else{ 
      //there are no cameras, so we need to account for that since 'c' will be null 
      return c; 
     } 

    } 

回答

1

這條線:

CameraInfo info = new CameraInfo(); 

獲得當前的攝像頭配置。這只是一個空的默認構造函數。獲取準確的CameraInfo對象的唯一方法是Camera#getCameraInfo()

你得到一個空的攝像頭的原因是因爲默認facing是0,所以,它進入第一個塊,嘗試open(),因爲它返回null:

如果設備不具有後置攝像頭,則返回null。

您可以從一開始就致電getNumberOfCameras()查看有多少個攝像頭。然後打開一個,並檢查它是CameraInfo以查看它面對的方式。

但是,如果您總是希望默認使用背面照相機(這看起來很有可能,請參閱您的代碼),只需將支票移除到facing並在open()上檢查爲空。

+0

請原諒我的無知。 java中的#有什麼作用? 也是getCameraInfo一個有效的方法與空參數?它看起來像在這裏的API文件,但不是在我的IDE ... http://developer.android.com/reference/android/hardware/Camera.CameraInfo.html#Camera.CameraInfo() –

+1

對不起,'' #'只是表示方法的符號。這是一種靜態方法,不,沒有參數沒有選項。爲了保存輸入參數,我鏈接到方法的文檔,以便您可以自己查看。空構造函數顯示在文檔中,因爲根據定義,所有對象都有一個空的構造函數。儘管如此,它不會做任何事情。 – Geobits

+0

很高興知道。謝謝你清理那個。 你是對的這個答案 –