|
|
@@ -3,6 +3,7 @@ package com.mokasz.image_pool.restful.controller;
|
|
|
import com.mokasz.image_pool.algorithm.OpenCvService;
|
|
|
import com.mokasz.image_pool.restful.entity.ImageQueryResponse;
|
|
|
import com.mokasz.image_pool.restful.entity.ImageSaveResponse;
|
|
|
+import com.mokasz.image_pool.restful.service.DownloadBytes;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -27,12 +28,24 @@ public class ImageController {
|
|
|
return new ImageSaveResponse("ok", 0);
|
|
|
}
|
|
|
|
|
|
+ private byte[] getFileBytes (MultipartFile file, String url) throws IOException {
|
|
|
+ if (url != null) {
|
|
|
+ return DownloadBytes.downloadAsBytesArray(url);
|
|
|
+ } else if (file != null) {
|
|
|
+ logger.info("File size: {} Name: {}", file.getSize(), file.getOriginalFilename());
|
|
|
+ return file.getBytes();
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("缺少file或url");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@PostMapping("/store")
|
|
|
public ImageSaveResponse store(
|
|
|
- @RequestParam("file") MultipartFile file, @RequestParam("pool") int pool) throws IOException {
|
|
|
- logger.info("File size: {} Name: {}", file.getSize(), file.getOriginalFilename());
|
|
|
+ @RequestParam(value = "file", required = false) MultipartFile file,
|
|
|
+ @RequestParam(value = "url", required = false) String url,
|
|
|
+ @RequestParam("pool") int pool) throws IOException {
|
|
|
float[] floats = new float[8];
|
|
|
- service.calculatePHash(file.getBytes(), floats);
|
|
|
+ service.calculatePHash(this.getFileBytes(file, url), floats);
|
|
|
float[] binaries = new float[64];
|
|
|
service.transformBinaryArray(floats, binaries);
|
|
|
logger.info("{}", binaries);
|
|
|
@@ -52,14 +65,15 @@ public class ImageController {
|
|
|
|
|
|
@PostMapping("/match")
|
|
|
public List<ImageQueryResponse> match(
|
|
|
- @RequestParam("file") MultipartFile file,
|
|
|
+ @RequestParam(value = "file", required = false) MultipartFile file,
|
|
|
+ @RequestParam(value = "url", required = false) String url,
|
|
|
@RequestParam("pool") int pool,
|
|
|
@RequestParam(value = "max", defaultValue = "1") float max,
|
|
|
@RequestParam(value = "min", defaultValue = "1") float min,
|
|
|
@RequestParam(value = "limit", defaultValue = "10") int limit)
|
|
|
throws IOException {
|
|
|
float[] floats = new float[8];
|
|
|
- service.calculatePHash(file.getBytes(), floats);
|
|
|
+ service.calculatePHash(this.getFileBytes(file, url), floats);
|
|
|
float[] binaries = new float[64];
|
|
|
service.transformBinaryArray(floats, binaries);
|
|
|
logger.info("{}", floats);
|