PhotoSer.java
4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.example.demo;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.imagesearch.model.v20180120.AddItemRequest;
import com.aliyuncs.imagesearch.model.v20180120.AddItemResponse;
import com.aliyuncs.imagesearch.model.v20180120.SearchItemRequest;
import com.aliyuncs.imagesearch.model.v20180120.SearchItemResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* Created by jack on 2018/3/14.
*/
@Service
@ConfigurationProperties("sdk")
@Data
public class PhotoSer {
private static final Logger LOGGER = LoggerFactory.getLogger(PhotoSer.class);
IClientProfile profile;
IAcsClient client;
String ak;
String aks;
String instanceId;
String instanceName;
public PhotoSer() {
}
@PostConstruct
public void init() {
try {
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "ImageSearch", "imagesearch.cn-shanghai.aliyuncs.com");
} catch (ClientException e) {
e.printStackTrace();
}
profile = DefaultProfile.getProfile("cn-shanghai", ak, aks);
client = new DefaultAcsClient(profile);
}
public void loadPhoto(int id, List<String> filePaths) throws ClientException {
AddItemRequest request = new AddItemRequest();
request.setInstanceName(instanceName);
request.setCatId("0");
request.setItemId(String.valueOf(id));
request.setCustContent("{\"key\":\"value\"}");
for (String filePath : filePaths) {
String fileName = FilenameUtils.getName(filePath);
byte[] fileContent = getBytes(filePath);
// 以字节流的方式读取图片内容, key 为图片名称,value 为图片内容
if (null == fileContent) continue;
request.addPicture(fileName, fileContent);
}
if (!request.buildPostContent()) {
LOGGER.error("build post content failed.");
return;
}
AddItemResponse response = null;
response = client.getAcsResponse(request);
LOGGER.info("success to load {}, resp {} ", id, response.getSuccess());
}
private byte[] getBytes(String photoPath) {
File file = new File(photoPath);
try {
byte[] content = FileUtils.readFileToByteArray(file);
return content;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public SearchItemResponse searchPhoto(String filePath) {
SearchItemRequest request = new SearchItemRequest();
request.setInstanceName(instanceName);
request.setNum(5);
request.setStart(0);
byte[] bytes = getBytes(filePath);
request.setSearchPicture(bytes);
if (!request.buildPostContent()) {
System.out.println("build post content failed.");
return null;
}
SearchItemResponse response = null;
try {
response = client.getAcsResponse(request);
return response;
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return null;
}
public SearchItemResponse searchPhoto(String filePath, int num) {
SearchItemRequest request = new SearchItemRequest();
request.setInstanceName(instanceName);
request.setNum(num);
request.setStart(0);
ImageUtils.zoomInImage(filePath, "/tmp/text.jpg");
byte[] bytes = getBytes("/tmp/text.jpg");
request.setSearchPicture(bytes);
if (!request.buildPostContent()) {
System.out.println("build post content failed.");
return null;
}
SearchItemResponse response = null;
try {
response = client.getAcsResponse(request);
return response;
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return null;
}
}