Skip to content

Commit dea6b97

Browse files
author
fantouchxie
committed
FaceIdentifyRequest FaceNewPersonRequest 支持 byte[] 类型参数
1 parent 364b0aa commit dea6b97

File tree

7 files changed

+143
-16
lines changed

7 files changed

+143
-16
lines changed

src/main/java/com/qcloud/image/demo/Demo.java

+48-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
import org.json.JSONObject;
4242

4343
import java.io.File;
44+
import java.io.FileNotFoundException;
45+
import java.io.IOException;
46+
import java.io.RandomAccessFile;
4447
import java.util.logging.Level;
4548
import java.util.logging.Logger;
4649

@@ -287,12 +290,23 @@ private static void faceFaceIdentify(ImageClient imageClient, String bucketName)
287290

288291
//2. 图片内容方式
289292
System.out.println("====================================================");
290-
String faceImageFileParamName = "image";
291-
File faceImageFile = new File("/home/test.jpg");
292-
faceIdentifyReq = new FaceIdentifyRequest(bucketName, groupId, faceImageFileParamName, faceImageFile);// 一个 groupId
293-
//faceIdentifyReq = new FaceIdentifyRequest(bucketName, groupIds, faceImageFileParamName, faceImageFile);// 多个 groupId
294-
ret = imageClient.faceIdentify(faceIdentifyReq);
293+
File faceImageFile = new File("assets", "icon_face_01.jpg");
294+
FaceIdentifyRequest faceIdentifyReq2 = new FaceIdentifyRequest(bucketName, groupId, faceImageFile);// 一个 groupId
295+
//FaceIdentifyRequest faceIdentifyReq2 = new FaceIdentifyRequest(bucketName, groupIds, faceImageFile);// 多个 groupId
296+
ret = imageClient.faceIdentify(faceIdentifyReq2);
295297
System.out.println("face identify ret:" + ret);
298+
299+
//3. 图片内容方式(byte[])
300+
System.out.println("====================================================");
301+
byte[] imgBytes = getFileBytes(faceImageFile);
302+
if (imgBytes != null) {
303+
FaceIdentifyRequest faceIdentifyReq3 = new FaceIdentifyRequest(bucketName, groupId, imgBytes);// 一个 groupId
304+
//FaceIdentifyRequest faceIdentifyReq3 = new FaceIdentifyRequest(bucketName, groupIds, imgBytes);// 多个 groupId
305+
ret = imageClient.faceIdentify(faceIdentifyReq3);
306+
System.out.println("face identify ret:" + ret);
307+
} else {
308+
System.out.println("face identify ret: get image content fail");
309+
}
296310
}
297311

298312
/**
@@ -515,9 +529,21 @@ private static String faceNewPerson(ImageClient imageClient, String bucketName)
515529
//2. 图片内容方式
516530
System.out.println("====================================================");
517531
File personNewImage = new File("assets","icon_face_01.jpg");
518-
personNewReq = new FaceNewPersonRequest(bucketName, personId, groupIds, personName, personNewImage, personName, personTag);
532+
personNewReq = new FaceNewPersonRequest(bucketName, personId, groupIds, personNewImage, personName, personTag);
519533
ret = imageClient.faceNewPerson(personNewReq);
520534
System.out.println("person new ret:" + ret);
535+
536+
//3. 图片内容方式(byte[])
537+
System.out.println("====================================================");
538+
byte[] imageContent = getFileBytes(personNewImage);
539+
if (imageContent != null) {
540+
personNewReq = new FaceNewPersonRequest(bucketName, personId, groupIds, imageContent, personName, personTag);
541+
ret = imageClient.faceNewPerson(personNewReq);
542+
System.out.println("person new ret:" + ret);
543+
} else {
544+
System.out.println("person new ret: get image content fail");
545+
}
546+
521547
return personId;
522548
}
523549

@@ -821,4 +847,20 @@ private static void imagePorn(ImageClient imageClient, String bucketName) {
821847
System.out.println("porn detect ret:" + ret);
822848
}
823849

850+
private static byte[] getFileBytes(File file) {
851+
byte[] imgBytes = null;
852+
try {
853+
RandomAccessFile f = new RandomAccessFile(file, "r");
854+
imgBytes = new byte[(int) f.length()];
855+
f.readFully(imgBytes);
856+
} catch (FileNotFoundException e) {
857+
e.printStackTrace();
858+
return null;
859+
} catch (IOException e) {
860+
e.printStackTrace();
861+
return null;
862+
}
863+
return imgBytes;
864+
}
865+
824866
}

src/main/java/com/qcloud/image/http/DefaultImageHttpClient.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected String sendPostRequest(HttpRequest httpRequest) throws AbstractImageEx
194194
Map<String, Object> params = httpRequest.getParams();
195195
MultipartBuilder multipartBuilder = new MultipartBuilder();
196196
try {
197-
setMultiPartEntity(multipartBuilder, params, imageList, image, keyList, imageKey);
197+
setMultiPartEntity(multipartBuilder, params, imageList, httpRequest.getBytesContentList(), image, keyList, imageKey);
198198
} catch (FileNotFoundException e) {
199199
throw new ParamException(e.getMessage());
200200
}
@@ -333,6 +333,16 @@ private void setMultiPartEntity(MultipartBuilder multipartBuilder,/*HttpPost htt
333333
}
334334
}
335335

336+
for (String key : contentList.keySet()) {
337+
byte[] content = contentList.get(key);
338+
if (content != null && content.length > 0) {
339+
multipartBuilder.addPart(
340+
Headers.of("Content-Disposition", String.format("form-data; name=\"%s\"", key)),
341+
RequestBody.create(MediaType.parse("image/jpg"), content)
342+
);
343+
}
344+
}
345+
336346
//HttpEntity entity = entityBuilder.build();
337347
//httpPost.setEntity(entity);
338348
}

src/main/java/com/qcloud/image/http/HttpRequest.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class HttpRequest {
2121
private ArrayList<String> urlList = new ArrayList<String>();
2222
private HashMap<String, File> imageList = new HashMap<String, File>();
2323

24+
private Map<String, byte[]> bytesContentList = new HashMap<String, byte[]>();
25+
2426
public String getUrl() {
2527
return url;
2628
}
@@ -105,8 +107,16 @@ public void setImage(File image) {
105107
isUrl = false;
106108
this.image = image;
107109
}
110+
111+
public void addBytes(String key,byte[] content){
112+
bytesContentList.put(key, content);
113+
}
114+
115+
public Map<String, byte[]> getBytesContentList() {
116+
return bytesContentList;
117+
}
108118

109-
@Override
119+
@Override
110120
public String toString() {
111121
StringBuilder sb = new StringBuilder();
112122
sb.append("url:").append(url).append(", method:").append(method).append(", ConentType:")

src/main/java/com/qcloud/image/op/DetectionOp.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.qcloud.image.http.HttpRequest;
1717
import com.qcloud.image.http.RequestBodyKey;
1818
import com.qcloud.image.http.RequestHeaderKey;
19+
import com.qcloud.image.request.AbstractBaseRequest.BytesContent;
1920
import com.qcloud.image.request.FaceAddFaceRequest;
2021
import com.qcloud.image.request.FaceAddGroupIdsRequest;
2122
import com.qcloud.image.request.FaceCompareRequest;
@@ -480,6 +481,7 @@ public String faceNewPerson(FaceNewPersonRequest request) throws AbstractImageEx
480481
httpRequest.addParam(RequestBodyKey.GROUP_IDS, request.getGroupIds());
481482
httpRequest.setContentType(HttpContentType.APPLICATION_JSON);
482483
} else {
484+
httpRequest.setContentType(HttpContentType.MULTIPART_FORM_DATA);
483485
int index;
484486
String[] groupIds = request.getGroupIds();
485487
for (index = 0;index < groupIds.length; index++) {
@@ -488,7 +490,10 @@ public String faceNewPerson(FaceNewPersonRequest request) throws AbstractImageEx
488490
httpRequest.addParam(key, data);
489491
}
490492
httpRequest.setImage(request.getImage());
491-
httpRequest.setContentType(HttpContentType.MULTIPART_FORM_DATA);
493+
BytesContent bytesContent = request.getBytesContent();
494+
if (bytesContent != null) {
495+
httpRequest.addBytes(bytesContent.getKey(), bytesContent.getContent());
496+
}
492497
}
493498

494499
return httpClient.sendHttpRequest(httpRequest);
@@ -859,8 +864,12 @@ public String faceIdentify(FaceIdentifyRequest request) throws AbstractImageExce
859864
throw new ParamException("groupId and groupIds both null or empty!!");
860865
}
861866
} else {
862-
httpRequest.setImage(request.getImage());
863867
httpRequest.setContentType(HttpContentType.MULTIPART_FORM_DATA);
868+
httpRequest.setImage(request.getImage());
869+
BytesContent bytesContent = request.getBytesContent();
870+
if (bytesContent != null) {
871+
httpRequest.addBytes(bytesContent.getKey(), bytesContent.getContent());
872+
}
864873
if (groupId != null && !groupId.isEmpty()) {
865874
httpRequest.addParam(RequestBodyKey.GROUP_ID, groupId);
866875
} else if (groupIds != null && groupIds.length > 0) {
@@ -1170,6 +1179,10 @@ public String faceLiveDetectPicture(FaceLiveDetectPictureRequest request, boolea
11701179
} else {
11711180
httpRequest.setContentType(HttpContentType.MULTIPART_FORM_DATA);
11721181
httpRequest.setImage(request.getImage());
1182+
BytesContent bytesContent = request.getBytesContent();
1183+
if (bytesContent != null) {
1184+
httpRequest.addBytes(bytesContent.getKey(), bytesContent.getContent());
1185+
}
11731186
}
11741187

11751188
return httpClient.sendHttpRequest(httpRequest);

src/main/java/com/qcloud/image/request/AbstractBaseRequest.java

+29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public abstract class AbstractBaseRequest {
1010
// bucket名
1111
private String bucketName;
1212

13+
private BytesContent mBytesContent;
14+
1315
public AbstractBaseRequest(String bucketName) {
1416
super();
1517
this.bucketName = bucketName;
@@ -45,4 +47,31 @@ public String toString() {
4547
public void check_param() throws ParamException {
4648
CommonParamCheckUtils.AssertNotNull("bucketName", this.bucketName);
4749
}
50+
51+
void setBytesContent(String key, byte[] content) {
52+
mBytesContent = new BytesContent(key, content);
53+
}
54+
55+
public BytesContent getBytesContent() {
56+
return mBytesContent;
57+
}
58+
59+
public static class BytesContent {
60+
61+
String key;
62+
byte[] content;
63+
64+
BytesContent(String key, byte[] content) {
65+
this.key = key;
66+
this.content = content;
67+
}
68+
69+
public String getKey() {
70+
return key;
71+
}
72+
73+
public byte[] getContent() {
74+
return content;
75+
}
76+
}
4877
}

src/main/java/com/qcloud/image/request/FaceIdentifyRequest.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ public FaceIdentifyRequest(String bucketName, String groupId, String url) {
3535
this.groupId = groupId;
3636
this.url = url;
3737
}
38-
39-
public FaceIdentifyRequest(String bucketName, String groupId, String name, File image) {
38+
39+
public FaceIdentifyRequest(String bucketName, String groupId, byte[] image) {
40+
super(bucketName);
41+
this.isUrl = false;
42+
this.groupId = groupId;
43+
setBytesContent("image",image);
44+
}
45+
46+
public FaceIdentifyRequest(String bucketName, String groupId, File image) {
4047
super(bucketName);
4148
this.isUrl = false;
4249
this.groupId = groupId;
@@ -50,12 +57,18 @@ public FaceIdentifyRequest(String bucketName, String[] groupIds, String url) {
5057
this.groupIds = groupIds;
5158
}
5259

53-
public FaceIdentifyRequest(String bucketName, String[] groupIds, String name, File image) {
60+
public FaceIdentifyRequest(String bucketName, String[] groupIds, File image) {
5461
super(bucketName);
5562
this.isUrl = false;
5663
this.groupIds = groupIds;
5764
this.image = image;
5865
}
66+
public FaceIdentifyRequest(String bucketName, String[] groupIds, byte[] image) {
67+
super(bucketName);
68+
this.isUrl = false;
69+
this.groupIds = groupIds;
70+
setBytesContent("image", image);
71+
}
5972

6073
public boolean isUrl() {
6174
return isUrl;
@@ -91,7 +104,7 @@ public void check_param() throws ParamException {
91104
if(isUrl){
92105
CommonParamCheckUtils.AssertNotNull("url", url);
93106
}else{
94-
CommonParamCheckUtils.AssertNotNull("image content", image);
107+
//CommonParamCheckUtils.AssertNotNull("image content", image);
95108
}
96109
}
97110
}

src/main/java/com/qcloud/image/request/FaceNewPersonRequest.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public FaceNewPersonRequest(String bucketName, String personId, String[] groupId
4646
this.personTag = personTag;
4747
}
4848

49-
public FaceNewPersonRequest(String bucketName, String personId, String[] groupIds, String fileName, File image, String personName, String personTag) {
49+
public FaceNewPersonRequest(String bucketName, String personId, String[] groupIds, File image, String personName, String personTag) {
5050
super(bucketName);
5151
this.isUrl = false;
5252
this.image = image;
@@ -55,6 +55,16 @@ public FaceNewPersonRequest(String bucketName, String personId, String[] groupId
5555
this.personName = personName;
5656
this.personTag = personTag;
5757
}
58+
59+
public FaceNewPersonRequest(String bucketName, String personId, String[] groupIds, byte[] image, String personName, String personTag) {
60+
super(bucketName);
61+
this.isUrl = false;
62+
setBytesContent("image", image);
63+
this.groupIds = groupIds;
64+
this.personId = personId;
65+
this.personName = personName;
66+
this.personTag = personTag;
67+
}
5868

5969
public boolean isUrl() {
6070
return isUrl;
@@ -118,7 +128,7 @@ public void check_param() throws ParamException {
118128
if(isUrl){
119129
CommonParamCheckUtils.AssertNotNull("url", url);
120130
}else{
121-
CommonParamCheckUtils.AssertNotNull("image content", image);
131+
//CommonParamCheckUtils.AssertNotNull("image content", image);
122132
}
123133
}
124134
}

0 commit comments

Comments
 (0)