1 package jp.osdn.gokigen.gokigenassets.camera.vendor.sony.operation.takepicture
3 import org.json.JSONObject
4 import android.graphics.RectF
5 import android.graphics.PointF
6 import android.util.Log
7 import jp.osdn.gokigen.gokigenassets.liveview.focusframe.IAutoFocusFrameDisplay
8 import jp.osdn.gokigen.gokigenassets.camera.vendor.sony.wrapper.ISonyCameraApi
9 import jp.osdn.gokigen.gokigenassets.liveview.IIndicatorControl
10 import jp.osdn.gokigen.gokigenassets.liveview.focusframe.IAutoFocusFrameDisplay.FocusFrameStatus
11 import java.lang.Exception
13 class AutoFocusControl(private val frameDisplayer: IAutoFocusFrameDisplay, private val indicator: IIndicatorControl)
15 private lateinit var cameraApi: ISonyCameraApi
16 fun setCameraApi(sonyCameraApi: ISonyCameraApi)
18 cameraApi = sonyCameraApi
21 fun lockAutoFocus(point: PointF)
23 Log.v(TAG, "lockAutoFocus() : [" + point.x + ", " + point.y + "]")
24 if (!::cameraApi.isInitialized)
26 Log.v(TAG, "ISonyCameraApi is not initialized...")
32 val preFocusFrameRect = getPreFocusFrameRect(point)
35 showFocusFrame(preFocusFrameRect, FocusFrameStatus.Running, 0.0)
36 val posX = point.x * 100.0
37 val posY = point.y * 100.0
38 Log.v(TAG, "AF ($posX, $posY)")
39 val resultsObj = cameraApi.setTouchAFPosition(posX, posY)
40 if (resultsObj != null)
42 //Log.v(TAG, " lockAutoFocus() LEN:${resultsObj.length()} $resultsObj")
43 if (findTouchAFPositionResult(resultsObj))
46 Log.v(TAG, "lockAutoFocus() : FOCUSED")
47 showFocusFrame(preFocusFrameRect, FocusFrameStatus.Focused, 0.0)
52 Log.v(TAG, "lockAutoFocus() : ERROR")
53 showFocusFrame(preFocusFrameRect, FocusFrameStatus.Failed, 1.0)
58 Log.v(TAG, "setTouchAFPosition() reply is null.")
66 showFocusFrame(preFocusFrameRect, FocusFrameStatus.Errored, 1.0)
86 fun halfPressShutter(isPressed: Boolean)
88 Log.v(TAG, "halfPressShutter() : $isPressed")
89 if (!::cameraApi.isInitialized)
91 Log.v(TAG, "ISonyCameraApi is not initialized...")
99 val resultsObj = if (isPressed) cameraApi.actHalfPressShutter() else cameraApi.cancelHalfPressShutter()
100 if (resultsObj == null)
102 Log.v(TAG, "halfPressShutter() [$isPressed] reply is null.")
106 indicator.onAfLockUpdate(isPressed)
126 fun unlockAutoFocus()
128 Log.v(TAG, "unlockAutoFocus()")
129 if (!::cameraApi.isInitialized)
131 Log.v(TAG, "ISonyCameraApi is not initialized...")
136 val thread = Thread {
139 val resultsObj = cameraApi.cancelTouchAFPosition()
140 if (resultsObj == null)
142 Log.v(TAG, "cancelTouchAFPosition() reply is null.")
163 private fun showFocusFrame(rect: RectF, status: FocusFrameStatus, duration: Double)
165 frameDisplayer.showFocusFrame(rect, status, duration.toFloat())
166 indicator.onAfLockUpdate(FocusFrameStatus.Focused === status)
173 private fun hideFocusFrame()
175 frameDisplayer.hideFocusFrame()
176 indicator.onAfLockUpdate(false)
183 private fun findTouchAFPositionResult(replyJson: JSONObject): Boolean
188 val indexOfTouchAFPositionResult = 1
189 val resultsObj = replyJson.getJSONArray("result")
190 if (!resultsObj.isNull(indexOfTouchAFPositionResult))
192 val touchAFPositionResultObj = resultsObj.getJSONObject(indexOfTouchAFPositionResult)
193 afResult = touchAFPositionResultObj.getBoolean("AFResult")
194 Log.v(TAG, "AF Result : $afResult")
199 //e.printStackTrace()
201 // resultがない... halfPressShutterで対応する
202 Log.v(TAG, " Touch-AF is fail. try halfPressShutter action.")
203 halfPressShutter(true)
212 private fun getPreFocusFrameRect(point: PointF): RectF
214 val imageWidth = frameDisplayer.getContentSizeWidth()
215 val imageHeight = frameDisplayer.getContentSizeHeight()
217 // Display a provisional focus frame at the touched point.
218 val focusWidth = 0.125f // 0.125 is rough estimate.
219 var focusHeight = 0.125f
220 focusHeight *= if (imageWidth > imageHeight)
222 imageWidth / imageHeight
226 imageHeight / imageWidth
228 return RectF(point.x - focusWidth / 2.0f, point.y - focusHeight / 2.0f, point.x + focusWidth / 2.0f, point.y + focusHeight / 2.0f)
233 private val TAG = AutoFocusControl::class.java.simpleName