OSDN Git Service

機能の実行部分をクラスとして独立。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / olycamerawrapper / takepicture / SingleShotControl.java
1 package jp.sfjp.gokigen.a01c.olycamerawrapper.takepicture;
2
3 import android.graphics.RectF;
4 import android.util.Log;
5
6 import java.util.HashMap;
7 import jp.co.olympus.camerakit.OLYCamera;
8 import jp.co.olympus.camerakit.OLYCameraAutoFocusResult;
9 import jp.sfjp.gokigen.a01c.IShowInformation;
10 import jp.sfjp.gokigen.a01c.liveview.IAutoFocusFrameDisplay;
11 import jp.sfjp.gokigen.a01c.olycamerawrapper.IIndicatorControl;
12
13 /**
14  *   一枚撮影用のクラス
15  *
16  * Created by MRSa on 2016/06/18.
17  */
18 public class SingleShotControl implements OLYCamera.TakePictureCallback
19 {
20     private final String TAG = toString();
21     //private final Context context;
22     private final OLYCamera camera;
23     private final IIndicatorControl indicator;
24     private final IAutoFocusFrameDisplay frameDisplayer;
25     private final IShowInformation statusDrawer;
26
27     /**
28      *  コンストラクタ
29      *
30      */
31     public SingleShotControl(OLYCamera camera, IAutoFocusFrameDisplay frameInfo, IIndicatorControl indicator, IShowInformation statusDrawer)
32     {
33         //this.context = context;
34         this.camera = camera;
35         this.indicator = indicator;
36         this.frameDisplayer = frameInfo;
37         this.statusDrawer = statusDrawer;
38     }
39
40     /**
41      *   1枚撮影する
42      *
43      */
44     public void singleShot()
45     {
46         try
47         {
48             camera.takePicture(new HashMap<String, Object>(), this);
49         }
50         catch (Exception e)
51         {
52             e.printStackTrace();
53         }
54     }
55
56     @Override
57     public void onProgress(OLYCamera olyCamera, OLYCamera.TakingProgress takingProgress, OLYCameraAutoFocusResult olyCameraAutoFocusResult)
58     {
59         if (takingProgress != OLYCamera.TakingProgress.EndFocusing)
60         {
61             return;
62         }
63
64         String result = olyCameraAutoFocusResult.getResult();
65         if (result == null)
66         {
67             Log.v(TAG, "FocusResult is null.");
68         }
69         else switch (result)
70         {
71             case "ok":
72                 RectF postFocusFrameRect = olyCameraAutoFocusResult.getRect();
73                 if (postFocusFrameRect != null)
74                 {
75                     showFocusFrame(postFocusFrameRect, IAutoFocusFrameDisplay.FocusFrameStatus.Focused, 0.0);
76                 }
77                 break;
78
79             case "none":
80             default:
81                 hideFocusFrame();
82                 break;
83         }
84     }
85
86     @Override
87     public void onCompleted()
88     {
89         try
90         {
91             camera.clearAutoFocusPoint();
92             hideFocusFrame();
93             // 撮影成功をバイブレータで知らせる
94             statusDrawer.vibrate(IShowInformation.VIBRATE_PATTERN_SIMPLE_MIDDLE);
95             {
96                 // 撮影成功の表示をToastで行う
97                 //Toast.makeText(context, R.string.shoot_camera, Toast.LENGTH_SHORT).show();
98             }
99         }
100         catch (Exception e)
101         {
102             e.printStackTrace();
103         }
104     }
105
106     @Override
107     public void onErrorOccurred(Exception e)
108     {
109         try
110         {
111             camera.clearAutoFocusPoint();
112             hideFocusFrame();
113             // 撮影失敗をバイブレータで知らせる
114             statusDrawer.vibrate(IShowInformation.VIBRATE_PATTERN_SIMPLE_SHORT);
115             {
116                 // 撮影失敗の表示をToastで行う
117                 //Toast.makeText(context, R.string.shoot_camera_failure, Toast.LENGTH_SHORT).show();
118             }
119         }
120         catch (Exception ee)
121         {
122             ee.printStackTrace();
123         }
124         e.printStackTrace();
125     }
126
127     private void showFocusFrame(RectF rect, IAutoFocusFrameDisplay.FocusFrameStatus status, double duration)
128     {
129         if (frameDisplayer != null)
130         {
131             frameDisplayer.showFocusFrame(rect, status, duration);
132         }
133         indicator.onAfLockUpdate(IAutoFocusFrameDisplay.FocusFrameStatus.Focused == status);
134     }
135
136     private void hideFocusFrame()
137     {
138         if (frameDisplayer != null)
139         {
140             frameDisplayer.hideFocusFrame();
141         }
142         indicator.onAfLockUpdate(false);
143     }
144 }