MyTask.java
10.7 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.example.test;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
@Component
public class MyTask implements ApplicationRunner {
private String url = "http://114.55.30.100:9020/";
private String ip;
{
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
@Override
public void run(ApplicationArguments args) throws Exception {
File file = new File("E:\\wwwhtdocs\\SmartCampus\\face17e50\\School562\\Student");
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File img = files[i];
File outFile = new File("E:\\wwwhtdocs\\SmartCampus\\face17e50\\School562\\Student",img.getName().split("\\.")[0]+".png");
FileInputStream fileInputStream = new FileInputStream(img);
FileOutputStream fileOutputStream =new FileOutputStream(outFile);
byte[] bytes = new byte[1024];
int length = 0;
while ((length=fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,length);
}
fileInputStream.close();
fileOutputStream.close();
img.delete();
}
// sendHaiKangLocal();
// removeImgs("C:\\TaoHandong\\copy\\school110\\Student");
// sendImgs();
/**
* 下发人脸到海康
*/
/*File file = new File("C:\\Users\\taohandong\\Desktop\\下发学校资料\\高级中学卡号和姓名.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String content = null;
String deviceId = "18262213";
String SchoolId = "479";
RestTemplate restTemplate = new RestTemplate();
List<String> studentS = new ArrayList<>();
while ((content=bufferedReader.readLine())!=null){
if (content.split(",").length>2){
String card = content.split(",")[0];
String name = content.split(",")[1];
String studentcode = content.split(",")[2];
String contUrl = url+"facereco/SendCard?card="+card+"&name="+name+"&validTimeEnabled="+1+"&startTime=2019-09-28 00:00:00&endTime=2023-09-28 00:00:00"+
"&deviceId="+deviceId;
//下发卡号
String cardResult = restTemplate.getForObject(contUrl,String.class);
System.out.println("卡号下发:"+cardResult);
String faceUrl = url+"facereco/SendStudentSingleImage?schoolId="+SchoolId+"&deviceId=" + deviceId + "&studentCode=" + studentcode;
String result = restTemplate.getForObject(faceUrl, String.class);
if (result.equals("0")){
studentS.add(studentcode);
}
System.out.println("人脸下发:"+result+" "+studentcode);
}
}*/
/**
* 替换学籍号
*/
/*File file = new File("E:\\wwwhtdocs\\SmartCampus\\face17e5\\School110\\Student");
File fileStudentcodes = new File("E:\\wwwhtdocs\\SmartCampus\\face17e5\\School110\\studentcode.txt");
List<String> studentList = new ArrayList<>();
String content = null;
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileStudentcodes));
while ((content=bufferedReader.readLine())!=null){
studentList.add(content);
}
List<String> studentListCode = new ArrayList<>();
File[] files = file.listFiles();
System.out.println("files:"+files.length);
for (int i = 0; i < files.length; i++) {
File img = files[i];
String studentCode = img.getName().split("\\.")[0];
if (!studentList.contains(studentCode))studentListCode.add(studentCode);
}
System.out.println("studentList:"+studentList.toString()+"\r\n");
System.out.println("studentListCode:"+studentListCode.toString());*/
}
private String haikangUrl = "http://192.168.1.100:8000/";
private void sendHaiKangLocal() throws IOException {
File file = new File("C:\\TaoHandong\\copy\\school110\\盖北.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String content = null;
String deviceId = "218963574";
String SchoolId = "110";
RestTemplate restTemplate = new RestTemplate();
List<String> strings = new ArrayList<>();//记录下发失败图片原因
List<String> strings1 = new ArrayList<>();
while ((content=bufferedReader.readLine())!=null){
if (content.split(",").length>2){
String card = content.split(",")[0];
String name = content.split(",")[1];
String studentcode = content.split(",")[2];
String contUrl = haikangUrl+"facereco/SendCard?card="+card+"&name="+name+"&validTimeEnabled="+1+"&startTime=2019-09-28 00:00:00&endTime=2023-09-28 00:00:00"+
"&deviceId="+deviceId;
//下发卡号
String cardResult = restTemplate.getForObject(contUrl,String.class);
System.out.println("卡号下发:"+cardResult+" card:"+card);
String faceUrl = haikangUrl+"facereco/SendStudentSingleImage?schoolId="+SchoolId+"&deviceId=" + deviceId + "&studentCode=" + studentcode;
String result = restTemplate.getForObject(faceUrl, String.class);
if (result.equals("0")){
String url = "C:\\TaoHandong\\copy\\school110\\Student\\"+studentcode+".png";
File fileExit = new File(url);
if (!fileExit.exists()){
strings.add(studentcode+",图片不存在");
}else {
strings1.add(studentcode+",图片不合格");
}
}
System.out.println("本地人脸下发:"+result+" "+studentcode);
}else {
System.out.println("信息不全");
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("图片不合格人数:"+strings1.toString()+" 图片不存在人数:"+strings.toString());
System.out.println("图片不合格人数:"+strings1.size()+" 图片不存在人数:"+strings.size());
}
private void sendImgs() throws IOException {
File file = new File("./config.txt");
if (!file.exists()){
System.out.println("配置文件不存在");
file.createNewFile();
return;
}
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String content = null;
String schoolId = "";
String filePath = "";//图片目录
while ((content = bufferedReader.readLine()) != null) {
if (content.contains("schoolId"))
schoolId = content.replace("schoolId:", "").trim();
if (content.contains("filePath:"))
filePath = content.replace("filePath:","").trim();
}
RestTemplate restTemplate = new RestTemplate();
File filePaths = new File(filePath);
if (filePaths.exists()){
File[] files = filePaths.listFiles();
for (int i = 0; i < files.length; i++) {
File imgFile = files[i];
String studentCode = imgFile.getName().split("\\.")[0];
if (!StringUtils.isEmpty(studentCode)){
String imgPath = imgFile.getAbsolutePath().replace("\\","/");
// imgPath = imgPath.replace("\\","%5C");
String apiBaseUrl = String.format("http://" + ip + ":8991/user/uploadImgAndUserInfo?file=%s&schoolId=" + schoolId + "&studentCode=" + studentCode+"&clint_type=22",imgPath);
System.out.println("apiBaseUrl:"+apiBaseUrl);
boolean result = restTemplate.getForObject(apiBaseUrl, boolean.class);
System.out.println("result:"+result);
}else {
System.out.println("学籍号为空");
}
}
System.out.println("结束调用");
}else {
System.out.println("图片目录不存在");
}
}
/**
* 移除相同的图片
*/
private void removeImgs(String path) {
// String path = "C:\\TaoHandong\\copy\\school479\\StudentCompressed1";
File file = new File(path);
File[] imgFiles = file.listFiles();
List<String> list = new ArrayList<>();
List<String> listExit = new ArrayList<>();
for (int i = 0; i < imgFiles.length; i++) {
File img = imgFiles[i];
String name = img.getName().split("\\.")[0];
if (list.contains(name)) {
listExit.add(name);
} else {
list.add(name);
}
}
System.out.println("listExit:" + list.size());
System.out.println("listExit:" + listExit.toString());
for (int i = 0; i < listExit.size(); i++) {
String name = listExit.get(i);
File file1 = new File(path + File.separator + name + ".jpg");
if (file1.exists())
System.out.println("file1.delete():" + file1.delete());
}
}
private void moveImg(BufferedImage bufferedImage, File img, File outFile, String newStudentCode, String studentCode) {
try {
//创建一个空白大小相同的RGB背景
BufferedImage newBufferedReader = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedReader.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
ImageIO.write(newBufferedReader, "png", new File(outFile.getAbsolutePath(), studentCode + ".png"));
ImageIO.write(newBufferedReader, "png", new File(img.getParentFile().getAbsolutePath(), newStudentCode + ".png"));
img.delete();
System.out.println("操作成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}