Pentax DSLR Remote Control app.
Revision | 4bef75d5b1960aec67411fd3e8943807c437d6b1 (tree) |
---|---|
Time | 2019-10-08 23:42:04 |
Author | MRSa <mrsa@myad...> |
Commiter | MRSa |
フルサイズは途中。
@@ -7,10 +7,14 @@ import androidx.annotation.NonNull; | ||
7 | 7 | |
8 | 8 | import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentCallback; |
9 | 9 | import net.osdn.gokigen.pkremote.camera.interfaces.playback.IProgressEvent; |
10 | +import net.osdn.gokigen.pkremote.camera.utils.SimpleLogDumper; | |
10 | 11 | import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandCallback; |
11 | 12 | import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandPublisher; |
12 | 13 | import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.messages.PtpIpCommandGeneric; |
13 | 14 | |
15 | +import java.io.ByteArrayOutputStream; | |
16 | +import java.util.Arrays; | |
17 | + | |
14 | 18 | public class PtpIpFullImageReceiver implements IPtpIpCommandCallback |
15 | 19 | { |
16 | 20 | private static final String TAG = PtpIpFullImageReceiver.class.getSimpleName(); |
@@ -20,6 +24,9 @@ public class PtpIpFullImageReceiver implements IPtpIpCommandCallback | ||
20 | 24 | private IDownloadContentCallback callback = null; |
21 | 25 | private int objectId = 0; |
22 | 26 | |
27 | + private int received_total_bytes = 0; | |
28 | + private int received_remain_bytes = 0; | |
29 | + | |
23 | 30 | PtpIpFullImageReceiver(@NonNull Activity activity, @NonNull IPtpIpCommandPublisher publisher) |
24 | 31 | { |
25 | 32 | this.activity = activity; |
@@ -77,9 +84,10 @@ public class PtpIpFullImageReceiver implements IPtpIpCommandCallback | ||
77 | 84 | @Override |
78 | 85 | public void onReceiveProgress(final int currentBytes, final int totalBytes, byte[] rx_body) |
79 | 86 | { |
80 | - int length = (rx_body == null) ? 0 : rx_body.length; | |
87 | + byte[] body = cutHeader(rx_body); | |
88 | + int length = (body == null) ? 0 : body.length; | |
81 | 89 | Log.v(TAG, " onReceiveProgress() " + currentBytes + "/" + totalBytes + " (" + length + " bytes.)"); |
82 | - callback.onProgress(rx_body, length, new IProgressEvent() { | |
90 | + callback.onProgress(body, length, new IProgressEvent() { | |
83 | 91 | @Override |
84 | 92 | public float getProgress() { |
85 | 93 | return ((float) currentBytes / (float) totalBytes); |
@@ -97,6 +105,87 @@ public class PtpIpFullImageReceiver implements IPtpIpCommandCallback | ||
97 | 105 | }); |
98 | 106 | } |
99 | 107 | |
108 | + private byte[] cutHeader(byte[] rx_body) | |
109 | + { | |
110 | + if (rx_body == null) | |
111 | + { | |
112 | + return (null); | |
113 | + } | |
114 | + int length = rx_body.length; | |
115 | + int data_position = 0; | |
116 | + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); | |
117 | + if (received_total_bytes == 0) | |
118 | + { | |
119 | + // データを最初に読んだとき。ヘッダ部分を読み飛ばす | |
120 | + data_position = (int) rx_body[0] & (0xff); | |
121 | + } | |
122 | + else if (received_remain_bytes > 0) | |
123 | + { | |
124 | + | |
125 | + Log.v(TAG, " >>> [ remain_bytes : " + received_remain_bytes + "] ( length : " + length + ") " + data_position); | |
126 | + SimpleLogDumper.dump_bytes("[zzz]", Arrays.copyOfRange(rx_body, data_position, (data_position + 160))); | |
127 | + | |
128 | + // データの読み込みが途中だった場合... | |
129 | + if (length < received_remain_bytes) | |
130 | + { | |
131 | + // 全部コピーする、足りないバイト数は残す | |
132 | + received_remain_bytes = received_remain_bytes - length; | |
133 | + received_total_bytes = received_total_bytes + rx_body.length; | |
134 | + return (rx_body); | |
135 | + } | |
136 | + else | |
137 | + { | |
138 | + byteStream.write(rx_body, data_position, received_remain_bytes); | |
139 | + data_position = received_remain_bytes; | |
140 | + received_remain_bytes = 0; | |
141 | + } | |
142 | + } | |
143 | + | |
144 | + while (data_position <= (length - 12)) | |
145 | + { | |
146 | + int body_size = (rx_body[data_position] & 0xff) + ((rx_body[data_position + 1] & 0xff) << 8) + | |
147 | + ((rx_body[data_position + 2] & 0xff) << 16) + ((rx_body[data_position + 3] & 0xff) << 24); | |
148 | + if (body_size <= 12) | |
149 | + { | |
150 | + Log.v(TAG, " BODY SIZE IS SMALL : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] " + rx_body.length + " "); | |
151 | + | |
152 | + int startpos = (data_position > 48) ? (data_position - 48) : 0; | |
153 | + SimpleLogDumper.dump_bytes("[xxx]", Arrays.copyOfRange(rx_body, startpos, (data_position + 48))); | |
154 | + | |
155 | + break; | |
156 | + } | |
157 | + | |
158 | + Log.v(TAG, " RX DATA : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] (" + received_total_bytes + ")"); | |
159 | + SimpleLogDumper.dump_bytes("[yyy] " + data_position + ": ", Arrays.copyOfRange(rx_body, data_position, (data_position + 64))); | |
160 | + | |
161 | + | |
162 | + if ((data_position + body_size) > length) | |
163 | + { | |
164 | + // データがすべてバッファ内になかったときは、バッファすべてコピーして残ったサイズを記憶しておく。 | |
165 | + int copysize = (length - ((data_position + 12))); | |
166 | + byteStream.write(rx_body, (data_position + 12), copysize); | |
167 | + received_remain_bytes = body_size - copysize - 12; // マイナス12は、ヘッダ分 | |
168 | + received_total_bytes = received_total_bytes + copysize; | |
169 | + Log.v(TAG, " --- copy : " + (data_position + 12) + " " + copysize + " remain : " + received_remain_bytes + " body size : " + body_size); | |
170 | + break; | |
171 | + } | |
172 | + try | |
173 | + { | |
174 | + byteStream.write(rx_body, (data_position + 12), (body_size - 12)); | |
175 | + data_position = data_position + body_size; | |
176 | + received_total_bytes = received_total_bytes + 12; | |
177 | + Log.v(TAG, " --- COPY : " + (data_position + 12) + " " + (body_size - 12) + " remain : " + received_remain_bytes); | |
178 | + | |
179 | + } | |
180 | + catch (Exception e) | |
181 | + { | |
182 | + Log.v(TAG, " pos : " + data_position + " size : " + body_size + " length : " + length); | |
183 | + e.printStackTrace(); | |
184 | + } | |
185 | + } | |
186 | + return (byteStream.toByteArray()); | |
187 | + } | |
188 | + | |
100 | 189 | @Override |
101 | 190 | public boolean isReceiveMulti() |
102 | 191 | { |