Selaa lähdekoodia

Demo of OpenCV PHash Calculation and PgSQL access

wudi 5 vuotta sitten
vanhempi
commit
df5e31d804

+ 26 - 5
pom.xml

@@ -23,20 +23,38 @@
 		</dependency>
 
 		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+		</dependency>
+
+		<dependency>
+			<groupId>org.projectlombok</groupId>
+			<artifactId>lombok</artifactId>
+			<version>1.18.20</version>
+			<scope>provided</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-devtools</artifactId>
+			<version>2.4.3</version>
+		</dependency>
+
+		<dependency>
 			<groupId>org.postgresql</groupId>
 			<artifactId>postgresql</artifactId>
 			<scope>runtime</scope>
 		</dependency>
 		<dependency>
+			<groupId>org.bytedeco</groupId>
+			<artifactId>opencv-platform</artifactId>
+			<version>4.5.1-1.5.5</version>
+		</dependency>
+		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-test</artifactId>
 			<scope>test</scope>
 		</dependency>
-		<dependency>
-			<groupId>org.openpnp</groupId>
-			<artifactId>opencv</artifactId>
-			<version>[4.3.0,)</version>
-		</dependency>
 	</dependencies>
 
 	<build>
@@ -44,6 +62,9 @@
 			<plugin>
 				<groupId>org.springframework.boot</groupId>
 				<artifactId>spring-boot-maven-plugin</artifactId>
+				<configuration>
+					<fork>true</fork><!-- 热部署 -->
+				</configuration>
 			</plugin>
 		</plugins>
 	</build>

+ 50 - 0
src/main/java/com/mokasz/image_pool/restful/controller/ImageController.java

@@ -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");
+    }
+}

+ 9 - 0
src/main/java/com/mokasz/image_pool/restful/entity/ImageQueryResponse.java

@@ -0,0 +1,9 @@
+package com.mokasz.image_pool.restful.entity;
+
+import lombok.Data;
+
+@Data
+public class ImageQueryResponse {
+    private final int retCode;
+    private final String message;
+}

+ 9 - 0
src/main/java/com/mokasz/image_pool/restful/entity/ImageSaveResponse.java

@@ -0,0 +1,9 @@
+package com.mokasz.image_pool.restful.entity;
+
+import lombok.Data;
+
+@Data
+public class ImageSaveResponse {
+    private final String hash;
+    private final long copies;
+}