|
|
@@ -0,0 +1,50 @@
|
|
|
+package com.mokasz.image_pool.restful.controller;
|
|
|
+
|
|
|
+import com.mokasz.image_pool.restful.entity.ImageQueryResponse;
|
|
|
+import com.mokasz.image_pool.restful.entity.ImageSaveResponse;
|
|
|
+import org.bytedeco.javacpp.Loader;
|
|
|
+import org.bytedeco.opencv.opencv_java;
|
|
|
+import org.opencv.core.Mat;
|
|
|
+import org.opencv.core.MatOfByte;
|
|
|
+import org.opencv.img_hash.PHash;
|
|
|
+import org.opencv.imgcodecs.Imgcodecs;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/service/image")
|
|
|
+public class ImageController {
|
|
|
+ // 静态初始化 OpenCV
|
|
|
+ static {
|
|
|
+ Loader.load(opencv_java.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
+ private final PHash pHash = PHash.create();
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ @PostMapping("/store")
|
|
|
+ public ImageSaveResponse store (@RequestParam("file") MultipartFile file) throws IOException {
|
|
|
+ logger.info("File size: {} Name: {}", file.getSize(), file.getOriginalFilename());
|
|
|
+ Mat input = Imgcodecs.imdecode(new MatOfByte(file.getBytes()), Imgcodecs.IMREAD_UNCHANGED);
|
|
|
+ Mat output = new Mat();
|
|
|
+ pHash.compute(input, output);
|
|
|
+ return new ImageSaveResponse(output.dump(), 1L);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/match")
|
|
|
+ public ImageQueryResponse match (@RequestParam("pool") String pool) {
|
|
|
+ int updated = jdbcTemplate.update("INSERT INTO test_conn (name) VALUES (?)", pool);
|
|
|
+ return new ImageQueryResponse(updated, "Updated");
|
|
|
+ }
|
|
|
+}
|