Skip to content

Commit 811376f

Browse files
committed
Java: MultiDataSource 完善渲染画框及标签等
1 parent 4d1e111 commit 811376f

5 files changed

Lines changed: 102 additions & 82 deletions

File tree

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/FileController.java‎

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
import apijson.demo.DemoParser;
3535

36-
import javax.imageio.ImageIO;
37-
3836
import static com.google.common.io.Files.getFileExtension;
3937

4038
/**文件相关的控制器,包括上传、下载、浏览等
@@ -134,7 +132,8 @@ public JSONObject upload(@RequestParam("file") MultipartFile file) {
134132
res.put("path", "/download/" + name);
135133
res.put("size", file.getBytes().length);
136134
return new DemoParser().extendSuccessResult(res);
137-
} catch (Exception e) {
135+
}
136+
catch (Exception e) {
138137
e.printStackTrace();
139138
return new DemoParser().newErrorResult(e);
140139
}
@@ -204,9 +203,12 @@ private MediaType determineContentType(String fileName) {
204203
@GetMapping("/download/cv/report/{id}")
205204
@ResponseBody
206205
public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "id") String idStr, HttpSession session) throws FileNotFoundException, IOException {
207-
long reportId = Long.parseLong(idStr);
208-
boolean isLast = reportId <= 0;
209-
String name = "CVAuto_report_" + (isLast ? "last" : reportId) + ".xlsx";
206+
long repOrDocId = Long.parseLong(idStr);
207+
if (repOrDocId <= 0) {
208+
throw new IllegalArgumentException("id 必须为 > 0 的 reportId 或 documentId 有效整数!");
209+
}
210+
211+
String name = "CVAuto_report_" + repOrDocId + ".xlsx";
210212
String path = fileUploadRootDir + name;
211213
File file = new File(path);
212214
long size = file.exists() ? file.length() : 0;
@@ -219,22 +221,20 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "id") String
219221

220222
{ // TestRecord <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
221223
JSONObject testRecord = new JSONObject();
222-
if (isLast) {
223-
testRecord.put("@order", "reportId-");
224-
} else {
225-
testRecord.put("reportId", reportId);
226-
}
224+
testRecord.put("reportId", repOrDocId);
225+
testRecord.put("documentId", repOrDocId);
226+
testRecord.put("@combine", "reportId,documentId");
227227
testRecord.put("@column", "reportId,documentId,randomId,sameIds");
228+
testRecord.put("@order", "reportId-");
228229

229230
request.put("TestRecord", testRecord);
230231
} // TestRecord >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
231232

232233
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
233234
JSONObject item = new JSONObject();
234-
item.put("count", 3);
235-
//item.put("count", 0);
235+
item.put("count", 0);
236+
// item.put("count", 3);
236237
item.put("join", "&/TestRecord");
237-
//item.put("join", "@/TestRecord");
238238

239239
{ // Random <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
240240
JSONObject random = new JSONObject();
@@ -250,19 +250,12 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "id") String
250250

251251
{ // TestRecord <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
252252
JSONObject testRecord = new JSONObject();
253-
testRecord.put("@column", "id,total,correct,wrong,compare,response");
253+
testRecord.put("@column", "id,total,correct,wrong,wrongs,compare,response");
254254
testRecord.put("randomId@", "/Random/id");
255255
testRecord.put("documentId@", "TestRecord/documentId");
256+
testRecord.put("reportId@", "TestRecord/reportId");
256257

257258
//testRecord.put("idx{}@", "TestRecord/sameIds");
258-
if (isLast) {
259-
testRecord.put("reportId@", "TestRecord/reportId");
260-
//testRecord.put("@combine", "idx | reportId@");
261-
} else {
262-
testRecord.put("reportId", reportId);
263-
//testRecord.put("@combine", "idx | reportId");
264-
}
265-
//testRecord.put("@key", "idx:(id)");
266259
//testRecord.put("@combine", "idx | reportId");
267260

268261
testRecord.put("total>=", 0);
@@ -281,20 +274,17 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "id") String
281274

282275
{ // TestRecord <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
283276
JSONObject testRecord = new JSONObject();
284-
testRecord.put("@column", "id,randomId,total,correct,wrong,compare,response");
277+
testRecord.put("@column", "id,randomId,total,correct,wrong,wrongs,compare,response");
285278
testRecord.put("id{}@", "TestRecord/sameIds");
286279
testRecord.put("documentId@", "TestRecord/documentId");
287-
if (isLast) {
288-
testRecord.put("reportId@", "TestRecord/reportId");
289-
} else {
290-
testRecord.put("reportId", reportId);
291-
}
280+
// testRecord.put("reportId@", "TestRecord/reportId");
292281
testRecord.put("total>=", 0);
293282
testRecord.put("correct>=", 0);
294283
testRecord.put("wrong>=", 0);
295284
testRecord.put("@order", "date-");
296285

297286
item.put("TestRecord", testRecord);
287+
item.put("count", 0);
298288
} // TestRecord >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
299289

300290
request.put("TestRecord[]", item);
@@ -307,16 +297,13 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "id") String
307297
throw new IOException(JSONResponse.getMsg(response));
308298
}
309299

310-
// JSONObject lastTr = response.getJSONObject("TestRecord");
311-
// long documentId = lastTr == null ? 0 : lastTr.getLongValue("documentId");
312-
// long randomId = lastTr == null ? 0 : lastTr.getLongValue("randomId");
313-
if (isLast) {
314-
JSONObject lastTr = response.getJSONObject("TestRecord");
315-
reportId = lastTr == null ? 0 : lastTr.getLongValue("reportId");
316-
if (reportId > 0) {
317-
name = "CVAuto_report_" + reportId + ".xlsx";
318-
path = fileUploadRootDir + name;
319-
}
300+
JSONObject lastTr = response.getJSONObject("TestRecord");
301+
long reportId = lastTr == null ? 0 : lastTr.getLongValue("reportId");
302+
// long documentId = lastTr == null ? 0 : lastTr.getLongValue("documentId");
303+
// long randomId = lastTr == null ? 0 : lastTr.getLongValue("randomId");
304+
if (reportId != repOrDocId) {
305+
name = "CVAuto_report_" + (reportId > 0 ? reportId : repOrDocId + "_last") + ".xlsx";
306+
path = fileUploadRootDir + name;
320307
}
321308

322309
JSONArray array = response.getJSONArray("[]");
@@ -373,14 +360,15 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "id") String
373360
JSONObject renderReq = new JSONObject();
374361
renderReq.put("img", img);
375362
renderReq.put("data", resObj);
363+
renderReq.put("wrongs", testRecord.getJSONArray("wrongs"));
376364
String body = renderReq.toJSONString();
377365
HttpHeaders headers = new HttpHeaders();
378366
String renderStr = demoController.sendRequest(session, HttpMethod.POST, "http://localhost:3003/cv/render", body, headers);
379367
renderStr = StringUtil.trim(renderStr);
380368
if (renderStr.length() > 100) {
381369
File renderFile = new File(fileUploadRootDir + nfn);
382370
try (FileOutputStream fos = new FileOutputStream(renderFile)) {
383-
int commaInd = renderStr.startsWith("data:image/") ? -1 : renderStr.indexOf("base64,");
371+
int commaInd = renderStr.startsWith("data:image/") ? renderStr.indexOf("base64,") : -1;
384372
String base64 = commaInd < 0 ? renderStr : renderStr.substring(commaInd + "base64,".length());
385373
byte[] bytes = Base64.getDecoder().decode(base64);
386374
fos.write(bytes);

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/apijson/StringUtil.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var StringUtil = {
2424
* @return
2525
*/
2626
get: function(s) {
27-
return s == null ? '' : (JSONResponse.isString(s) ? s : JSON.stringify(s));
27+
// return s == null ? '' : (JSONResponse.isString(s) ? s : JSON.stringify(s));
28+
return s == null ? '' : (typeof s == 'string' ? s : JSON.stringify(s));
2829
},
2930

3031
/**获取去掉前后空格后的string,为null则返回''

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/js/main.js‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8060,21 +8060,25 @@ https://github.com/Tencent/APIJSON/issues
80608060
req = newReq
80618061
}
80628062

8063-
axios.interceptors.request.use(function (config) {
8064-
config.metadata = { startTime: new Date().getTime()}
8065-
return config;
8066-
}, function (error) {
8067-
return Promise.reject(error);
8068-
});
8069-
axios.interceptors.response.use(function (response) {
8070-
response.config.metadata.endTime = new Date().getTime()
8071-
response.duration = response.config.metadata.endTime - response.config.metadata.startTime
8072-
return response;
8073-
}, function (error) {
8074-
error.config.metadata.endTime = new Date().getTime();
8075-
error.duration = error.config.metadata.endTime - error.config.metadata.startTime;
8076-
return Promise.reject(error);
8077-
});
8063+
var interceptors = axios.interceptors
8064+
if (interceptors) {
8065+
interceptors.request.use(function (config) {
8066+
config.metadata = { startTime: new Date().getTime()}
8067+
return config;
8068+
}, function (error) {
8069+
return Promise.reject(error);
8070+
})
8071+
8072+
interceptors.response.use(function (response) {
8073+
response.config.metadata.endTime = new Date().getTime()
8074+
response.duration = response.config.metadata.endTime - response.config.metadata.startTime
8075+
return response;
8076+
}, function (error) {
8077+
error.config.metadata.endTime = new Date().getTime();
8078+
error.duration = error.config.metadata.endTime - error.config.metadata.startTime;
8079+
return Promise.reject(error);
8080+
})
8081+
}
80788082

80798083
// axios.defaults.withcredentials = true
80808084
axios({

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/cv/js/server.js‎

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const bodyParser = require('koa-bodyparser');
44
// const Vue = require('vue');
55
const {getRequestFromURL, App} = require('./main');
66
// const { createBundleRenderer } = require('vue-server-renderer')
7-
const { createCanvas, Image } = require("canvas");
7+
const { createCanvas, Image, loadImage } = require("canvas");
88

99
const JSONResponse = require('../apijson/JSONResponse');
1010
const StringUtil = require('../apijson/StringUtil');
1111

12+
//const DEBUG = false;
13+
const DEBUG = true;
1214
var isCrossEnabled = true; // false;
1315
var isLoading = false;
1416
var startTime = 0;
@@ -177,14 +179,18 @@ app.use(async ctx => {
177179
var stdd = body.standard;
178180
var response = typeof res != 'string' ? res : (StringUtil.isEmpty(res, true) ? null : JSON.parse(res));
179181
var standard = typeof stdd != 'string' ? stdd : (StringUtil.isEmpty(stdd, true) ? null : JSON.parse(stdd));
180-
console.log('\n\nresponse = ' + JSON.stringify(response));
181-
console.log('\n\nstdd = ' + JSON.stringify(stdd));
182+
if (DEBUG) {
183+
console.log('\n\nresponse = ' + JSON.stringify(response));
184+
console.log('\n\nstdd = ' + JSON.stringify(stdd));
185+
}
182186
var compare = JSONResponse.compareResponse(null, standard, response || {}, '', isML, null, null, false) || {}
183187

184188
if (body.newStandard) {
185189
compare.newStandard = JSONResponse.updateFullStandard(standard, response, isML)
186190
}
187-
console.log('\n\ncompare = ' + JSON.stringify(compare));
191+
if (DEBUG) {
192+
console.log('\n\ncompare = ' + JSON.stringify(compare));
193+
}
188194

189195
ctx.status = ctx.response.status = 200;
190196
ctx.body = ctx.response.body = compare == null ? '' : JSON.stringify(compare);
@@ -194,16 +200,18 @@ app.use(async ctx => {
194200
// }
195201
}
196202
else if (ctx.path == '/cv/render') {
197-
done = [false];
198-
// var json = '';
199-
// ctx.req.addListener('data', (data) => {
200-
// json += data;
201-
// })
202-
// ctx.req.addListener('end', function() {
203-
// console.log(json);
204-
var body = ctx.body || ctx.req.body || ctx.request.body || {} // || JSON.parse(json) || {};
205-
console.log('\n\n <<<<<<< body = ' + body);
206-
console.log('\n\n <<<<<<< body = ' + JSON.stringify(body));
203+
// done = [false];
204+
// var json = '';
205+
// ctx.req.addListener('data', (data) => {
206+
// json += data;
207+
// })
208+
// ctx.req.addListener('end', function() {
209+
// console.log(json);
210+
var body = ctx.body || ctx.req.body || ctx.request.body|| {}; // || JSON.parse(json) || {};
211+
if (DEBUG) {
212+
console.log('\n\n <<<<<<< body = ' + body);
213+
console.log('\n\n <<<<<<< body = ' + JSON.stringify(body));
214+
}
207215
var base64 = body.img;
208216
const data = typeof base64 != 'string' || base64.length < 10 ? '' : base64.replace(/^data:image\/\w+;base64,/, '');
209217
if (StringUtil.isEmpty(data, true)) {
@@ -222,38 +230,47 @@ app.use(async ctx => {
222230
}
223231

224232
console.log('\n await loadImage(base64) >>>');
225-
// const img = await loadImage(base64);
226-
const buf = Buffer.from(data, 'base64');
227-
const img = new Image();
228-
img.src = buf;
233+
var img;
234+
if (StringUtil.isUri(data)) {
235+
// img = await loadImage(data);
236+
img = loadImage(data);
237+
} else {
238+
const buf = Buffer.from(data, 'base64');
239+
img = new Image();
240+
img.src = buf;
241+
}
229242

230243
const w = img.width;
231244
const h = img.height;
232245

233246
console.log('\n createCanvas(' + w + ', ' + h + ') >>>');
234247
// 画布
235248
const canvas = createCanvas(w, h);
236-
const ctx = canvas.getContext("2d");
237-
ctx.imageSmoothingEnabled = true;
249+
const canvasCtx = canvas.getContext("2d");
250+
canvasCtx.imageSmoothingEnabled = true;
238251
// 背景图
239252
console.log('\n ctx.drawImage(img, 0, 0, ' + w + ', ' + h + ') >>>');
240-
ctx.drawImage(img, 0, 0, w, h);
253+
canvasCtx.drawImage(img, 0, 0, w, h);
241254

255+
var wrongs = body.wrongs;
242256
console.log('\n JSONResponse.drawDetections(canvas, det, {...}, img, null, false) >>>');
243257
JSONResponse.drawDetections(canvas, det, {
244258
labelBackground: true,
245-
rotateBoxes: true
259+
rotateBoxes: true,
260+
wrongs: wrongs
246261
}, img, null, false);
247262

248263
console.log('\n var render = canvas.toDataURL("image/jpeg") >>>');
249264
var render = canvas.toDataURL("image/jpeg");
250-
console.log('\n\n >>> render = ' + render);
265+
if (DEBUG) {
266+
console.log('\n\n >>> render = ' + render);
267+
}
251268
ctx.status = ctx.response.status = StringUtil.isEmpty(render) ? 500 : 200;
252269
ctx.body = ctx.response.body = render;
253270
done = [true];
254-
// })
255-
// while (! done[0]) {
256-
// }
271+
// })
272+
// while (! done[0]) {
273+
// }
257274
}
258275
});
259276

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"axios": "^1.11.0",
4+
"canvas": "^3.2.0",
5+
"json5": "^2.2.3",
6+
"koa": "^3.0.1",
7+
"koa-bodyparser": "^4.4.1",
8+
"koa2-cors": "^2.0.6"
9+
}
10+
}

0 commit comments

Comments
 (0)