OSDN Git Service

PowerShotZoomで動作するようにしてみる。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / canon / wrapper / liveview / CanonLiveViewControl.java
1 package net.osdn.gokigen.a01d.camera.canon.wrapper.liveview;
2
3 import android.app.Activity;
4 import android.util.Log;
5
6 import androidx.annotation.NonNull;
7
8 import net.osdn.gokigen.a01d.camera.ILiveViewControl;
9 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
10 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandPublisher;
11 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommunication;
12 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGenericWithRetry;
13 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback;
14 import net.osdn.gokigen.a01d.liveview.liveviewlistener.IImageDataReceiver;
15 import net.osdn.gokigen.a01d.liveview.liveviewlistener.ILiveViewListener;
16
17 import java.util.Arrays;
18 import java.util.Map;
19
20 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_GET_VIEWFRAME;
21
22 public class CanonLiveViewControl implements ILiveViewControl, ILiveViewListener, IPtpIpCommunication, IPtpIpLiveViewImageCallback
23 {
24     private final String TAG = this.toString();
25     private final IPtpIpCommandPublisher commandIssuer;
26     private final int delayMs;
27     private final boolean isDumpLog = false;
28     private final boolean isSearchJpegHeader;
29     private final int retryCount = 1500;
30     private final CanonLiveViewImageReceiver imageReceiver;
31     private IImageDataReceiver dataReceiver = null;
32     private boolean liveViewIsReceiving = false;
33     private boolean commandIssued = false;
34
35     public CanonLiveViewControl(@NonNull Activity context, @NonNull IPtpIpInterfaceProvider interfaceProvider, int delayMs, boolean isSearchJpegHeader)
36     {
37         this.commandIssuer = interfaceProvider.getCommandPublisher();
38         this.isSearchJpegHeader = isSearchJpegHeader;
39         this.delayMs = delayMs;
40         //this.imageReceiver = new CanonLiveViewImageReceiver(this);
41         this.imageReceiver = new CanonLiveViewImageReceiver(context,this);
42         Log.v(TAG, " -=-=-=-=-=- CanonLiveViewControl : delay " + delayMs + " ms");
43     }
44
45     public ILiveViewListener getLiveViewListener()
46     {
47         return (this);
48     }
49
50     @Override
51     public void changeLiveViewSize(String size)
52     {
53
54     }
55
56     @Override
57     public void startLiveView()
58     {
59         Log.v(TAG, " startLiveView() ");
60         liveViewIsReceiving = true;
61         try
62         {
63             Thread thread = new Thread(new Runnable() {
64                 @Override
65                 public void run()
66                 {
67                     try
68                     {
69                         while (liveViewIsReceiving)
70                         {
71                             if (!commandIssued)
72                             {
73                                 if (isDumpLog)
74                                 {
75                                     Log.v(TAG, " enqueueCommand() ");
76                                 }
77                                 commandIssued = true;
78                                 commandIssuer.enqueueCommand(new PtpIpCommandGenericWithRetry(imageReceiver, SEQ_GET_VIEWFRAME, delayMs, retryCount, false, false, 0, 0x9153, 12, 0x00200000, 0x01, 0x00, 0x00));
79                             }
80                             try
81                             {
82                                 Thread.sleep(delayMs);
83                             }
84                             catch (Exception e)
85                             {
86                                 e.printStackTrace();
87                             }
88                         }
89                     }
90                     catch (Exception e)
91                     {
92                         e.printStackTrace();
93                     }
94                 }
95             });
96             thread.start();
97         }
98         catch (Exception e)
99         {
100             e.printStackTrace();
101         }
102     }
103
104     @Override
105     public void stopLiveView()
106     {
107         Log.v(TAG, " stopLiveView() ");
108         liveViewIsReceiving = false;
109
110     }
111
112     @Override
113     public void updateDigitalZoom()
114     {
115         Log.v(TAG, " updateDigitalZoom() ");
116
117     }
118
119     @Override
120     public void updateMagnifyingLiveViewScale(boolean isChangeScale)
121     {
122         Log.v(TAG, " updateMagnifyingLiveViewScale() ");
123     }
124
125     @Override
126     public float getMagnifyingLiveViewScale()
127     {
128         return 0;
129     }
130
131     @Override
132     public float getDigitalZoomScale()
133     {
134         return 0;
135     }
136
137     @Override
138     public void setCameraLiveImageView(IImageDataReceiver target)
139     {
140         Log.v(TAG, " setCameraLiveImageView() ");
141         this.dataReceiver = target;
142     }
143
144     @Override
145     public boolean connect()
146     {
147         Log.v(TAG, " connect() ");
148         return (true);
149     }
150
151     @Override
152     public void disconnect()
153     {
154         Log.v(TAG, " disconnect() ");
155     }
156
157     @Override
158     public void onCompleted(byte[] data, Map<String, Object> metadata)
159     {
160         //Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- ");
161         try
162         {
163             if ((dataReceiver != null)&&(data != null))
164             {
165                 if (isDumpLog)
166                 {
167                     Log.v(TAG, "  ---+++--- RECEIVED LV IMAGE ---+++--- : " + data.length + " bytes.");
168                 }
169                 int headerSize = searchJpegHeader(data);
170                 if (headerSize >= 0)
171                 {
172                     dataReceiver.setImageData(Arrays.copyOfRange(data, headerSize, data.length), metadata);  // ヘッダ部分を切り取って送る
173                 }
174             }
175         }
176         catch (Exception e)
177         {
178             e.printStackTrace();
179         }
180         commandIssued = false;
181     }
182
183     private int searchJpegHeader(byte[] data)
184     {
185         if (data.length <= 8)
186         {
187             return (-1);
188         }
189         if (!isSearchJpegHeader)
190         {
191             // JPEG ヘッダを探さない場合は、8バイト固定とする
192             return (8);
193         }
194         try
195         {
196             int size = data.length - 1;
197             int index = 0;
198             while (index < size)
199             {
200                 if ((data[index] == (byte) 0xff)&&(data[index + 1] == (byte) 0xd8))
201                 {
202                     return (index);
203                 }
204                 index++;
205             }
206         }
207         catch (Exception e)
208         {
209             e.printStackTrace();
210         }
211
212         // 見つからなかったときは 8 を返す
213         return (8);
214     }
215
216     @Override
217     public void onErrorOccurred(Exception e)
218     {
219         Log.v(TAG, " onErrorOccurred () : " + e.getLocalizedMessage());
220         commandIssued = false;
221     }
222 }