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

chengzhenyu 6 years ago
parent
commit
c8e414e419

+ 48 - 15
app/src/main/java/ai/pai/ptp/test/MyTestActivity.java

@@ -5,6 +5,7 @@ import android.content.Intent;
5 5
 import android.graphics.Bitmap;
6 6
 import android.os.Bundle;
7 7
 import android.view.View;
8
+import android.widget.GridView;
8 9
 import android.widget.ImageView;
9 10
 import android.widget.ListView;
10 11
 
@@ -20,11 +21,13 @@ import ai.pai.ptp.R;
20 21
 public class MyTestActivity extends Activity implements Camera.CameraListener, Camera.StorageInfoListener, Camera.RetrieveImageInfoListener {
21 22
 
22 23
     private ImageView photoIv;
23
-    private MyTestAdapter adapter;
24
-
24
+    private TestLogListAdapter adapter;
25
+    private GridView photoGridView;
26
+    private TestPhotoGridAdapter photoGridAdapter;
25 27
     private PtpService ptp;
26 28
     private Camera camera;
27 29
     private boolean isInStart;
30
+    private int mode = 0;  //0 拍照新增模式 1 照片浏览模式
28 31
 
29 32
     @Override
30 33
     protected void onCreate(Bundle savedInstanceState) {
@@ -35,9 +38,13 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
35 38
 
36 39
         photoIv = (ImageView) findViewById(R.id.iv_latest_photo);
37 40
         ListView infoListView = (ListView) findViewById(R.id.list_info);
38
-        adapter = new MyTestAdapter(this, infoListView);
41
+        adapter = new TestLogListAdapter(this, infoListView);
39 42
         infoListView.setAdapter(adapter);
40 43
 
44
+        photoGridView = (GridView) findViewById(R.id.gridview_photos);
45
+        photoGridAdapter = new TestPhotoGridAdapter(this, photoGridView);
46
+        photoGridView.setAdapter(photoGridAdapter);
47
+
41 48
         findViewById(R.id.btn_get_devece_info).setOnClickListener(new View.OnClickListener() {
42 49
             @Override
43 50
             public void onClick(View v) {
@@ -54,6 +61,8 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
54 61
             @Override
55 62
             public void onClick(View v) {
56 63
                 if (camera != null) {
64
+                    mode = 0;
65
+                    photoGridAdapter.clear();
57 66
                     camera.capture();
58 67
                 } else {
59 68
                     ptp.initialize(MyTestActivity.this, getIntent());
@@ -65,6 +74,8 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
65 74
             @Override
66 75
             public void onClick(View v) {
67 76
                 if (camera != null) {
77
+                    mode = 1;
78
+                    photoGridAdapter.clear();
68 79
                     camera.retrieveImageHandles(MyTestActivity.this, 0xFFFFFFFF, PtpConstants.ObjectFormat.EXIF_JPEG);
69 80
                 } else {
70 81
                     ptp.initialize(MyTestActivity.this, getIntent());
@@ -120,7 +131,7 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
120 131
 
121 132
     @Override
122 133
     public void onError(String message) {
123
-        adapter.addInfo("发生错误 " + message);
134
+        adapter.addInfo("发生错误 " + message + "\n请重新打开相机");
124 135
         camera = null;
125 136
     }
126 137
 
@@ -157,7 +168,13 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
157 168
     @Override
158 169
     public void onCapturedPictureReceived(int objectHandle, String filename, Bitmap thumbnail, Bitmap bitmap) {
159 170
         adapter.addInfo("收到新照片 ");
160
-        photoIv.setImageBitmap(bitmap);
171
+        Photo photo = new Photo();
172
+        photo.handle = objectHandle;
173
+        photo.fileName = filename;
174
+        photo.thumb = thumbnail;
175
+        photo.origin = bitmap;
176
+        photo.captureTime = System.currentTimeMillis() + "";
177
+        photoGridAdapter.addPhoto(photo);
161 178
     }
162 179
 
163 180
     @Override
@@ -209,12 +226,12 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
209 226
     @Override
210 227
     public void onStorageFound(final int handle, String label) {
211 228
         adapter.addInfo("找到相机存储卡位置,句柄=" + handle + " label = " + label);
212
-        photoIv.post(new Runnable() {
213
-            @Override
214
-            public void run() {
215
-                camera.retrieveImageHandles(MyTestActivity.this, handle, PtpConstants.ObjectFormat.EXIF_JPEG);
216
-            }
217
-        });
229
+//        photoIv.post(new Runnable() {
230
+////            @Override
231
+////            public void run() {
232
+////                camera.retrieveImageHandles(MyTestActivity.this, handle, PtpConstants.ObjectFormat.EXIF_JPEG);
233
+////            }
234
+////        });
218 235
     }
219 236
 
220 237
     @Override
@@ -228,18 +245,34 @@ public class MyTestActivity extends Activity implements Camera.CameraListener, C
228 245
         photoIv.post(new Runnable() {
229 246
             @Override
230 247
             public void run() {
231
-                camera.retrieveImageInfo(MyTestActivity.this, handles[0]);
248
+                photoGridAdapter.addPhotos(handles);
232 249
             }
233 250
         });
234 251
     }
235 252
 
236 253
     @Override
237
-    public void onImageInfoRetrieved(int objectHandle, ObjectInfo objectInfo, final Bitmap thumbnail) {
238
-        adapter.addInfo("找到相机存储图像文件,句柄=" + objectHandle + " info = " + objectInfo);
254
+    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() {
257
+            @Override
258
+            public void run() {
259
+                Photo photo = new Photo();
260
+                photo.handle = objectHandle;
261
+                photo.fileName = objectInfo.filename;
262
+                photo.thumb = thumbnail;
263
+                photo.captureTime = objectInfo.captureDate;
264
+                photoGridAdapter.updatePhoto(photo);
265
+            }
266
+        });
267
+    }
268
+
269
+    public void retrieveImageInfo(final int handle) {
239 270
         photoIv.post(new Runnable() {
240 271
             @Override
241 272
             public void run() {
242
-                photoIv.setImageBitmap(thumbnail);
273
+                if (camera != null) {
274
+                    camera.retrieveImageInfo(MyTestActivity.this, handle);
275
+                }
243 276
             }
244 277
         });
245 278
 

+ 12 - 0
app/src/main/java/ai/pai/ptp/test/Photo.java

@@ -0,0 +1,12 @@
1
+package ai.pai.ptp.test;
2
+
3
+import android.graphics.Bitmap;
4
+
5
+public class Photo {
6
+    public int handle;
7
+    public String fileName;
8
+    public String captureTime;
9
+    public Bitmap thumb;
10
+    public Bitmap origin;
11
+    public boolean isSync;
12
+}

+ 3 - 3
app/src/main/java/ai/pai/ptp/test/MyTestAdapter.java

@@ -14,7 +14,7 @@ import java.util.Date;
14 14
 
15 15
 import ai.pai.ptp.R;
16 16
 
17
-public class MyTestAdapter extends BaseAdapter {
17
+public class TestLogListAdapter extends BaseAdapter {
18 18
 
19 19
     private ArrayList<String> infoList;
20 20
     private Context context;
@@ -22,7 +22,7 @@ public class MyTestAdapter extends BaseAdapter {
22 22
     private LayoutInflater inflater;
23 23
     private SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss\t");
24 24
 
25
-    public MyTestAdapter(Context context, ListView listView) {
25
+    public TestLogListAdapter(Context context, ListView listView) {
26 26
         this.context = context;
27 27
         this.listView = listView;
28 28
         infoList = new ArrayList<>();
@@ -59,7 +59,7 @@ public class MyTestAdapter extends BaseAdapter {
59 59
     public View getView(int position, View convertView, ViewGroup parent) {
60 60
         ViewHolder holder;
61 61
         if (convertView == null) {
62
-            convertView = inflater.inflate(R.layout.item_test_info, null);
62
+            convertView = inflater.inflate(R.layout.item_test_log_info, null);
63 63
             holder = new ViewHolder();
64 64
             holder.info = (TextView) convertView.findViewById(R.id.tv_info);
65 65
             convertView.setTag(holder);

+ 127 - 0
app/src/main/java/ai/pai/ptp/test/TestPhotoGridAdapter.java

@@ -0,0 +1,127 @@
1
+package ai.pai.ptp.test;
2
+
3
+import android.view.LayoutInflater;
4
+import android.view.View;
5
+import android.view.ViewGroup;
6
+import android.widget.BaseAdapter;
7
+import android.widget.GridView;
8
+import android.widget.ImageView;
9
+
10
+import java.util.ArrayList;
11
+
12
+import ai.pai.ptp.R;
13
+
14
+public class TestPhotoGridAdapter extends BaseAdapter {
15
+
16
+    private MyTestActivity cameraActivity;
17
+    private ArrayList<Photo> photos;
18
+    private GridView gridView;
19
+    private LayoutInflater inflater;
20
+    private int height;
21
+
22
+    public TestPhotoGridAdapter(MyTestActivity context, GridView gridView) {
23
+        cameraActivity = context;
24
+        this.gridView = gridView;
25
+        photos = new ArrayList<>();
26
+        inflater = LayoutInflater.from(context);
27
+        int screenW = context.getResources().getDisplayMetrics().widthPixels;
28
+        height = (int) (screenW - 4 * 3 * context.getResources().getDisplayMetrics().density) / 3;
29
+    }
30
+
31
+    @Override
32
+    public int getCount() {
33
+        return photos.size();
34
+    }
35
+
36
+    @Override
37
+    public Object getItem(int position) {
38
+        return photos.get(position);
39
+    }
40
+
41
+    @Override
42
+    public long getItemId(int position) {
43
+        return position;
44
+    }
45
+
46
+    public void addPhoto(final Photo photo) {
47
+        gridView.post(new Runnable() {
48
+            @Override
49
+            public void run() {
50
+                photos.add(0, photo);
51
+                notifyDataSetChanged();
52
+            }
53
+        });
54
+    }
55
+
56
+    public void updatePhoto(final Photo newPhoto) {
57
+        gridView.post(new Runnable() {
58
+            @Override
59
+            public void run() {
60
+                for(int k = 0; k < photos.size();k++){
61
+                    if(photos.get(k).handle == newPhoto.handle){
62
+                        photos.get(k).captureTime = newPhoto.captureTime;
63
+                        photos.get(k).thumb = newPhoto.thumb;
64
+                        photos.get(k).origin = newPhoto.origin;
65
+                        photos.get(k).fileName = newPhoto.fileName;
66
+                        break;
67
+                    }
68
+                }
69
+                notifyDataSetChanged();
70
+            }
71
+        });
72
+    }
73
+
74
+    public void addPhotos(final int[] handles){
75
+        gridView.post(new Runnable() {
76
+            @Override
77
+            public void run() {
78
+                ArrayList<Photo> tmp = new ArrayList<>();
79
+                for(int k =0;k<handles.length;k++){
80
+                    Photo photo = new Photo();
81
+                    photo.handle = handles[k];
82
+                    tmp.add(photo);
83
+                }
84
+                photos.addAll(tmp);
85
+                notifyDataSetChanged();
86
+            }
87
+        });
88
+    }
89
+
90
+    public void clear() {
91
+        gridView.post(new Runnable() {
92
+            @Override
93
+            public void run() {
94
+                photos.clear();
95
+                notifyDataSetChanged();
96
+            }
97
+        });
98
+    }
99
+
100
+    @Override
101
+    public View getView(int position, View convertView, ViewGroup parent) {
102
+        ViewHolder holder;
103
+        if (convertView == null) {
104
+            convertView = inflater.inflate(R.layout.item_grid_photo, null);
105
+            holder = new ViewHolder();
106
+            holder.photo = (ImageView) convertView.findViewById(R.id.iv_photo);
107
+            convertView.setTag(holder);
108
+        } else {
109
+            holder = (ViewHolder) convertView.getTag();
110
+        }
111
+        holder.photo.getLayoutParams().height = height;
112
+        Photo photo = photos.get(position);
113
+        if (photo.thumb != null) {
114
+            holder.photo.setImageBitmap(photo.thumb);
115
+        } else if (photo.origin != null) {
116
+            holder.photo.setImageBitmap(photo.origin);
117
+        } else if(photo.handle >0 && !photo.isSync){
118
+            photo.isSync = true;
119
+            cameraActivity.retrieveImageInfo(photo.handle);
120
+        }
121
+        return convertView;
122
+    }
123
+
124
+    final class ViewHolder {
125
+        public ImageView photo;
126
+    }
127
+}

+ 33 - 14
app/src/main/res/layout/activity_my_test.xml

@@ -1,7 +1,8 @@
1 1
 <?xml version="1.0" encoding="utf-8"?>
2 2
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
-    android:orientation="vertical" android:layout_width="match_parent"
4
-    android:layout_height="match_parent">
3
+    android:layout_width="match_parent"
4
+    android:layout_height="match_parent"
5
+    android:orientation="vertical">
5 6
 
6 7
     <Button
7 8
         android:id="@+id/btn_get_devece_info"
@@ -9,13 +10,14 @@
9 10
         android:layout_height="48dp"
10 11
         android:layout_margin="4dp"
11 12
         android:gravity="center"
12
-        android:text="获取设备信息"/>
13
+        android:text="获取设备信息" />
13 14
 
14 15
     <ListView
15 16
         android:id="@+id/list_info"
16 17
         android:layout_width="match_parent"
17
-        android:divider="@android:color/transparent"
18
-        android:layout_height="200dp" />
18
+        android:layout_height="200dp"
19
+        android:divider="@android:color/transparent" />
20
+
19 21
     <LinearLayout
20 22
         android:layout_width="match_parent"
21 23
         android:layout_height="48dp"
@@ -24,25 +26,42 @@
24 26
         <Button
25 27
             android:id="@+id/btn_capture_photo"
26 28
             android:layout_width="0dp"
27
-            android:layout_weight="1"
28 29
             android:layout_height="match_parent"
30
+            android:layout_weight="1"
29 31
             android:gravity="center"
30
-            android:text="拍摄"/>
32
+            android:text="拍摄模式" />
31 33
 
32 34
         <Button
33 35
             android:id="@+id/btn_get_photos"
34 36
             android:layout_width="0dp"
35
-            android:layout_weight="1"
36 37
             android:layout_height="match_parent"
38
+            android:layout_weight="1"
37 39
             android:gravity="center"
38
-            android:text="获取相机SD卡照片"/>
40
+            android:text="浏览模式" />
39 41
     </LinearLayout>
40 42
 
41
-
42
-    <ImageView
43
-        android:id="@+id/iv_latest_photo"
43
+    <RelativeLayout
44 44
         android:layout_width="match_parent"
45
-        android:layout_height="match_parent"
46
-        android:scaleType="centerCrop"/>
45
+        android:layout_height="match_parent">
46
+
47
+
48
+        <GridView
49
+            android:id="@+id/gridview_photos"
50
+            android:layout_width="match_parent"
51
+            android:layout_height="match_parent"
52
+            android:cacheColorHint="@android:color/transparent"
53
+            android:horizontalSpacing="4dp"
54
+            android:listSelector="@android:color/transparent"
55
+            android:numColumns="3"
56
+            android:verticalSpacing="4dp"
57
+            />
58
+
59
+        <ImageView
60
+            android:id="@+id/iv_latest_photo"
61
+            android:layout_width="match_parent"
62
+            android:layout_height="match_parent"
63
+            android:visibility="gone"
64
+            android:scaleType="fitCenter" />
65
+    </RelativeLayout>
47 66
 
48 67
 </LinearLayout>

+ 13 - 0
app/src/main/res/layout/item_grid_photo.xml

@@ -0,0 +1,13 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+    android:layout_width="match_parent"
4
+    android:layout_height="match_parent">
5
+
6
+    <ImageView
7
+        android:id="@+id/iv_photo"
8
+        android:layout_width="match_parent"
9
+        android:layout_height="match_parent"
10
+        android:background="#e2e2e2"
11
+        android:scaleType="centerCrop" />
12
+
13
+</RelativeLayout>

app/src/main/res/layout/item_test_info.xml → app/src/main/res/layout/item_test_log_info.xml