1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.liveview;
3 import android.util.Log;
5 import androidx.annotation.NonNull;
6 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
7 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback;
8 import net.osdn.gokigen.a01d.camera.utils.SimpleLogDumper;
10 import java.io.ByteArrayOutputStream;
11 import java.util.Arrays;
13 public class NikonLiveViewImageReceiver implements IPtpIpCommandCallback
15 private final String TAG = toString();
17 private IPtpIpLiveViewImageCallback callback;
19 private int received_total_bytes = 0;
20 private int received_remain_bytes = 0;
22 private int target_image_size = 0;
23 private boolean receivedFirstData = false;
24 private ByteArrayOutputStream byteStream;
26 NikonLiveViewImageReceiver(@NonNull IPtpIpLiveViewImageCallback callback)
28 this.callback = callback;
29 byteStream = new ByteArrayOutputStream();
33 public void receivedMessage(int id, byte[] rx_body)
37 // end of receive sequence.
38 //byte [] thumbnail = byteStream.toByteArray();
39 //Log.v(TAG, " TransferComplete() RECEIVED : " + id + " size : " + target_image_size + " (" + thumbnail.length + ")");
40 //SimpleLogDumper.dump_bytes(" [xxxxx]", Arrays.copyOfRange(thumbnail, 0, (64)));
41 //SimpleLogDumper.dump_bytes(" [zzzzz]", Arrays.copyOfRange(thumbnail, (thumbnail.length - 64), (thumbnail.length)));
42 callback.onCompleted(byteStream.toByteArray(), null);
43 receivedFirstData = false;
44 received_remain_bytes = 0;
45 received_total_bytes = 0;
46 target_image_size = 0;
54 callback.onErrorOccurred(e);
60 public void onReceiveProgress(final int currentBytes, final int totalBytes, byte[] rx_body)
62 // Log.v(TAG, " onReceiveProgress() " + currentBytes + "/" + totalBytes);
64 // 受信したデータから、通信のヘッダ部分を削除する
68 private void cutHeader(byte[] rx_body)
74 int length = rx_body.length;
75 int data_position = 0;
76 if (!receivedFirstData)
78 // データを最初に読んだとき。ヘッダ部分を読み飛ばす
79 receivedFirstData = true;
80 data_position = (int) rx_body[0] & (0xff);
81 //Log.v(TAG, " FIRST DATA POS. : " + data_position);
82 //SimpleLogDumper.dump_bytes(" [sssss]", Arrays.copyOfRange(rx_body, 0, (64)));
84 else if (received_remain_bytes > 0)
86 // データの読み込みが途中だった場合...
87 if (length < received_remain_bytes)
89 // 全部コピーする、足りないバイト数は残す
90 received_remain_bytes = received_remain_bytes - length;
91 received_total_bytes = received_total_bytes + rx_body.length;
92 byteStream.write(rx_body, 0, rx_body.length);
97 byteStream.write(rx_body, data_position, received_remain_bytes);
98 data_position = received_remain_bytes;
99 received_remain_bytes = 0;
103 while (data_position <= (length - 12)) {
104 int body_size = (rx_body[data_position] & 0xff) + ((rx_body[data_position + 1] & 0xff) << 8) +
105 ((rx_body[data_position + 2] & 0xff) << 16) + ((rx_body[data_position + 3] & 0xff) << 24);
106 if (body_size <= 12) {
107 Log.v(TAG, " --- BODY SIZE IS SMALL : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] " + rx_body.length + " (" + target_image_size + ")");
108 //int startpos = (data_position > 48) ? (data_position - 48) : 0;
109 //SimpleLogDumper.dump_bytes(" [xxx]", Arrays.copyOfRange(rx_body, startpos, (data_position + 48)));
113 // 受信データ(のヘッダ部分)をダンプする
114 Log.v(TAG, " RX DATA : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] (" + received_total_bytes + ")");
115 SimpleLogDumper.dump_bytes(" [zzz] " + data_position + ": ", Arrays.copyOfRange(rx_body, data_position, (data_position + 48)));
117 if ((data_position + body_size) > length) {
118 // データがすべてバッファ内になかったときは、バッファすべてコピーして残ったサイズを記憶しておく。
119 int copysize = (length - ((data_position + 12)));
120 byteStream.write(rx_body, (data_position + 12), copysize);
121 received_remain_bytes = body_size - copysize - 12; // マイナス12は、ヘッダ分
122 received_total_bytes = received_total_bytes + copysize;
123 //Log.v(TAG, " ----- copy : " + (data_position + 12) + " " + copysize + " remain : " + received_remain_bytes + " body size : " + body_size);
127 byteStream.write(rx_body, (data_position + 12), (body_size - 12));
128 data_position = data_position + body_size;
129 received_total_bytes = received_total_bytes + 12;
130 //Log.v(TAG, " --- COPY : " + (data_position + 12) + " " + (body_size - 12) + " remain : " + received_remain_bytes);
132 } catch (Exception e) {
133 Log.v(TAG, " pos : " + data_position + " size : " + body_size + " length : " + length);
140 public boolean isReceiveMulti()