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 = 5000;
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 IContentInfoCallback callback)
169 String url = getPhotoUrl + path + "/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());
186 public void downloadContentScreennail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
188 //Log.v(TAG, "downloadContentScreennail() : " + path);
189 String suffix = "?size=view";
190 String url = getPhotoUrl + path + suffix;
191 Log.v(TAG, "downloadContentScreennail() GET URL : " + url);
194 Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, DEFAULT_TIMEOUT);
195 HashMap<String, Object> map = new HashMap<>();
196 map.put("Orientation", 0);
197 callback.onCompleted(bmp, map);
206 public void downloadContentThumbnail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
208 //Log.v(TAG, "downloadContentThumbnail() : " + path);
209 String suffix = "?size=view";
210 if (path.contains(".JPG"))
212 suffix = "?size=thumb";
214 String url = getPhotoUrl + path + suffix;
215 Log.v(TAG, "downloadContentThumbnail() GET URL : " + url);
218 Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, DEFAULT_TIMEOUT);
219 HashMap<String, Object> map = new HashMap<>();
220 map.put("Orientation", 0);
221 callback.onCompleted(bmp, map);
226 callback.onErrorOccurred(new NullPointerException());
231 public void downloadContent(@NonNull String path, boolean isSmallSize, @NonNull final IDownloadContentCallback callback)
233 Log.v(TAG, "downloadContent() : " + path);
234 String suffix = "?size=full";
237 suffix = "?size=view";
239 String url = getPhotoUrl + path + suffix;
240 Log.v(TAG, "downloadContent() GET URL : " + url);
243 SimpleHttpClient.httpGetBytes(url, DEFAULT_TIMEOUT, new SimpleHttpClient.IReceivedMessageCallback() {
245 public void onCompleted() {
246 callback.onCompleted();
250 public void onErrorOccurred(Exception e) {
251 callback.onErrorOccurred(e);
255 public void onReceive(int readBytes, int length, int size, byte[] data) {
256 float percent = (length == 0) ? 0.0f : ((float) readBytes / (float) length);
257 ProgressEvent event = new ProgressEvent(percent, null);
258 callback.onProgress(data, size, event);
269 * 撮影時刻は(個別に)取れるが、非常に遅い...
272 private Date getCameraContentDate(@NonNull ICameraContent cameraContent)
278 String imageInfoUrl = "http://192.168.0.1/v1/photos/" + cameraContent.getContentPath() + "/" + cameraContent.getContentName() + "/info?storage=" + cameraContent.getCardId();
279 Log.v(TAG, "getCameraContentDate() : " + imageInfoUrl);
280 fileInfo = SimpleHttpClient.httpGet(imageInfoUrl, DEFAULT_TIMEOUT);
281 if (fileInfo != null)
283 String datetime = new JSONObject(fileInfo).getString("datetime");
284 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); // "yyyy-MM-dd'T'HH:mm:ssZ"
285 dateFormatter.setCalendar(new GregorianCalendar());
286 return (dateFormatter.parse(datetime));
294 return (cameraContent.getCapturedDate());
299 * カメラ内画像ファイルの取得処理... GRコマンドが失敗したらPENTAXコマンドを使う。
303 public void getCameraContentList(ICameraContentListCallback callback)
309 getGrCameraContentListImpl(callback);
317 getCameraContentListImpl(callback);
321 * RICOH GR2用のカメラ内画像ファイル一覧取得処理
322 * (エラー発生時には、通常のPENTAX用のカメラ内画像ファイル一覧取得処理を使う)
325 private void getGrCameraContentListImpl(ICameraContentListCallback callback)
327 List<ICameraContent> fileList = new ArrayList<>();
328 String imageListurl = "http://192.168.0.1/_gr/objs";
331 // try ~ catch でくくらない ... だめだったら PENTAXのシーケンスに入るようにしたいので
332 contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
333 if (contentList == null)
336 throw (new NullPointerException());
341 String cameraId = statusChecker.getCameraId();
342 JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
343 if (dirsArray != null)
345 int size = dirsArray.length();
346 for (int index = 0; index < size; index++)
348 JSONObject object = dirsArray.getJSONObject(index);
349 String dirName = object.getString("name");
350 JSONArray filesArray = object.getJSONArray("files");
351 int nofFiles = filesArray.length();
352 for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
354 JSONObject fileObject = filesArray.getJSONObject(fileIndex);
355 String fileName = fileObject.getString("n");
356 String dateString = fileObject.getString("d");
357 Date capturedDate = new Date();
358 if (dateString != null)
360 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); // "yyyy-MM-dd'T'HH:mm:ssZ"
361 dateFormatter.setCalendar(new GregorianCalendar());
362 capturedDate = dateFormatter.parse(dateString);
364 ICameraContent cameraContent = new CameraContentInfo(cameraId, "sd1", dirName, fileName, capturedDate);
365 fileList.add(cameraContent);
373 throw (new NullPointerException());
375 callback.onCompleted(fileList);
378 private void getCameraContentListImpl(ICameraContentListCallback callback)
380 List<ICameraContent> fileList = new ArrayList<>();
381 String imageListurl = "http://192.168.0.1/v1/photos?limit=3000";
385 contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
386 if (contentList == null)
389 callback.onErrorOccurred(new NullPointerException());
396 callback.onErrorOccurred(e);
401 String cameraId = statusChecker.getCameraId();
402 JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
403 if (dirsArray != null)
405 int size = dirsArray.length();
406 for (int index = 0; index < size; index++)
408 JSONObject object = dirsArray.getJSONObject(index);
409 String dirName = object.getString("name");
410 JSONArray filesArray = object.getJSONArray("files");
411 int nofFiles = filesArray.length();
412 for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
414 String fileName = filesArray.getString(fileIndex);
415 ICameraContent cameraContent = new CameraContentInfo(cameraId, "sd1", dirName, fileName, new Date());
416 cameraContent.setCapturedDate(getCameraContentDate(cameraContent));
417 fileList.add(cameraContent);
424 //callback.onErrorOccurred(e);
432 ee.printStackTrace();
435 callback.onCompleted(fileList);