1 package net.osdn.gokigen.pkremote.camera.vendor.ricoh.wrapper;
2 import android.graphics.Bitmap;
3 import android.util.Log;
5 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContent;
6 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContentListCallback;
7 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraFileInfo;
8 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IContentInfoCallback;
9 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentListCallback;
10 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentCallback;
11 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadThumbnailImageCallback;
12 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IPlaybackControl;
13 import net.osdn.gokigen.pkremote.camera.playback.CameraContentInfo;
14 import net.osdn.gokigen.pkremote.camera.playback.CameraFileInfo;
15 import net.osdn.gokigen.pkremote.camera.playback.ProgressEvent;
16 import net.osdn.gokigen.pkremote.camera.utils.SimpleHttpClient;
18 import org.json.JSONArray;
19 import org.json.JSONObject;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Locale;
29 import androidx.annotation.NonNull;
35 public class RicohGr2PlaybackControl implements IPlaybackControl
37 private final String TAG = toString();
38 private final String getPhotoUrl = "http://192.168.0.1/v1/photos/";
39 private final RicohGr2StatusChecker statusChecker;
40 private static final int DEFAULT_TIMEOUT = 7700;
41 private final boolean useGrCommand;
46 画像の一覧をとる : http://192.168.0.1/v1/photos?limit=3000
47 画像の情報をとる : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.DNG/info
48 サムネール画像をとる(JPEG) : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.JPG?size=thumb
49 サムネール画像をとる(DNG) : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.DNG?size=view
50 サムネール画像をとる(MOV) : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.MOV?size=view
51 デバイス表示用画像をとる : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.JPG?size=view
52 画像(JPEG)をダウンロードする : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.JPG?size=full
53 画像(DNG)をダウンロードする : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.DNG?size=full
54 動画をダウンロードする : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.MOV?size=full
57 RicohGr2PlaybackControl(RicohGr2StatusChecker statusChecker, boolean useGrCommand)
59 this.statusChecker = statusChecker;
60 this.useGrCommand = useGrCommand;
64 public String getRawFileSuffix()
70 public void downloadContentList(@NonNull IDownloadContentListCallback callback)
72 List<ICameraFileInfo> fileList = new ArrayList<>();
73 String imageListurl = "http://192.168.0.1/v1/photos?limit=3000";
77 contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
78 if (contentList == null)
81 callback.onErrorOccurred(new NullPointerException());
88 callback.onErrorOccurred(e);
93 JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
94 if (dirsArray != null)
96 int size = dirsArray.length();
97 for (int index = 0; index < size; index++)
99 JSONObject object = dirsArray.getJSONObject(index);
100 String dirName = object.getString("name");
101 JSONArray filesArray = object.getJSONArray("files");
102 int nofFiles = filesArray.length();
103 for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
105 String fileName = filesArray.getString(fileIndex);
106 fileList.add(new CameraFileInfo(dirName, fileName));
113 callback.onErrorOccurred(e);
116 callback.onCompleted(fileList);
120 public void updateCameraFileInfo(ICameraFileInfo info)
122 String url = getPhotoUrl + info.getDirectoryPath() + "/" + info.getFilename() + "/info";
123 Log.v(TAG, "updateCameraFileInfo() GET URL : " + url);
126 String response = SimpleHttpClient.httpGet(url, DEFAULT_TIMEOUT);
127 if ((response == null)||(response.length() < 1))
131 JSONObject object = new JSONObject(response);
134 boolean captured = object.getBoolean("captured");
135 String av = getJSONString(object, "av");
136 String tv = getJSONString(object, "tv");
137 String sv = getJSONString(object,"sv");
138 String xv = getJSONString(object,"xv");
139 int orientation = object.getInt("orientation");
140 String aspectRatio = getJSONString(object,"aspectRatio");
141 String cameraModel = getJSONString(object,"cameraModel");
142 String latLng = getJSONString(object,"latlng");
143 String dateTime = object.getString("datetime");
144 info.updateValues(dateTime, av, tv, sv, xv, orientation, aspectRatio, cameraModel, latLng, captured);
152 private String getJSONString(JSONObject object, String key)
157 value = object.getString(key);
167 public void getContentInfo(@NonNull String path, @NonNull String name, @NonNull IContentInfoCallback callback)
169 String url = getPhotoUrl + path + "/" + name + "/info";
170 Log.v(TAG, "getContentInfo() GET URL : " + url);
173 String response = SimpleHttpClient.httpGet(url, DEFAULT_TIMEOUT);
174 if ((response == null)||(response.length() < 1))
176 callback.onErrorOccurred(new NullPointerException());
178 CameraFileInfo fileInfo = new CameraFileInfo(path, name);
180 JSONObject object = new JSONObject(response);
182 boolean captured = object.getBoolean("captured");
183 String av = getJSONString(object, "av");
184 String tv = getJSONString(object, "tv");
185 String sv = getJSONString(object,"sv");
186 String xv = getJSONString(object,"xv");
187 int orientation = object.getInt("orientation");
188 String aspectRatio = getJSONString(object,"aspectRatio");
189 String cameraModel = getJSONString(object,"cameraModel");
190 String latLng = getJSONString(object,"latlng");
191 String dateTime = object.getString("datetime");
192 fileInfo.updateValues(dateTime, av, tv, sv, xv, orientation, aspectRatio, cameraModel, latLng, captured);
194 callback.onCompleted(fileInfo);
203 public void downloadContentScreennail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
205 //Log.v(TAG, "downloadContentScreennail() : " + path);
206 String suffix = "?size=view";
207 String url = getPhotoUrl + path + suffix;
208 Log.v(TAG, "downloadContentScreennail() GET URL : " + url);
211 Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, DEFAULT_TIMEOUT);
212 HashMap<String, Object> map = new HashMap<>();
213 map.put("Orientation", 0);
214 callback.onCompleted(bmp, map);
223 public void downloadContentThumbnail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
225 //Log.v(TAG, "downloadContentThumbnail() : " + path);
226 String suffix = "?size=view";
227 if (path.contains(".JPG"))
229 suffix = "?size=thumb";
231 String url = getPhotoUrl + path + suffix;
232 Log.v(TAG, "downloadContentThumbnail() GET URL : " + url);
235 Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, DEFAULT_TIMEOUT);
236 HashMap<String, Object> map = new HashMap<>();
237 map.put("Orientation", 0);
238 callback.onCompleted(bmp, map);
243 callback.onErrorOccurred(new NullPointerException());
248 public void downloadContent(@NonNull String path, boolean isSmallSize, @NonNull final IDownloadContentCallback callback)
250 Log.v(TAG, "downloadContent() : " + path);
251 String suffix = "?size=full";
254 suffix = "?size=view";
256 String url = getPhotoUrl + path + suffix;
257 Log.v(TAG, "downloadContent() GET URL : " + url);
260 SimpleHttpClient.httpGetBytes(url, DEFAULT_TIMEOUT, new SimpleHttpClient.IReceivedMessageCallback() {
262 public void onCompleted() {
263 callback.onCompleted();
267 public void onErrorOccurred(Exception e) {
268 callback.onErrorOccurred(e);
272 public void onReceive(int readBytes, int length, int size, byte[] data) {
273 float percent = (length == 0) ? 0.0f : ((float) readBytes / (float) length);
274 ProgressEvent event = new ProgressEvent(percent, null);
275 callback.onProgress(data, size, event);
286 * 撮影時刻は(個別に)取れるが、非常に遅い...
289 private Date getCameraContentDate(@NonNull ICameraContent cameraContent)
291 // 各ファイルを個別に撮影時刻をとると、反応が悪すぎるので処理を抑止。
296 String imageInfoUrl = "http://192.168.0.1/v1/photos/" + cameraContent.getContentPath() + "/" + cameraContent.getContentName() + "/info?storage=" + cameraContent.getCardId();
297 //Log.v(TAG, "getCameraContentDate() : " + imageInfoUrl);
298 fileInfo = SimpleHttpClient.httpGet(imageInfoUrl, DEFAULT_TIMEOUT);
299 if (fileInfo != null)
301 String datetime = new JSONObject(fileInfo).getString("datetime");
302 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); // "yyyy-MM-dd'T'HH:mm:ssZ"
303 dateFormatter.setCalendar(new GregorianCalendar());
304 return (dateFormatter.parse(datetime));
312 return (cameraContent.getCapturedDate());
317 * カメラ内画像ファイルの取得処理... GRコマンドが失敗したらPENTAXコマンドを使う。
321 public void getCameraContentList(ICameraContentListCallback callback)
327 getGrCameraContentListImpl(callback);
335 getCameraContentListImpl(callback);
339 * RICOH GR2用のカメラ内画像ファイル一覧取得処理
340 * (エラー発生時には、通常のPENTAX用のカメラ内画像ファイル一覧取得処理を使う)
343 private void getGrCameraContentListImpl(ICameraContentListCallback callback)
345 List<ICameraContent> fileList = new ArrayList<>();
346 String imageListurl = "http://192.168.0.1/_gr/objs";
349 // try ~ catch でくくらない ... だめだったら PENTAXのシーケンスに入るようにしたいので
350 contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
351 if (contentList == null)
354 throw (new NullPointerException());
359 String cameraId = statusChecker.getCameraId();
360 JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
361 if (dirsArray != null)
363 int size = dirsArray.length();
364 for (int index = 0; index < size; index++)
366 JSONObject object = dirsArray.getJSONObject(index);
367 String dirName = object.getString("name");
368 JSONArray filesArray = object.getJSONArray("files");
369 int nofFiles = filesArray.length();
370 for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
372 JSONObject fileObject = filesArray.getJSONObject(fileIndex);
373 String fileName = fileObject.getString("n");
374 String dateString = fileObject.getString("d");
375 Date capturedDate = new Date(2001, 1, 1);
376 if (dateString != null)
378 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); // "yyyy-MM-dd'T'HH:mm:ssZ"
379 dateFormatter.setCalendar(new GregorianCalendar());
380 capturedDate = dateFormatter.parse(dateString);
382 ICameraContent cameraContent = new CameraContentInfo(cameraId, "sd1", dirName, fileName, capturedDate);
383 fileList.add(cameraContent);
391 throw (new NullPointerException());
393 callback.onCompleted(fileList);
396 private void getCameraContentListImpl(ICameraContentListCallback callback)
398 List<ICameraContent> fileList = new ArrayList<>();
399 String imageListurl = "http://192.168.0.1/v1/photos?limit=3000";
403 contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
404 if (contentList == null)
407 callback.onErrorOccurred(new NullPointerException());
414 callback.onErrorOccurred(e);
419 Log.v(TAG, "PHOTO LIST RECV: [" + contentList.length() + "]");
420 String cameraId = statusChecker.getCameraId();
421 JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
422 if (dirsArray != null)
424 int size = dirsArray.length();
425 Log.v(TAG, "DIRECTORIES : " + size);
426 for (int index = 0; index < size; index++)
428 JSONObject object = dirsArray.getJSONObject(index);
429 String dirName = object.getString("name");
430 JSONArray filesArray = object.getJSONArray("files");
431 int nofFiles = filesArray.length();
432 Log.v(TAG, "FILES : [" + dirName + "] " + nofFiles);
433 for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
435 String fileName = filesArray.getString(fileIndex);
436 Log.v(TAG, "FILE : " + fileName);
437 ICameraContent cameraContent = new CameraContentInfo(cameraId, "sd1", dirName, fileName, new Date());
438 cameraContent.setCapturedDate(getCameraContentDate(cameraContent));
439 fileList.add(cameraContent);
445 Log.v(TAG, "NOT FOUND dirs array.");
456 ee.printStackTrace();
459 callback.onCompleted(fileList);