OSDN Git Service

次バージョンの準備。
[gokigen/mangle.git] / app / src / main / java / jp / osdn / gokigen / gokigenassets / camera / vendor / sony / operation / takepicture / AutoFocusControl.kt
1 package jp.osdn.gokigen.gokigenassets.camera.vendor.sony.operation.takepicture
2
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
12
13 class AutoFocusControl(private val frameDisplayer: IAutoFocusFrameDisplay, private val indicator: IIndicatorControl)
14 {
15     private lateinit var cameraApi: ISonyCameraApi
16     fun setCameraApi(sonyCameraApi: ISonyCameraApi)
17     {
18         cameraApi = sonyCameraApi
19     }
20
21     fun lockAutoFocus(point: PointF)
22     {
23         Log.v(TAG, "lockAutoFocus() : [" + point.x + ", " + point.y + "]")
24         if (!::cameraApi.isInitialized)
25         {
26             Log.v(TAG, "ISonyCameraApi is not initialized...")
27             return
28         }
29         try
30         {
31             val thread = Thread {
32                 val preFocusFrameRect = getPreFocusFrameRect(point)
33                 try
34                 {
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)
41                     {
42                         //Log.v(TAG, " lockAutoFocus() LEN:${resultsObj.length()}  $resultsObj")
43                         if (findTouchAFPositionResult(resultsObj))
44                         {
45                             // AF FOCUSED
46                             Log.v(TAG, "lockAutoFocus() : FOCUSED")
47                             showFocusFrame(preFocusFrameRect, FocusFrameStatus.Focused, 0.0)
48                         }
49                         else
50                         {
51                             // AF ERROR
52                             Log.v(TAG, "lockAutoFocus() : ERROR")
53                             showFocusFrame(preFocusFrameRect, FocusFrameStatus.Failed, 1.0)
54                         }
55                     }
56                     else
57                     {
58                         Log.v(TAG, "setTouchAFPosition() reply is null.")
59                     }
60                 }
61                 catch (e: Exception)
62                 {
63                     e.printStackTrace()
64                     try
65                     {
66                         showFocusFrame(preFocusFrameRect, FocusFrameStatus.Errored, 1.0)
67                     }
68                     catch (ee: Exception)
69                     {
70                         ee.printStackTrace()
71                     }
72                 }
73             }
74             thread.start()
75         }
76         catch (e: Exception)
77         {
78             e.printStackTrace()
79         }
80     }
81
82     /**
83      * シャッター半押し処理
84      *
85      */
86     fun halfPressShutter(isPressed: Boolean)
87     {
88         Log.v(TAG, "halfPressShutter() : $isPressed")
89         if (!::cameraApi.isInitialized)
90         {
91             Log.v(TAG, "ISonyCameraApi is not initialized...")
92             return
93         }
94         try
95         {
96             val thread = Thread {
97                 try
98                 {
99                     val resultsObj = if (isPressed) cameraApi.actHalfPressShutter() else cameraApi.cancelHalfPressShutter()
100                     if (resultsObj == null)
101                     {
102                         Log.v(TAG, "halfPressShutter() [$isPressed] reply is null.")
103                     }
104                     else
105                     {
106                         indicator.onAfLockUpdate(isPressed)
107                     }
108                 }
109                 catch (e: Exception)
110                 {
111                     e.printStackTrace()
112                 }
113             }
114             thread.start()
115         }
116         catch (e: Exception)
117         {
118             e.printStackTrace()
119         }
120     }
121
122     /**
123      *
124      *
125      */
126     fun unlockAutoFocus()
127     {
128         Log.v(TAG, "unlockAutoFocus()")
129         if (!::cameraApi.isInitialized)
130         {
131             Log.v(TAG, "ISonyCameraApi is not initialized...")
132             return
133         }
134         try
135         {
136             val thread = Thread {
137                 try
138                 {
139                     val resultsObj = cameraApi.cancelTouchAFPosition()
140                     if (resultsObj == null)
141                     {
142                         Log.v(TAG, "cancelTouchAFPosition() reply is null.")
143                     }
144                     hideFocusFrame()
145                 }
146                 catch (e: Exception)
147                 {
148                     e.printStackTrace()
149                 }
150             }
151             thread.start()
152         }
153         catch (e: Exception)
154         {
155             e.printStackTrace()
156         }
157     }
158
159     /**
160      *
161      *
162      */
163     private fun showFocusFrame(rect: RectF, status: FocusFrameStatus, duration: Double)
164     {
165         frameDisplayer.showFocusFrame(rect, status, duration.toFloat())
166         indicator.onAfLockUpdate(FocusFrameStatus.Focused === status)
167     }
168
169     /**
170      *
171      *
172      */
173     private fun hideFocusFrame()
174     {
175         frameDisplayer.hideFocusFrame()
176         indicator.onAfLockUpdate(false)
177     }
178
179     /**
180      *
181      *
182      */
183     private fun findTouchAFPositionResult(replyJson: JSONObject): Boolean
184     {
185         var afResult = false
186         try
187         {
188             val indexOfTouchAFPositionResult = 1
189             val resultsObj = replyJson.getJSONArray("result")
190             if (!resultsObj.isNull(indexOfTouchAFPositionResult))
191             {
192                 val touchAFPositionResultObj = resultsObj.getJSONObject(indexOfTouchAFPositionResult)
193                 afResult = touchAFPositionResultObj.getBoolean("AFResult")
194                 Log.v(TAG, "AF Result : $afResult")
195             }
196         }
197         catch (e: Exception)
198         {
199             //e.printStackTrace()
200
201             // resultがない... halfPressShutterで対応する
202             Log.v(TAG, " Touch-AF is fail. try halfPressShutter action.")
203             halfPressShutter(true)
204         }
205         return afResult
206     }
207
208     /**
209      *
210      *
211      */
212     private fun getPreFocusFrameRect(point: PointF): RectF
213     {
214         val imageWidth = frameDisplayer.getContentSizeWidth()
215         val imageHeight = frameDisplayer.getContentSizeHeight()
216
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)
221         {
222             imageWidth / imageHeight
223         }
224         else
225         {
226             imageHeight / imageWidth
227         }
228         return RectF(point.x - focusWidth / 2.0f, point.y - focusHeight / 2.0f, point.x + focusWidth / 2.0f, point.y + focusHeight / 2.0f)
229     }
230
231     companion object
232     {
233         private val TAG = AutoFocusControl::class.java.simpleName
234     }
235 }