2015-04-06 164 views
1

我想爲我的android應用程序使用新相機硬件API(android.hardware.camera2)的示例代碼。首先,我通過Android Studio的Kotlin插件的自動轉換功能將Java代碼轉換爲Kotlin代碼。下面是一塊轉換代碼:Kotlin - 常量的未解決的參考

private val mCaptureCallback = object : CameraCaptureSession.CaptureCallback() { 

    private fun process(result: CaptureResult) { 
     when (mState) { 
      STATE_PREVIEW -> { 
      }// We have nothing to do when the camera preview is working normally. 
      STATE_WAITING_LOCK -> { 
       val afState = result.get(CaptureResult.CONTROL_AF_STATE) 
       if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) { 
        // CONTROL_AE_STATE can be null on some devices 
        val aeState = result.get<Int>(CaptureResult.CONTROL_AE_STATE) 
        if (aeState == null || aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) { 
         mState = STATE_WAITING_NON_PRECAPTURE 
         captureStillPicture() 
        } else { 
         runPrecaptureSequence() 
        } 
       } 
      } 
      STATE_WAITING_PRECAPTURE -> { 
       // CONTROL_AE_STATE can be null on some devices 
       val aeState = result.get<Int>(CaptureResult.CONTROL_AE_STATE) 
       if (aeState == null || aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE || aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) { 
        mState = STATE_WAITING_NON_PRECAPTURE 
       } 
      } 
      STATE_WAITING_NON_PRECAPTURE -> { 
       // CONTROL_AE_STATE can be null on some devices 
       val aeState = result.get<Int>(CaptureResult.CONTROL_AE_STATE) 
       if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) { 
        mState = STATE_PICTURE_TAKEN 
        captureStillPicture() 
       } 
      } 
     } 
    } 

我得到的未決引用錯誤:

CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED 
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED 
CaptureResult.CONTROL_AE_STATE_CONVERGED 
CaptureResult.CONTROL_AE_STATE_PRECAPTURE 
CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED 

但是,這些常量在原來的Java代碼的認可。有這些錯誤的解決方案嗎?

回答

2

這些變量在CameraMetadata中聲明。在Kotlin中,靜態變量和方法不能在子類上調用。

+0

那麼我應該使用Java代碼嗎? –

+0

不,只需使用CameraMetadata.CONTROL_AF_STATE_FOCUSED_LOCKED而不是CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED –

+0

謝謝!我會嘗試你的建議。 –