按应用场景,区分为拍摄模式和浏览模式

chengzhenyu 6 gadi atpakaļ
vecāks
revīzija
0c16a5fd79

+ 69 - 21
app/src/main/java/ai/pai/ptp/test/MyTestActivity.java

@@ -4,6 +4,8 @@ import android.app.Activity;
4 4
 import android.content.Intent;
5 5
 import android.graphics.Bitmap;
6 6
 import android.os.Bundle;
7
+import android.os.Handler;
8
+import android.os.Message;
7 9
 import android.view.View;
8 10
 import android.widget.GridView;
9 11
 import android.widget.ImageView;
@@ -15,10 +17,12 @@ import com.remoteyourcam.usb.ptp.PtpService;
15 17
 import com.remoteyourcam.usb.ptp.model.LiveViewData;
16 18
 import com.remoteyourcam.usb.ptp.model.ObjectInfo;
17 19
 
20
+import java.util.ArrayList;
21
+
18 22
 import ai.pai.ptp.R;
19 23
 
20 24
 
21
-public class MyTestActivity extends Activity implements Camera.CameraListener, Camera.StorageInfoListener, Camera.RetrieveImageInfoListener {
25
+public class MyTestActivity extends Activity implements Handler.Callback, Camera.CameraListener, Camera.StorageInfoListener, Camera.RetrieveImageInfoListener {
22 26
 
23 27
     private ImageView photoIv;
24 28
     private TestLogListAdapter adapter;
@@ -27,13 +31,19 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
27 31
     private PtpService ptp;
28 32
     private Camera camera;
29 33
     private boolean isInStart;
30
-    private int mode = 0;  //0 拍照新增模式 1 照片浏览模式
34
+
35
+    private Handler mainHandler;
36
+    private int mode = 0;  //0 拍照新增模式,模拟的是我们之前的session 1 照片浏览模式
37
+    private int[] photoHandles = new int[0];
38
+    private boolean initHandlesBeforeCapture = false;
39
+    private final static int MSG_CHECK_STORAGE = 100;
31 40
 
32 41
     @Override
33 42
     protected void onCreate(Bundle savedInstanceState) {
34 43
         super.onCreate(savedInstanceState);
35 44
         setContentView(R.layout.activity_my_test);
36 45
 
46
+        mainHandler = new Handler(this);
37 47
         ptp = PtpService.Singleton.getInstance(this);
38 48
 
39 49
         photoIv = (ImageView) findViewById(R.id.iv_latest_photo);
@@ -52,7 +62,6 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
52 62
                     adapter.addInfo(camera.getDeviceInfo());
53 63
                 } else {
54 64
                     adapter.addInfo("未发现相机设备");
55
-                    ptp.initialize(MyTestActivity.this, getIntent());
56 65
                 }
57 66
             }
58 67
         });
@@ -61,11 +70,16 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
61 70
             @Override
62 71
             public void onClick(View v) {
63 72
                 if (camera != null) {
73
+                    if (mode == 1) {
74
+                        photoGridAdapter.clear();
75
+                        photoHandles = new int[0];
76
+                        mainHandler.sendEmptyMessage(MSG_CHECK_STORAGE);
77
+                    }
64 78
                     mode = 0;
65
-                    photoGridAdapter.clear();
79
+                    initHandlesBeforeCapture = false;
66 80
                     camera.capture();
67 81
                 } else {
68
-                    ptp.initialize(MyTestActivity.this, getIntent());
82
+                    adapter.addInfo("未发现相机设备");
69 83
                 }
70 84
             }
71 85
         });
@@ -76,9 +90,10 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
76 90
                 if (camera != null) {
77 91
                     mode = 1;
78 92
                     photoGridAdapter.clear();
79
-                    camera.retrieveImageHandles(MyTestActivity.this, 0xFFFFFFFF, PtpConstants.ObjectFormat.EXIF_JPEG);
93
+                    photoHandles = new int[0];
94
+                    mainHandler.sendEmptyMessage(MSG_CHECK_STORAGE);
80 95
                 } else {
81
-                    ptp.initialize(MyTestActivity.this, getIntent());
96
+                    adapter.addInfo("未发现相机设备");
82 97
                 }
83 98
             }
84 99
         });
@@ -96,6 +111,7 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
96 111
     protected void onStop() {
97 112
         super.onStop();
98 113
         isInStart = false;
114
+        mainHandler.removeCallbacksAndMessages(null);
99 115
         ptp.setCameraListener(null);
100 116
         if (isFinishing()) {
101 117
             ptp.shutdown();
@@ -112,16 +128,28 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
112 128
     }
113 129
 
114 130
     @Override
131
+    public boolean handleMessage(Message msg) {
132
+        if (msg.what == MSG_CHECK_STORAGE) {
133
+            if (camera != null) {
134
+                mainHandler.removeMessages(MSG_CHECK_STORAGE);
135
+                camera.retrieveImageHandles(MyTestActivity.this, 0xFFFFFFFF, PtpConstants.ObjectFormat.EXIF_JPEG);
136
+                mainHandler.sendEmptyMessageDelayed(MSG_CHECK_STORAGE, 1000);
137
+            }
138
+        }
139
+        return false;
140
+    }
141
+
142
+    @Override
115 143
     public void onCameraStarted(Camera camera) {
116 144
         this.camera = camera;
117 145
         adapter.addInfo("开始会话");
118
-
119 146
     }
120 147
 
121 148
     @Override
122 149
     public void onCameraStopped(Camera camera) {
123 150
         this.camera = null;
124 151
         adapter.addInfo("结束会话");
152
+        mainHandler.removeCallbacksAndMessages(null);
125 153
     }
126 154
 
127 155
     @Override
@@ -132,6 +160,7 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
132 160
     @Override
133 161
     public void onError(String message) {
134 162
         adapter.addInfo("发生错误 " + message + "\n请重新打开相机");
163
+        mainHandler.removeCallbacksAndMessages(null);
135 164
         camera = null;
136 165
     }
137 166
 
@@ -174,7 +203,7 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
174 203
         photo.thumb = thumbnail;
175 204
         photo.origin = bitmap;
176 205
         photo.captureTime = System.currentTimeMillis() + "";
177
-        photoGridAdapter.addPhoto(photo);
206
+        photoGridAdapter.updatePhoto(photo);
178 207
     }
179 208
 
180 209
     @Override
@@ -226,12 +255,6 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
226 255
     @Override
227 256
     public void onStorageFound(final int handle, String label) {
228 257
         adapter.addInfo("找到相机存储卡位置,句柄=" + handle + " label = " + label);
229
-//        photoIv.post(new Runnable() {
230
-////            @Override
231
-////            public void run() {
232
-////                camera.retrieveImageHandles(MyTestActivity.this, handle, PtpConstants.ObjectFormat.EXIF_JPEG);
233
-////            }
234
-////        });
235 258
     }
236 259
 
237 260
     @Override
@@ -241,19 +264,44 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
241 264
 
242 265
     @Override
243 266
     public void onImageHandlesRetrieved(final int[] handles) {
244
-        adapter.addInfo("找到相机存储图像文件,共" + handles.length + "张");
245
-        photoIv.post(new Runnable() {
267
+        mainHandler.post(new Runnable() {
246 268
             @Override
247 269
             public void run() {
248
-                photoGridAdapter.addPhotos(handles);
270
+                if (handles.length != photoHandles.length) {
271
+                    adapter.addInfo("找到相机存储图像文件,共" + handles.length + "张");
272
+                }
273
+                ArrayList<Integer> diff = new ArrayList<>();
274
+                for (int i = 0; i < handles.length; i++) {
275
+                    boolean isMatch = false;
276
+                    for (int j = 0; j < photoHandles.length; j++) {
277
+                        if (photoHandles[j] == handles[i]) {
278
+                            isMatch = true;
279
+                            break;
280
+                        }
281
+                    }
282
+                    if (!isMatch) {
283
+                        diff.add(handles[i]);
284
+                    }
285
+                }
286
+                if (diff.size() > 0) {
287
+                    int[] diffArray = new int[diff.size()];
288
+                    for (int k = 0; k < diff.size(); k++) {
289
+                        diffArray[k] = diff.get(k);
290
+                    }
291
+                    if ((mode == 0 && initHandlesBeforeCapture) || mode == 1) {
292
+                        photoGridAdapter.addPhotos(diffArray);
293
+                    }
294
+                }
295
+                photoHandles = handles;
296
+                initHandlesBeforeCapture = true;
249 297
             }
250 298
         });
251 299
     }
252 300
 
253 301
     @Override
254 302
     public void onImageInfoRetrieved(final int objectHandle, final ObjectInfo objectInfo, final Bitmap thumbnail) {
255
-        adapter.addInfo("找到相机存储图像文件,句柄=" + objectHandle + " 文件名 = " + objectInfo.filename + " 拍摄日期 = "+ objectInfo.captureDate);
256
-        photoIv.post(new Runnable() {
303
+        adapter.addInfo("找到相机存储图像文件,句柄=" + objectHandle + " 文件名 = " + objectInfo.filename + " 拍摄日期 = " + objectInfo.captureDate);
304
+        mainHandler.post(new Runnable() {
257 305
             @Override
258 306
             public void run() {
259 307
                 Photo photo = new Photo();
@@ -267,7 +315,7 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
267 315
     }
268 316
 
269 317
     public void retrieveImageInfo(final int handle) {
270
-        photoIv.post(new Runnable() {
318
+        mainHandler.post(new Runnable() {
271 319
             @Override
272 320
             public void run() {
273 321
                 if (camera != null) {

+ 6 - 1
app/src/main/java/ai/pai/ptp/test/TestPhotoGridAdapter.java

@@ -57,15 +57,20 @@ public class TestPhotoGridAdapter extends BaseAdapter {
57 57
         gridView.post(new Runnable() {
58 58
             @Override
59 59
             public void run() {
60
+                boolean isFound = false;
60 61
                 for(int k = 0; k < photos.size();k++){
61 62
                     if(photos.get(k).handle == newPhoto.handle){
62 63
                         photos.get(k).captureTime = newPhoto.captureTime;
63 64
                         photos.get(k).thumb = newPhoto.thumb;
64 65
                         photos.get(k).origin = newPhoto.origin;
65 66
                         photos.get(k).fileName = newPhoto.fileName;
67
+                        isFound = true;
66 68
                         break;
67 69
                     }
68 70
                 }
71
+                if(!isFound){
72
+                    photos.add(0,newPhoto);
73
+                }
69 74
                 notifyDataSetChanged();
70 75
             }
71 76
         });
@@ -81,7 +86,7 @@ public class TestPhotoGridAdapter extends BaseAdapter {
81 86
                     photo.handle = handles[k];
82 87
                     tmp.add(photo);
83 88
                 }
84
-                photos.addAll(tmp);
89
+                photos.addAll(0,tmp);
85 90
                 notifyDataSetChanged();
86 91
             }
87 92
         });

+ 2 - 0
app/src/main/java/com/remoteyourcam/usb/ptp/Camera.java

@@ -145,6 +145,8 @@ public interface Camera {
145 145
 
146 146
     void capture();
147 147
 
148
+    void openCapture();
149
+
148 150
     boolean isLiveViewSupported();
149 151
 
150 152
     boolean isLiveViewAfAreaSupported();

+ 6 - 0
app/src/main/java/com/remoteyourcam/usb/ptp/PtpCamera.java

@@ -28,6 +28,7 @@ import com.remoteyourcam.usb.ptp.commands.GetDevicePropValueCommand;
28 28
 import com.remoteyourcam.usb.ptp.commands.GetObjectHandlesCommand;
29 29
 import com.remoteyourcam.usb.ptp.commands.GetStorageInfosAction;
30 30
 import com.remoteyourcam.usb.ptp.commands.InitiateCaptureCommand;
31
+import com.remoteyourcam.usb.ptp.commands.InitiateOpenCaptureCommand;
31 32
 import com.remoteyourcam.usb.ptp.commands.OpenSessionCommand;
32 33
 import com.remoteyourcam.usb.ptp.commands.RetrieveImageAction;
33 34
 import com.remoteyourcam.usb.ptp.commands.RetrieveImageInfoAction;
@@ -804,6 +805,11 @@ public abstract class PtpCamera implements Camera {
804 805
     }
805 806
 
806 807
     @Override
808
+    public void openCapture() {
809
+        queue.add(new InitiateOpenCaptureCommand(this));
810
+    }
811
+
812
+    @Override
807 813
     public boolean isAutoFocusSupported() {
808 814
         return autoFocusSupported;
809 815
     }

+ 44 - 0
app/src/main/java/com/remoteyourcam/usb/ptp/commands/InitiateOpenCaptureCommand.java

@@ -0,0 +1,44 @@
1
+/**
2
+ * Copyright 2013 Nils Assbeck, Guersel Ayaz and Michael Zoech
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+package com.remoteyourcam.usb.ptp.commands;
17
+
18
+import com.remoteyourcam.usb.ptp.PtpCamera;
19
+import com.remoteyourcam.usb.ptp.PtpCamera.IO;
20
+import com.remoteyourcam.usb.ptp.PtpConstants.Operation;
21
+import com.remoteyourcam.usb.ptp.PtpConstants.Response;
22
+
23
+import java.nio.ByteBuffer;
24
+
25
+public class InitiateOpenCaptureCommand extends Command {
26
+
27
+    public InitiateOpenCaptureCommand(PtpCamera camera) {
28
+        super(camera);
29
+    }
30
+
31
+    @Override
32
+    public void exec(IO io) {
33
+        io.handleCommand(this);
34
+        if (responseCode == Response.DeviceBusy) {
35
+            camera.onDeviceBusy(this, true); // TODO when nikon live view is enabled this stalls
36
+            return;
37
+        }
38
+    }
39
+
40
+    @Override
41
+    public void encodeCommand(ByteBuffer b) {
42
+        encodeCommand(b, Operation.InitiateOpenCapture, 0, 0);
43
+    }
44
+}