2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.data;
19 import android.content.Context;
20 import android.graphics.Rect;
22 import com.android.gallery3d.R;
23 import com.android.gallery3d.picasasource.PicasaSource;
25 import java.util.ArrayList;
26 import java.util.TreeMap;
28 public class FaceClustering extends Clustering {
29 @SuppressWarnings("unused")
30 private static final String TAG = "FaceClustering";
32 private FaceCluster[] mClusters;
33 private String mUntaggedString;
34 private Context mContext;
36 private class FaceCluster {
37 ArrayList<Path> mPaths = new ArrayList<Path>();
43 public FaceCluster(String name) {
47 public void add(MediaItem item, int faceIndex) {
48 Path path = item.getPath();
50 Face[] faces = item.getFaces();
52 Face face = faces[faceIndex];
53 if (mCoverItem == null) {
55 mCoverRegion = face.getPosition();
56 mCoverFaceIndex = faceIndex;
58 Rect region = face.getPosition();
59 if (mCoverRegion.width() < region.width() &&
60 mCoverRegion.height() < region.height()) {
62 mCoverRegion = face.getPosition();
63 mCoverFaceIndex = faceIndex;
73 public MediaItem getCover() {
74 if (mCoverItem != null) {
75 if (PicasaSource.isPicasaImage(mCoverItem)) {
76 return PicasaSource.getFaceItem(mContext, mCoverItem, mCoverFaceIndex);
85 public FaceClustering(Context context) {
86 mUntaggedString = context.getResources().getString(R.string.untagged);
91 public void run(MediaSet baseSet) {
92 final TreeMap<Face, FaceCluster> map =
93 new TreeMap<Face, FaceCluster>();
94 final FaceCluster untagged = new FaceCluster(mUntaggedString);
96 baseSet.enumerateTotalMediaItems(new MediaSet.ItemConsumer() {
98 public void consume(int index, MediaItem item) {
99 Face[] faces = item.getFaces();
100 if (faces == null || faces.length == 0) {
101 untagged.add(item, -1);
104 for (int j = 0; j < faces.length; j++) {
105 Face face = faces[j];
106 FaceCluster cluster = map.get(face);
107 if (cluster == null) {
108 cluster = new FaceCluster(face.getName());
109 map.put(face, cluster);
111 cluster.add(item, j);
117 mClusters = map.values().toArray(new FaceCluster[m + ((untagged.size() > 0) ? 1 : 0)]);
118 if (untagged.size() > 0) {
119 mClusters[m] = untagged;
124 public int getNumberOfClusters() {
125 return mClusters.length;
129 public ArrayList<Path> getCluster(int index) {
130 return mClusters[index].mPaths;
134 public String getClusterName(int index) {
135 return mClusters[index].mName;
139 public MediaItem getClusterCover(int index) {
140 return mClusters[index].getCover();