Skip to content

Commit b733652

Browse files
committed
support multiple face coordinates in upload and explicit. optionaly use Coordinates as a wrapper of multiple rectangles
1 parent c2284f0 commit b733652

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.cloudinary;
2+
3+
import java.util.Collection;
4+
import java.util.ArrayList;
5+
import org.apache.commons.lang.StringUtils;
6+
import java.awt.Rectangle;
7+
public class Coordinates {
8+
9+
Collection<Rectangle> coordinates = new ArrayList<Rectangle>();
10+
11+
public Coordinates() {
12+
}
13+
14+
public Coordinates(Collection<Rectangle> coordinates) {
15+
this.coordinates = coordinates;
16+
}
17+
18+
public void addRect(Rectangle rect) {
19+
this.coordinates.add(rect);
20+
}
21+
22+
23+
public Collection<Rectangle> underlaying() {
24+
return this.coordinates;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
ArrayList<String> rects = new ArrayList<String>();
30+
for (Rectangle rect : this.coordinates) {
31+
rects.add(rect.x + "," + rect.y + "," + rect.width + "," + rect.height);
32+
}
33+
return StringUtils.join(rects, "|");
34+
}
35+
36+
}

cloudinary-core/src/main/java/com/cloudinary/Uploader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public Map<String, Object> buildUploadParams(Map options) {
6363
params.put("proxy", (String) options.get("proxy"));
6464
params.put("folder", (String) options.get("folder"));
6565
params.put("tags", StringUtils.join(Cloudinary.asArray(options.get("tags")), ","));
66-
params.put("face_coordinates", StringUtils.join(Cloudinary.asArray(options.get("face_coordinates")), ","));
66+
if (options.get("face_coordinates") != null) {
67+
params.put("face_coordinates", options.get("face_coordinates").toString());
68+
}
6769
params.put("allowed_formats", StringUtils.join(Cloudinary.asArray(options.get("allowed_formats")), ","));
6870
Map<String,String> inputContext = null;
6971
if (options.get("context") != null && options.get("context") instanceof Map) {

cloudinary-core/src/test/java/com/cloudinary/test/UploaderTest.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
import java.util.List;
1212
import java.util.Map;
1313

14+
import java.awt.Rectangle;
15+
1416
import org.junit.Before;
1517
import org.junit.BeforeClass;
1618
import org.junit.Test;
1719

1820
import com.cloudinary.Cloudinary;
21+
import com.cloudinary.Coordinates;
1922
import com.cloudinary.Transformation;
2023

2124
@SuppressWarnings({"rawtypes", "unchecked"})
@@ -216,27 +219,44 @@ public void testAllowedFormatsWithFormat() throws Exception {
216219
@Test
217220
public void testFaceCoordinates() throws Exception {
218221
//should allow sending face coordinates
219-
Long[] coordinates = new Long[]{120L,30L,109L,150L};
222+
Coordinates coordinates = new Coordinates();
223+
Rectangle rect1 = new Rectangle(121,31,110,151);
224+
Rectangle rect2 = new Rectangle(120,30,109,150);
225+
coordinates.addRect(rect1);
226+
coordinates.addRect(rect2);
220227
Map result = cloudinary.uploader().upload("src/test/resources/logo.png", Cloudinary.asMap("face_coordinates", coordinates, "faces", true));
221228
org.json.simple.JSONArray resultFaces = (org.json.simple.JSONArray) result.get("faces");
222-
assertEquals(1, resultFaces.size());
229+
assertEquals(2, resultFaces.size());
230+
223231
Object[] resultCoordinates = ((org.json.simple.JSONArray) resultFaces.get(0)).toArray();
224-
assertEquals(4, resultCoordinates.length);
225-
for (int i=0; i < resultCoordinates.length; i++) {
226-
assertEquals(coordinates[i], resultCoordinates[i]);
227-
}
228232

229-
Long[] differentCoordinates = new Long[]{121L,31L,110L,151L};
233+
assertEquals((long)rect1.x, resultCoordinates[0]);
234+
assertEquals((long)rect1.y, resultCoordinates[1]);
235+
assertEquals((long)rect1.width, resultCoordinates[2]);
236+
assertEquals((long)rect1.height, resultCoordinates[3]);
237+
238+
resultCoordinates = ((org.json.simple.JSONArray) resultFaces.get(1)).toArray();
239+
240+
assertEquals((long)rect2.x, resultCoordinates[0]);
241+
assertEquals((long)rect2.y, resultCoordinates[1]);
242+
assertEquals((long)rect2.width, resultCoordinates[2]);
243+
assertEquals((long)rect2.height, resultCoordinates[3]);
244+
245+
Coordinates differentCoordinates = new Coordinates();
246+
Rectangle rect3 = new Rectangle(122,32,111,152);
247+
differentCoordinates.addRect(rect3);
230248
cloudinary.uploader().explicit((String) result.get("public_id"), Cloudinary.asMap("face_coordinates", differentCoordinates, "faces", true, "type", "upload"));
231249
Map info = cloudinary.api().resource((String) result.get("public_id"), Cloudinary.asMap("faces", true));
232250

233251
resultFaces = (org.json.simple.JSONArray) info.get("faces");
234252
assertEquals(1, resultFaces.size());
235253
resultCoordinates = ((org.json.simple.JSONArray) resultFaces.get(0)).toArray();
236-
assertEquals(4, resultCoordinates.length);
237-
for (int i=0; i < resultCoordinates.length; i++) {
238-
assertEquals(differentCoordinates[i], resultCoordinates[i]);
239-
}
254+
255+
assertEquals((long)rect3.x, resultCoordinates[0]);
256+
assertEquals((long)rect3.y, resultCoordinates[1]);
257+
assertEquals((long)rect3.width, resultCoordinates[2]);
258+
assertEquals((long)rect3.height, resultCoordinates[3]);
259+
240260
}
241261

242262
@Test

0 commit comments

Comments
 (0)