OSDN Git Service

GR2 / PENTAXモードの自動認識・切り替えをできるようにした。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / ricohgr2 / operation / takepicture / RicohGr2AutoFocusControl.java
1 package net.osdn.gokigen.a01d.camera.ricohgr2.operation.takepicture;
2
3 import android.graphics.PointF;
4 import android.graphics.RectF;
5 import android.util.Log;
6
7 import net.osdn.gokigen.a01d.camera.ricohgr2.wrapper.IUsePentaxCommand;
8 import net.osdn.gokigen.a01d.camera.utils.SimpleHttpClient;
9 import net.osdn.gokigen.a01d.liveview.IAutoFocusFrameDisplay;
10 import net.osdn.gokigen.a01d.liveview.IIndicatorControl;
11
12 import org.json.JSONObject;
13
14 import androidx.annotation.NonNull;
15
16 /**
17  *
18  *
19  */
20 public class RicohGr2AutoFocusControl
21 {
22     private static final String TAG = RicohGr2AutoFocusControl.class.getSimpleName();
23     private final IIndicatorControl indicator;
24     private final IAutoFocusFrameDisplay frameDisplayer;
25     private final IUsePentaxCommand usePentaxCommand;
26     private String unlockAutoFocusUrl = "http://192.168.0.1/v1/lens/focus/unlock";
27     private String halfPressShutterUrl = "http://192.168.0.1/_gr";
28     private int timeoutMs = 6000;
29
30
31     /**
32      *
33      *
34      */
35     public RicohGr2AutoFocusControl(@NonNull final IAutoFocusFrameDisplay frameDisplayer, final IIndicatorControl indicator, @NonNull IUsePentaxCommand usePentaxCommand)
36     {
37         this.frameDisplayer = frameDisplayer;
38         this.indicator = indicator;
39         this.usePentaxCommand = usePentaxCommand;
40     }
41
42     /**
43      *
44      *
45      */
46     public void lockAutoFocus(@NonNull final PointF point)
47     {
48         Log.v(TAG, "lockAutoFocus() : [" + point.x + ", " + point.y + "]");
49         try
50         {
51             Thread thread = new Thread(new Runnable()
52             {
53                 @Override
54                 public void run()
55                 {
56                     RectF preFocusFrameRect = getPreFocusFrameRect(point);
57                     try
58                     {
59                         showFocusFrame(preFocusFrameRect, IAutoFocusFrameDisplay.FocusFrameStatus.Running, 0.0);
60
61                         String lockAutoFocusUrl = (usePentaxCommand.getUsePentaxCommand()) ? "http://192.168.0.1/v1/lens/focus" : "http://192.168.0.1/v1/lens/focus/lock";
62                         //int posX = (int) (Math.round(point.x * 100.0));
63                         //int posY = (int) (Math.round(point.y * 100.0));
64                         String postData = "pos=" + ( (int) (Math.round(point.x * 100.0))) + "," + ((int) (Math.round(point.y * 100.0)));
65                         Log.v(TAG, "AF (" + postData + ")");
66                         String result = SimpleHttpClient.httpPost(lockAutoFocusUrl, postData, timeoutMs);
67                         if ((result == null)||(result.length() < 1))
68                         {
69                             Log.v(TAG, "setTouchAFPosition() reply is null.");
70                         }
71                         else if (findTouchAFPositionResult(result))
72                         {
73                             // AF FOCUSED
74                             Log.v(TAG, "lockAutoFocus() : FOCUSED");
75                             showFocusFrame(preFocusFrameRect, IAutoFocusFrameDisplay.FocusFrameStatus.Focused, 1.0);  // いったん1秒だけ表示
76                         }
77                         else
78                         {
79                             // AF ERROR
80                             Log.v(TAG, "lockAutoFocus() : ERROR");
81                             showFocusFrame(preFocusFrameRect, IAutoFocusFrameDisplay.FocusFrameStatus.Failed, 1.0);
82                         }
83                     }
84                     catch (Exception e)
85                     {
86                         e.printStackTrace();
87                         try
88                         {
89                             showFocusFrame(preFocusFrameRect, IAutoFocusFrameDisplay.FocusFrameStatus.Errored, 1.0);
90                         }
91                         catch (Exception ee)
92                         {
93                             ee.printStackTrace();
94                         }
95                     }
96                 }
97             });
98             thread.start();
99         }
100         catch (Exception e)
101         {
102             e.printStackTrace();
103         }
104     }
105
106     /**
107      *
108      *
109      */
110     public void unlockAutoFocus()
111     {
112         Log.v(TAG, "unlockAutoFocus()");
113         try
114         {
115             Thread thread = new Thread(new Runnable()
116             {
117                 @Override
118                 public void run()
119                 {
120                     try
121                     {
122                         String result = SimpleHttpClient.httpPost(unlockAutoFocusUrl, "", timeoutMs);
123                         if ((result == null)||(result.length() < 1))
124                         {
125                             Log.v(TAG, "cancelTouchAFPosition() reply is null.");
126                         }
127                         hideFocusFrame();
128                     }
129                     catch (Exception e)
130                     {
131                         e.printStackTrace();
132                     }
133                 }
134             });
135             thread.start();
136         }
137         catch (Exception e)
138         {
139             e.printStackTrace();
140         }
141     }
142
143     /**
144      *
145      *
146      */
147     public void halfPressShutter(final boolean isPressed)
148     {
149         Log.v(TAG, "halfPressShutter() " + isPressed);
150         try
151         {
152             Thread thread = new Thread(new Runnable()
153             {
154                 @Override
155                 public void run()
156                 {
157                     try
158                     {
159                         String postData = (isPressed) ? "cmd=baf 1" : "cmd=baf 0";
160                         String result = SimpleHttpClient.httpPost(halfPressShutterUrl, postData, timeoutMs);
161                         if ((result == null)||(result.length() < 1))
162                         {
163                             Log.v(TAG, "halfPressShutter() [" + isPressed + "] reply is null.");
164                         }
165                     }
166                     catch (Exception e)
167                     {
168                         e.printStackTrace();
169                     }
170                 }
171             });
172             thread.start();
173         }
174         catch (Exception e)
175         {
176             e.printStackTrace();
177         }
178     }
179
180     /**
181      *
182      *
183      */
184     private void showFocusFrame(RectF rect, IAutoFocusFrameDisplay.FocusFrameStatus status, double duration)
185     {
186         frameDisplayer.showFocusFrame(rect, status, duration);
187         indicator.onAfLockUpdate(IAutoFocusFrameDisplay.FocusFrameStatus.Focused == status);
188     }
189
190     /**
191      *
192      *
193      */
194     private void hideFocusFrame()
195     {
196         frameDisplayer.hideFocusFrame();
197         indicator.onAfLockUpdate(false);
198     }
199
200     /**
201      *
202      *
203      */
204     private RectF getPreFocusFrameRect(@NonNull PointF point)
205     {
206         float imageWidth =  frameDisplayer.getContentSizeWidth();
207         float imageHeight =  frameDisplayer.getContentSizeHeight();
208
209         // Display a provisional focus frame at the touched point.
210         float focusWidth = 0.125f;  // 0.125 is rough estimate.
211         float focusHeight = 0.125f;
212         if (imageWidth > imageHeight)
213         {
214             focusHeight *= (imageWidth / imageHeight);
215         }
216         else
217         {
218             focusHeight *= (imageHeight / imageWidth);
219         }
220         return (new RectF(point.x - focusWidth / 2.0f, point.y - focusHeight / 2.0f,
221                 point.x + focusWidth / 2.0f, point.y + focusHeight / 2.0f));
222     }
223
224     /**
225      *
226      *
227      */
228     private static boolean findTouchAFPositionResult(String replyString)
229     {
230         boolean afResult = false;
231         try
232         {
233             JSONObject resultObject = new JSONObject(replyString);
234             String result = resultObject.getString("errMsg");
235             boolean focused = resultObject.getBoolean("focused");
236             if (result.contains("OK"))
237             {
238                 afResult = focused;
239                 Log.v(TAG, "AF Result : " + afResult);
240
241             }
242         }
243         catch (Exception e)
244         {
245             e.printStackTrace();
246         }
247         return (afResult);
248     }
249 }