OSDN Git Service

1枚撮影ができるようになった。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / olycamerawrapper / OlyCameraCoordinator.java
1 package jp.sfjp.gokigen.a01c.olycamerawrapper;
2
3 import android.app.Activity;
4 import android.graphics.PointF;
5 import android.graphics.RectF;
6 import android.util.Log;
7 import android.view.MotionEvent;
8 import android.widget.Toast;
9
10 import androidx.annotation.NonNull;
11 import androidx.appcompat.app.AppCompatActivity;
12 import androidx.preference.PreferenceDataStore;
13 import androidx.preference.PreferenceManager;
14
15 import jp.co.olympus.camerakit.OLYCamera;
16
17 import jp.co.olympus.camerakit.OLYCameraLiveViewListener;
18 import jp.sfjp.gokigen.a01c.ICameraConnection;
19 import jp.sfjp.gokigen.a01c.ICameraController;
20 import jp.sfjp.gokigen.a01c.ICameraFeatureDispatcher;
21 import jp.sfjp.gokigen.a01c.IShowInformation;
22 import jp.sfjp.gokigen.a01c.R;
23 import jp.sfjp.gokigen.a01c.liveview.CameraLiveViewListenerImpl;
24 import jp.sfjp.gokigen.a01c.liveview.IAutoFocusFrameDisplay;
25 import jp.sfjp.gokigen.a01c.liveview.ICameraStatusReceiver;
26 import jp.sfjp.gokigen.a01c.liveview.ILiveImageStatusNotify;
27 import jp.sfjp.gokigen.a01c.olycamerawrapper.dispatcher.FeatureDispatcher;
28 import jp.sfjp.gokigen.a01c.olycamerawrapper.indicator.CameraStatusDisplay;
29 import jp.sfjp.gokigen.a01c.olycamerawrapper.indicator.ICameraStatusDisplay;
30 import jp.sfjp.gokigen.a01c.olycamerawrapper.listeners.CameraPropertyListenerImpl;
31 import jp.sfjp.gokigen.a01c.olycamerawrapper.listeners.CameraRecordingListenerImpl;
32 import jp.sfjp.gokigen.a01c.olycamerawrapper.listeners.CameraStatusListenerImpl;
33 import jp.sfjp.gokigen.a01c.olycamerawrapper.property.ICameraPropertyLoadSaveOperations;
34 import jp.sfjp.gokigen.a01c.olycamerawrapper.property.ILoadSaveCameraProperties;
35 import jp.sfjp.gokigen.a01c.olycamerawrapper.property.IOlyCameraProperty;
36 import jp.sfjp.gokigen.a01c.olycamerawrapper.property.IOlyCameraPropertyProvider;
37 import jp.sfjp.gokigen.a01c.olycamerawrapper.property.LoadSaveCameraProperties;
38 import jp.sfjp.gokigen.a01c.olycamerawrapper.property.OlyCameraPropertyProxy;
39 import jp.sfjp.gokigen.a01c.olycamerawrapper.takepicture.AutoFocusControl;
40 import jp.sfjp.gokigen.a01c.olycamerawrapper.takepicture.BracketingShotControl;
41 import jp.sfjp.gokigen.a01c.olycamerawrapper.takepicture.MovieRecordingControl;
42 import jp.sfjp.gokigen.a01c.olycamerawrapper.takepicture.SingleShotControl;
43 import jp.sfjp.gokigen.a01c.olycamerawrapper.property.CameraPropertyLoadSaveOperations;
44 import jp.sfjp.gokigen.a01c.preference.IPreferenceCameraPropertyAccessor;
45
46 /**
47  *   OlyCameraCoordinator : Olympus Air との接続、切断の間をとりもつクラス。
48  *                         (OLYCameraクラスの実体を保持する)
49  *
50  *    1. クラスを作成する
51  *    2. connectWifi() でカメラと接続する
52  *    3. disconnect() でカメラと切断する
53  *
54  *    X. onDisconnectedByError() でカメラの通信状態が変更されたことを受信する
55  *    o. CameraInteractionCoordinator.ICameraCallback でカメラとの接続状態を通知する
56  *
57  */
58 public class OlyCameraCoordinator implements ICameraController, IIndicatorControl, ICameraRunMode, IOLYCameraObjectProvider
59 {
60     private final String TAG = toString();
61     private final IAutoFocusFrameDisplay focusFrameDisplay;
62     private final OLYCamera camera;
63     private final Activity context;
64
65     // 本クラスの配下のカメラ制御クラス群
66     private final AutoFocusControl autoFocus;
67     private final SingleShotControl singleShot;
68     private final MovieRecordingControl movieControl;
69     private final BracketingShotControl bracketingShot;
70     private final OlyCameraPropertyProxy propertyProxy;
71     private final CameraPropertyLoadSaveOperations loadSaveOperations;
72     private final LoadSaveCameraProperties loadSaveCameraProperties;
73     private final OlyCameraConnection cameraConnection;
74     private final ICameraStatusDisplay cameraStatusDisplay;
75     private final LevelMeterHolder levelMeter;
76     private final ZoomLensHolder zoomLensHolder;
77
78     private boolean isManualFocus = false;
79     private boolean isAutoFocusLocked = false;
80     //private boolean isExposureLocked = false;
81
82     private FeatureDispatcher featureDispatcher = null;
83
84
85     /**
86      * コンストラクタ
87      */
88     public OlyCameraCoordinator(Activity context, IAutoFocusFrameDisplay focusFrameDisplay, IShowInformation showInformation, ICameraStatusReceiver receiver)
89     {
90         this.context = context;
91         this.focusFrameDisplay = focusFrameDisplay;
92
93         // OLYMPUS CAMERA クラスの初期化、リスナの設定
94         camera = new OLYCamera();
95         camera.setContext(context.getApplicationContext());
96
97         this.cameraConnection = new OlyCameraConnection(context, camera, receiver);
98         camera.setConnectionListener(cameraConnection);
99
100         // 本クラスの配下のカメラ制御クラス群の設定
101         autoFocus = new AutoFocusControl(camera, focusFrameDisplay, this); // AF制御
102         singleShot = new SingleShotControl(camera, focusFrameDisplay, this, showInformation);  // 1枚撮影
103         movieControl = new MovieRecordingControl(context, camera, showInformation); // ムービー撮影
104         bracketingShot = new BracketingShotControl(camera, focusFrameDisplay, this, showInformation);  // ブラケッティング&インターバル撮影
105         propertyProxy = new OlyCameraPropertyProxy(camera); // カメラプロパティ
106         cameraStatusDisplay = new CameraStatusDisplay(propertyProxy, showInformation);  // 画面表示
107         this.levelMeter = new LevelMeterHolder(showInformation, PreferenceManager.getDefaultSharedPreferences(context).getBoolean(IPreferenceCameraPropertyAccessor.SHOW_LEVEL_GAUGE_STATUS, false));  // デジタル水準器
108         camera.setCameraStatusListener(new CameraStatusListenerImpl(context, cameraStatusDisplay, levelMeter));
109         camera.setCameraPropertyListener(new CameraPropertyListenerImpl(cameraStatusDisplay));
110         camera.setRecordingListener(new CameraRecordingListenerImpl(context, showInformation));
111         loadSaveCameraProperties = new LoadSaveCameraProperties(context, propertyProxy, this);
112         loadSaveOperations = new CameraPropertyLoadSaveOperations(context, loadSaveCameraProperties, cameraStatusDisplay);
113         zoomLensHolder = new ZoomLensHolder(camera);
114     }
115
116     @Override
117     public void setLiveViewListener(@NonNull CameraLiveViewListenerImpl listener)
118     {
119         Log.v(TAG, "setLiveViewListener()");
120         try
121         {
122             camera.setLiveViewListener(listener);
123         }
124         catch (Exception e)
125         {
126             e.printStackTrace();
127         }
128     }
129
130     /**
131      *   ライブビューの解像度を設定する
132      *
133      */
134     @Override
135     public void changeLiveViewSize(String size)
136     {
137         Log.v(TAG, "changeLiveViewSize() : " + size);
138         try
139         {
140             camera.changeLiveViewSize(CameraPropertyUtilities.toLiveViewSizeType(size));
141         }
142         catch (Exception e)
143         {
144             e.printStackTrace();
145         }
146     }
147
148     /**
149      *   ライブビューの開始
150      *
151      */
152     @Override
153     public void startLiveView()
154     {
155         Log.v(TAG, "startLiveView()");
156         try
157         {
158             camera.startLiveView();
159         }
160         catch (Exception e)
161         {
162             e.printStackTrace();
163         }
164     }
165
166     /**
167      *   ライブビューの終了
168      *
169      */
170     @Override
171     public void stopLiveView()
172     {
173         Log.v(TAG, "stopLiveView()");
174         try
175         {
176             camera.stopLiveView();
177         }
178         catch (Exception e)
179         {
180             e.printStackTrace();
181         }
182     }
183
184     /**
185      *   撮影モードの更新
186      *
187      */
188     @Override
189     public void updateTakeMode()
190     {
191         cameraStatusDisplay.updateTakeMode();
192     }
193
194     /**
195      *   フォーカスロックの実行
196      */
197     public boolean driveAutoFocus(MotionEvent event)
198     {
199         if (event.getAction() != MotionEvent.ACTION_DOWN)
200         {
201             return (false);
202         }
203         PointF point = focusFrameDisplay.getPointWithEvent(event);
204         return ((focusFrameDisplay.isContainsPoint(point)) && autoFocus.lockAutoFocus(point));
205     }
206
207     /**
208      *   オートフォーカスのポイントに含まれているか確認する (trueだったら含まれる)
209      *
210      */
211     @Override
212     public boolean isContainsAutoFocusPoint(MotionEvent event)
213     {
214         //// 以下の機能は、タッチイベントの受信時のみ動作させる
215         //if (event.getAction() != MotionEvent.ACTION_DOWN)
216         //{
217         //    return (false);
218         //}
219         try
220         {
221             // タッチした位置を取得する
222             PointF point = focusFrameDisplay.getPointWithEvent(event);
223
224             // AFの有効座標を取得する
225             RectF rect = camera.getAutoFocusEffectiveArea();
226             //Log.v(TAG, "AF POINT (" + point.x + "," + point.y + ")");
227             //Log.v(TAG, "AF EFFECTIVE AREA (" + rect.left + "," + rect.left + ") - (" + rect.right + "," + rect.bottom + ")");
228             return (rect.contains(point.x, point.y));
229         }
230         catch (Exception e)
231         {
232             e.printStackTrace();
233         }
234         return (false);
235     }
236
237     /**
238      *   フォーカスロックの解除
239      */
240     public void unlockAutoFocus()
241     {
242         autoFocus.unlockAutoFocus();
243         focusFrameDisplay.hideFocusFrame();
244         isAutoFocusLocked = false;
245     }
246
247     /**
248      *   画像を1枚撮影
249      */
250     @Override
251     public void singleShot()
252     {
253         singleShot.singleShot();
254         {
255             // 撮影の表示をToastで行う (成功とか失敗とか言っていない)
256             Toast.makeText(context, R.string.shoot_camera, Toast.LENGTH_SHORT).show();
257         }
258     }
259
260     /**
261      *   ムービーの撮影・停止
262      */
263     @Override
264     public void movieControl()
265     {
266         movieControl.movieControl();
267     }
268
269     /**
270      *   インターバル&ブラケッティング撮影の実行
271      *
272      *     @param bracketingStyle : ブラケッティングスタイル
273      *     @param bracketingCount : 撮影枚数
274      *     @param durationSeconds : 撮影間隔(単位:秒)
275      *
276      */
277     @Override
278     public void bracketingShot(int bracketingStyle, int bracketingCount, int durationSeconds)
279     {
280         bracketingShot.bracketingShot(bracketingStyle, bracketingCount, durationSeconds);
281     }
282
283     /**
284      *   撮影確認画像を生成するか設定する
285      *
286      */
287     @Override
288     public void setRecViewMode(boolean isRecViewMode)
289     {
290         try
291         {
292             String value = "<" + IOlyCameraProperty.REC_PREVIEW + "/";
293             if (isRecViewMode)
294             {
295                 value = value + "ON>";
296             }
297             else
298             {
299                 value = value + "OFF>";
300             }
301             propertyProxy.setCameraPropertyValue(IOlyCameraProperty.REC_PREVIEW, value);
302         }
303         catch (Exception e)
304         {
305             e.printStackTrace();
306         }
307     }
308
309
310     @Override
311     public void toggleAutoExposure()
312     {
313         try
314         {
315             if (isAELock())
316             {
317                 Log.v(TAG, "toggleAutoExposure() : unlockAutoExposure()");
318                 camera.unlockAutoExposure();
319             }
320             else
321             {
322                 Log.v(TAG, "toggleAutoExposure() : lockAutoExposure()");
323                 camera.lockAutoExposure();
324             }
325             updateIndicatorScreen(false);
326         }
327         catch (Exception e)
328         {
329             e.printStackTrace();
330         }
331         cameraStatusDisplay.updateAeLockState();
332     }
333
334     /**
335      *   AF/MF の切り替えを行う
336      *
337      */
338     @Override
339     public void toggleManualFocus()
340     {
341         try
342         {
343             boolean isHideFocusFrame = false;
344             String property_name = IOlyCameraProperty.FOCUS_STILL;
345             String poverty_value = "<" + IOlyCameraProperty.FOCUS_STILL + "/";
346
347             // マニュアルフォーカス切替え
348             if (!isManualFocus)
349             {
350                 // AF -> MF  : オートフォーカスを解除して設定する
351                 Log.v(TAG, "toggleManualFocus() : to " + IOlyCameraProperty.FOCUS_MF);
352                 poverty_value = poverty_value + IOlyCameraProperty.FOCUS_MF + ">";
353                 camera.unlockAutoFocus();
354                 camera.setCameraPropertyValue(property_name, poverty_value);
355                 isHideFocusFrame = true;
356             }
357             else
358             {
359                 // MF -> AF
360                 Log.v(TAG, "toggleManualFocus() : to " + IOlyCameraProperty.FOCUS_SAF);
361                 poverty_value = poverty_value + IOlyCameraProperty.FOCUS_SAF + ">";
362                 camera.setCameraPropertyValue(property_name, poverty_value);
363             }
364             updateIndicatorScreen(isHideFocusFrame);
365         }
366         catch (Exception e)
367         {
368             e.printStackTrace();
369         }
370     }
371
372     private void updateIndicatorScreen(boolean isHideFocusFrame)
373     {
374         isManualFocus();
375         if (isHideFocusFrame)
376         {
377             isAutoFocusLocked = false;
378             focusFrameDisplay.hideFocusFrame();
379         }
380         cameraStatusDisplay.updateCameraStatus();
381     }
382
383     @Override
384     public boolean isManualFocus()
385     {
386         isManualFocus = propertyProxy.isManualFocus();
387         return (isManualFocus);
388     }
389
390     @Override
391     public boolean isAFLock()
392     {
393         return (isAutoFocusLocked);
394     }
395
396     @Override
397     public boolean isAELock()
398     {
399         return (propertyProxy.isExposureLocked());
400     }
401
402 /*
403     @Override
404     public void setCameraStatusListener(OLYCameraStatusListener listener)
405     {
406         camera.setCameraStatusListener(listener);
407     }
408
409     @Override
410     public String getCameraStatusSummary(ICameraStatusSummary decoder)
411     {
412         return (decoder.getCameraStatusMessage(camera, ""));
413     }
414 */
415
416     /**
417      *   ステータス表示をすべて更新する
418      *
419      */
420     @Override
421     public void updateStatusAll()
422     {
423         cameraStatusDisplay.updateCameraStatusAll();
424     }
425
426     @Override
427     public void changeRunMode(boolean isRecording)
428     {
429         OLYCamera.RunMode runMode = (isRecording) ? OLYCamera.RunMode.Recording : OLYCamera.RunMode.Playback;
430         Log.v(TAG, "changeRunMode() : " + runMode);
431         try
432         {
433             camera.changeRunMode(runMode);
434         }
435         catch (Exception e)
436         {
437             e.printStackTrace();
438         }
439     }
440
441     @Override
442     public boolean isRecordingMode()
443     {
444         boolean isRecordingMode = false;
445         try
446         {
447             OLYCamera.RunMode runMode = camera.getRunMode();
448             isRecordingMode =  (runMode == OLYCamera.RunMode.Recording);
449         }
450         catch (Exception e)
451         {
452             e.printStackTrace();
453         }
454
455         return (isRecordingMode);
456     }
457
458     @Override
459     public IOlyCameraPropertyProvider getCameraPropertyProvider()
460     {
461         return (propertyProxy);
462     }
463
464     @Override
465     public ICameraPropertyLoadSaveOperations getCameraPropertyLoadSaveOperations()
466     {
467         return (loadSaveOperations);
468     }
469
470     @Override
471     public ILoadSaveCameraProperties getLoadSaveCameraProperties()
472     {
473         return (loadSaveCameraProperties);
474     }
475
476     @Override
477     public ICameraRunMode getChangeRunModeExecutor()
478     {
479         return (this);
480     }
481
482     @Override
483     public ICameraConnection getConnectionInterface()
484     {
485         return (cameraConnection);
486     }
487
488     @Override
489     public IZoomLensHolder getZoomLensHolder()
490     {
491         return (zoomLensHolder);
492     }
493
494     @Override
495     public ILevelGauge getLevelGauge()
496     {
497         return (levelMeter);
498     }
499
500     @Override
501     public @NonNull ICameraFeatureDispatcher getFeatureDispatcher(@NonNull AppCompatActivity context, @NonNull IShowInformation statusDrawer, @NonNull ICameraController camera, @NonNull PreferenceDataStore preferenceAccessWrapper, @NonNull ILiveImageStatusNotify liveImageView)
502     {
503         if (featureDispatcher == null)
504         {
505             featureDispatcher = new FeatureDispatcher(context, statusDrawer, camera, preferenceAccessWrapper, liveImageView);
506         }
507         return (featureDispatcher);
508     }
509
510     @Override
511     public void onAfLockUpdate(boolean isAfLocked)
512     {
513         isAutoFocusLocked = isAfLocked;
514         updateIndicatorScreen(false);
515     }
516
517     @Override
518     public void onShootingStatusUpdate(shootingStatus status)
519     {
520         Log.v(TAG, " ShootingStatus : " + status);
521     }
522
523     @Override
524     public OLYCamera getOLYCamera()
525     {
526         return (camera);
527     }
528
529 }