OSDN Git Service

am bfbd7bb4: Merge "Fix 5481444 Face clustering should use PWA profile shot instead...
authorWei Huang <weih@google.com>
Tue, 15 Nov 2011 21:11:00 +0000 (13:11 -0800)
committerAndroid Git Automerger <android-git-automerger@android.com>
Tue, 15 Nov 2011 21:11:00 +0000 (13:11 -0800)
* commit 'bfbd7bb47582be2a636eadffb041a698c52817f6':
  Fix 5481444 Face clustering should use PWA profile shot instead of random photo

gallerycommon/src/com/android/gallery3d/common/BitmapUtils.java
src/com/android/gallery3d/data/ClusterAlbum.java
src/com/android/gallery3d/data/ClusterAlbumSet.java
src/com/android/gallery3d/data/Clustering.java
src/com/android/gallery3d/data/Face.java
src/com/android/gallery3d/data/FaceClustering.java
src/com/android/gallery3d/data/LocalImage.java
src/com/android/gallery3d/data/MediaItem.java
src_pd/com/android/gallery3d/picasasource/PicasaSource.java

index c34e896..2192cf8 100644 (file)
@@ -31,8 +31,8 @@ import java.lang.reflect.Method;
 
 public class BitmapUtils {
     private static final String TAG = "BitmapUtils";
-    public static final int UNCONSTRAINED = -1;
     private static final int COMPRESS_JPEG_QUALITY = 90;
+    public static final int UNCONSTRAINED = -1;
 
     private BitmapUtils(){}
 
index 32f9023..569e5a4 100644 (file)
@@ -24,6 +24,7 @@ public class ClusterAlbum extends MediaSet implements ContentListener {
     private String mName = "";
     private DataManager mDataManager;
     private MediaSet mClusterAlbumSet;
+    private MediaItem mCover;
 
     public ClusterAlbum(Path path, DataManager dataManager,
             MediaSet clusterAlbumSet) {
@@ -33,6 +34,15 @@ public class ClusterAlbum extends MediaSet implements ContentListener {
         mClusterAlbumSet.addContentListener(this);
     }
 
+    public void setCoverMediaItem(MediaItem cover) {
+        mCover = cover;
+    }
+
+    @Override
+    public MediaItem getCoverMediaItem() {
+        return mCover != null ? mCover : super.getCoverMediaItem();
+    }
+
     void setMediaItems(ArrayList<Path> paths) {
         mPaths = paths;
     }
index 5b0569a..16501c2 100644 (file)
@@ -117,6 +117,7 @@ public class ClusterAlbumSet extends MediaSet implements ContentListener {
             }
             album.setMediaItems(clustering.getCluster(i));
             album.setName(childName);
+            album.setCoverMediaItem(clustering.getClusterCover(i));
             mAlbums.add(album);
         }
     }
index 542dda2..4072bf5 100644 (file)
@@ -23,4 +23,7 @@ public abstract class Clustering {
     public abstract int getNumberOfClusters();
     public abstract ArrayList<Path> getCluster(int index);
     public abstract String getClusterName(int index);
+    public MediaItem getClusterCover(int index) {
+        return null;
+    }
 }
index cc1a2d3..c5fd131 100644 (file)
 
 package com.android.gallery3d.data;
 
+import android.graphics.Rect;
+
 import com.android.gallery3d.common.Utils;
 
+import java.util.StringTokenizer;
+
 public class Face implements Comparable<Face> {
     private String mName;
     private String mPersonId;
+    private Rect mPosition;
 
-    public Face(String name, String personId) {
+    public Face(String name, String personId, String rect) {
         mName = name;
         mPersonId = personId;
-        Utils.assertTrue(mName != null && mPersonId != null);
+        Utils.assertTrue(mName != null && mPersonId != null && rect != null);
+        StringTokenizer tokenizer = new StringTokenizer(rect);
+        mPosition = new Rect();
+        while (tokenizer.hasMoreElements()) {
+            mPosition.left = Integer.parseInt(tokenizer.nextToken());
+            mPosition.top = Integer.parseInt(tokenizer.nextToken());
+            mPosition.right = Integer.parseInt(tokenizer.nextToken());
+            mPosition.bottom = Integer.parseInt(tokenizer.nextToken());
+        }
+    }
+
+    public Rect getPosition() {
+        return mPosition;
+    }
+
+    public int getWidth() {
+        return mPosition.right - mPosition.left;
+    }
+
+    public int getHeight() {
+        return mPosition.bottom - mPosition.top;
     }
 
     public String getName() {
@@ -45,12 +70,7 @@ public class Face implements Comparable<Face> {
         return false;
     }
 
-    @Override
-    public int hashCode() {
-        return mPersonId.hashCode();
-    }
-
     public int compareTo(Face another) {
-        return mPersonId.compareTo(another.mPersonId);
+        return mName.compareTo(another.mName);
     }
 }
index 6ed73b5..126286c 100644 (file)
 
 package com.android.gallery3d.data;
 
-import com.android.gallery3d.R;
-
 import android.content.Context;
+import android.graphics.Rect;
+
+import com.android.gallery3d.R;
+import com.android.gallery3d.picasasource.PicasaSource;
 
 import java.util.ArrayList;
-import java.util.Map;
 import java.util.TreeMap;
 
 public class FaceClustering extends Clustering {
     @SuppressWarnings("unused")
     private static final String TAG = "FaceClustering";
 
-    private ArrayList<ArrayList<Path>> mClusters;
-    private String[] mNames;
+    private FaceCluster[] mClusters;
     private String mUntaggedString;
+    private Context mContext;
+
+    private class FaceCluster {
+        ArrayList<Path> mPaths = new ArrayList<Path>();
+        String mName;
+        MediaItem mCoverItem;
+        Rect mCoverRegion;
+        int mCoverFaceIndex;
+
+        public FaceCluster(String name) {
+            mName = name;
+        }
+
+        public void add(MediaItem item, int faceIndex) {
+            Path path = item.getPath();
+            mPaths.add(path);
+            Face[] faces = item.getFaces();
+            if (faces != null) {
+                Face face = faces[faceIndex];
+                if (mCoverItem == null) {
+                    mCoverItem = item;
+                    mCoverRegion = face.getPosition();
+                    mCoverFaceIndex = faceIndex;
+                } else {
+                    Rect region = face.getPosition();
+                    if (mCoverRegion.width() < region.width() &&
+                            mCoverRegion.height() < region.height()) {
+                        mCoverItem = item;
+                        mCoverRegion = face.getPosition();
+                        mCoverFaceIndex = faceIndex;
+                    }
+                }
+            }
+        }
+
+        public int size() {
+            return mPaths.size();
+        }
+
+        public MediaItem getCover() {
+            if (mCoverItem != null) {
+                if (PicasaSource.isPicasaImage(mCoverItem)) {
+                    return PicasaSource.getFaceItem(mContext, mCoverItem, mCoverFaceIndex);
+                } else {
+                    return mCoverItem;
+                }
+            }
+            return null;
+        }
+    }
 
     public FaceClustering(Context context) {
         mUntaggedString = context.getResources().getString(R.string.untagged);
+        mContext = context;
     }
 
     @Override
     public void run(MediaSet baseSet) {
-        final TreeMap<Face, ArrayList<Path>> map =
-                new TreeMap<Face, ArrayList<Path>>();
-        final ArrayList<Path> untagged = new ArrayList<Path>();
+        final TreeMap<Face, FaceCluster> map =
+                new TreeMap<Face, FaceCluster>();
+        final FaceCluster untagged = new FaceCluster(mUntaggedString);
 
         baseSet.enumerateTotalMediaItems(new MediaSet.ItemConsumer() {
             public void consume(int index, MediaItem item) {
-                Path path = item.getPath();
-
                 Face[] faces = item.getFaces();
                 if (faces == null || faces.length == 0) {
-                    untagged.add(path);
+                    untagged.add(item, -1);
                     return;
                 }
                 for (int j = 0; j < faces.length; j++) {
-                    Face key = faces[j];
-                    ArrayList<Path> list = map.get(key);
-                    if (list == null) {
-                        list = new ArrayList<Path>();
-                        map.put(key, list);
+                    Face face = faces[j];
+                    FaceCluster cluster = map.get(face);
+                    if (cluster == null) {
+                        cluster = new FaceCluster(face.getName());
+                        map.put(face, cluster);
                     }
-                    list.add(path);
+                    cluster.add(item, j);
                 }
             }
         });
 
         int m = map.size();
-        mClusters = new ArrayList<ArrayList<Path>>();
-        mNames = new String[m + ((untagged.size() > 0) ? 1 : 0)];
-        int i = 0;
-        for (Map.Entry<Face, ArrayList<Path>> entry : map.entrySet()) {
-            mNames[i++] = entry.getKey().getName();
-            mClusters.add(entry.getValue());
-        }
+        mClusters = map.values().toArray(new FaceCluster[m + ((untagged.size() > 0) ? 1 : 0)]);
         if (untagged.size() > 0) {
-            mNames[i++] = mUntaggedString;
-            mClusters.add(untagged);
+            mClusters[m] = untagged;
         }
     }
 
     @Override
     public int getNumberOfClusters() {
-        return mClusters.size();
+        return mClusters.length;
     }
 
     @Override
     public ArrayList<Path> getCluster(int index) {
-        return mClusters.get(index);
+        return mClusters[index].mPaths;
     }
 
     @Override
     public String getClusterName(int index) {
-        return mNames[index];
+        return mClusters[index].mName;
+    }
+
+    @Override
+    public MediaItem getClusterCover(int index) {
+        return mClusters[index].getCover();
     }
 }
index e70b2ee..fa3ece3 100644 (file)
@@ -40,9 +40,6 @@ import java.io.IOException;
 
 // LocalImage represents an image in the local storage.
 public class LocalImage extends LocalMediaItem {
-    private static final int THUMBNAIL_TARGET_SIZE = 640;
-    private static final int MICROTHUMBNAIL_TARGET_SIZE = 200;
-
     private static final String TAG = "LocalImage";
 
     static final Path ITEM_PATH = Path.fromString("/local/image/item");
index a0c6d8c..b34a8c8 100644 (file)
@@ -28,6 +28,10 @@ public abstract class MediaItem extends MediaObject {
     public static final int TYPE_THUMBNAIL = 1;
     public static final int TYPE_MICROTHUMBNAIL = 2;
 
+    public static final int THUMBNAIL_TARGET_SIZE = 640;
+    public static final int MICROTHUMBNAIL_TARGET_SIZE = 200;
+    public static final int CACHED_IMAGE_QUALITY = 95;
+
     public static final int IMAGE_READY = 0;
     public static final int IMAGE_WAIT = 1;
     public static final int IMAGE_ERROR = -1;
index 4a4f786..a4f9621 100644 (file)
@@ -17,6 +17,7 @@
 package com.android.gallery3d.picasasource;
 
 import com.android.gallery3d.app.GalleryApp;
+import com.android.gallery3d.data.MediaItem;
 import com.android.gallery3d.data.MediaObject;
 import com.android.gallery3d.data.MediaSet;
 import com.android.gallery3d.data.MediaSource;
@@ -80,6 +81,10 @@ public class PicasaSource extends MediaSource {
         }
     }
 
+    public static MediaItem getFaceItem(Context context, MediaItem item, int faceIndex) {
+        throw new UnsupportedOperationException();
+    }
+
     public static boolean isPicasaImage(MediaObject object) {
         return false;
     }