最近项目需要用到以图搜图,搜索了第三方产品,并且分析了项目需求,最终选择七牛云以图搜图,然而,七牛云文档就是个坑,只有php的demo,而java的却没有,联系客服几天才给java的demo
static final String accessKey = "xxxxxxxxx";
static final String secretKey = "xxxxxxxxxx";
static final String url_add_ku = "http://ai.qiniuapi.com/v1/image/group/test/new";
static final String url_del_ku = "http://ai.qiniuapi.com/v1/image/group/test/remove";
static final String url_show_all_ku = "http://ai.qiniuapi.com/v1/image/group"; //get
static final String url_add_image = "http://ai.qiniuapi.com/v1/image/group/test/add";
static final String url_del_image = "http://ai.qiniuapi.com/v1/image/group/test/delete";
static final String url_show_image = "http://ai.qiniuapi.com/v1/image/group/test?limit=100"; //显示指定图像库中所有图片
static final String url_search_image = "http://ai.qiniuapi.com/v1/image/groups/search"; //以图搜图
public static void main(String[] args) {
StringMap headers = new StringMap();
headers.put("Authorization", qiniuToken());
try {
Client client = new Client();
Response resp = client.post(url_search_image, json().getBytes(), headers, Client.JsonMime);
System.out.println(resp.toString());
System.out.println(resp.bodyString());
} catch (Exception e) {
e.printStackTrace();
}
}
static String qiniuToken() {
Auth auth = Auth.create(accessKey, secretKey);
String authorization = (String) auth.authorizationV2(url_search_image, "POST", json().getBytes(), Client.JsonMime).get("Authorization");
System.out.println(authorization);
return authorization;
}
static String json() {
//return "{\"data\": [{\"uri\": \"https://mars-assets.qnssl.com/resource/gogopher.jpg\"}]}"; //创建图库
//return "{\"data\": [{\"uri\": \"https://ss0.bdstatic.com/6Ox1bjeh1BF3odCf/it/u=2051025535,674887090&fm=74&app=80&f=JPEG&size=f121,140?sec=1880279984&t=c1e41bea92f6fae086d003cc9e0fcc4d\",\"attribute\": {\"id\": \"1\",\"label\": \"标签\",\"desc\": \"描述\"}}]}"; //添加图库
return "{\"data\": {\"uri\": \"https://ss0.bdstatic.com/6Ox1bjeh1BF3odCf/it/u=2051025535,674887090&fm=74&app=80&f=JPEG&size=f121,140?sec=1880279984&t=c1e41bea92f6fae086d003cc9e0fcc4d\"},\"params\": {\"groups\": [\"test\"],\"limit\": 5,\"threshold\": 0.85}}";
}