Kaynağa Gözat

Style: reformat

wudi 5 yıl önce
ebeveyn
işleme
9eaab06514

+ 3 - 3
src/main/java/com/mokasz/image_pool/ImagePoolApplication.java

@@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 @SpringBootApplication
 public class ImagePoolApplication {
 
-	public static void main(String[] args) {
-		SpringApplication.run(ImagePoolApplication.class, args);
-	}
+    public static void main(String[] args) {
+        SpringApplication.run(ImagePoolApplication.class, args);
+    }
 
 }

+ 1 - 1
src/main/java/com/mokasz/image_pool/algorithm/FeatureExtractor.java

@@ -19,7 +19,7 @@ public class FeatureExtractor {
         Loader.load(opencv_java.class);
     }
 
-    public static void compute (byte[] fileBytes) {
+    public static void compute(byte[] fileBytes) {
         Mat input = Imgcodecs.imdecode(new MatOfByte(fileBytes), Imgcodecs.IMREAD_GRAYSCALE);
         ORB detector = ORB.create(128);
         Mat output = new Mat();

+ 2 - 1
src/main/java/com/mokasz/image_pool/algorithm/OpenCvService.java

@@ -14,6 +14,7 @@ public class OpenCvService {
     static {
         Loader.load(opencv_java.class);
     }
+
     private final PHash pHash = PHash.create();
     public static final int HashLength = 8;
 
@@ -26,7 +27,7 @@ public class OpenCvService {
         }
     }
 
-    public void calculatePHash (byte[] fileBytes, float[] vector) {
+    public void calculatePHash(byte[] fileBytes, float[] vector) {
         Mat input = Imgcodecs.imdecode(new MatOfByte(fileBytes), Imgcodecs.IMREAD_UNCHANGED);
         Mat output = new Mat();
         pHash.compute(input, output);

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

@@ -20,66 +20,66 @@ import java.util.List;
 @RequiredArgsConstructor
 public class ImageController {
 
-  private final OpenCvService service;
+    private final OpenCvService service;
 
-  private final JdbcTemplate jdbcTemplate;
+    private final JdbcTemplate jdbcTemplate;
 
-  private byte[] getFileBytes (MultipartFile file, String url) throws IOException {
-    if (url != null) {
-      return DownloadBytes.downloadAsBytesArray(url);
-    } else if (file != null) {
-      return file.getBytes();
-    } else {
-      throw new RuntimeException("缺少file或url");
+    private byte[] getFileBytes(MultipartFile file, String url) throws IOException {
+        if (url != null) {
+            return DownloadBytes.downloadAsBytesArray(url);
+        } else if (file != null) {
+            return file.getBytes();
+        } else {
+            throw new RuntimeException("缺少file或url");
+        }
     }
-  }
 
-  @PostMapping("/store")
-  public ImageSaveResponse store(
-      @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(this.getFileBytes(file, url), floats);
-    log.info("size:",floats);
-    float[] binaries = new float[64];
-    service.transformBinaryArray(floats, binaries);
-    String hash = service.getReadableHash(floats);
-    jdbcTemplate.update(
-            "INSERT INTO image_pool.image_hash VALUES (?, NOW(), ?) ON CONFLICT (hash) DO NOTHING",
-            hash,
-            binaries);
-    jdbcTemplate.update(
-        "INSERT INTO image_pool.image_to_pool VALUES (?, ?, NOW()) ON CONFLICT (hash, pool_id) DO NOTHING",
-        hash,
-        pool);
-    return new ImageSaveResponse(hash, pool);
-  }
+    @PostMapping("/store")
+    public ImageSaveResponse store(
+            @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(this.getFileBytes(file, url), floats);
+        log.info("size:", floats);
+        float[] binaries = new float[64];
+        service.transformBinaryArray(floats, binaries);
+        String hash = service.getReadableHash(floats);
+        jdbcTemplate.update(
+                "INSERT INTO image_pool.image_hash VALUES (?, NOW(), ?) ON CONFLICT (hash) DO NOTHING",
+                hash,
+                binaries);
+        jdbcTemplate.update(
+                "INSERT INTO image_pool.image_to_pool VALUES (?, ?, NOW()) ON CONFLICT (hash, pool_id) DO NOTHING",
+                hash,
+                pool);
+        return new ImageSaveResponse(hash, pool);
+    }
 
-  @PostMapping("/match")
-  public List<ImageQueryResponse> match(
-      @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(this.getFileBytes(file, url), floats);
-    float[] binaries = new float[64];
-    service.transformBinaryArray(floats, binaries);
-    List<ImageQueryResponse> list =
-        jdbcTemplate.query(
-            "SELECT * FROM (SELECT hash, (64 - l2_distance(?::FLOAT2[], vector)) / 64 AS similarity FROM image_pool.image_hash WHERE hash IN (SELECT hash FROM image_pool.image_to_pool WHERE pool_id = ?) ORDER BY vector <-> ?::FLOAT2[] ASC) t WHERE similarity >=? and similarity<=? ORDER BY similarity DESC LIMIT ?",
-            (ResultSet rs, int rowNum) ->
-                new ImageQueryResponse(rs.getFloat("similarity"), rs.getString("hash")),
-            binaries,
-            pool,
-            binaries,
-            min,
-            max,
-            limit);
-    return list;
-  }
+    @PostMapping("/match")
+    public List<ImageQueryResponse> match(
+            @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(this.getFileBytes(file, url), floats);
+        float[] binaries = new float[64];
+        service.transformBinaryArray(floats, binaries);
+        List<ImageQueryResponse> list =
+                jdbcTemplate.query(
+                        "SELECT * FROM (SELECT hash, (64 - l2_distance(?::FLOAT2[], vector)) / 64 AS similarity FROM image_pool.image_hash WHERE hash IN (SELECT hash FROM image_pool.image_to_pool WHERE pool_id = ?) ORDER BY vector <-> ?::FLOAT2[] ASC) t WHERE similarity >=? and similarity<=? ORDER BY similarity DESC LIMIT ?",
+                        (ResultSet rs, int rowNum) ->
+                                new ImageQueryResponse(rs.getFloat("similarity"), rs.getString("hash")),
+                        binaries,
+                        pool,
+                        binaries,
+                        min,
+                        max,
+                        limit);
+        return list;
+    }
 }

+ 3 - 2
src/main/java/com/mokasz/image_pool/restful/controller/IndexController.java

@@ -15,13 +15,14 @@ public class IndexController {
     @Value("${pom.version}")
     private String version;
 
-    private String getHostname () {
+    private String getHostname() {
         try {
             return InetAddress.getLocalHost().getHostName();
         } catch (UnknownHostException e) {
-            return "x";
+            return "";
         }
     }
+
     @GetMapping("/")
     public IndexResponse index() {
         return new IndexResponse(version, getHostname());

+ 1 - 1
src/main/java/com/mokasz/image_pool/restful/service/DownloadBytes.java

@@ -6,7 +6,7 @@ import java.io.IOException;
 import java.net.URL;
 
 public class DownloadBytes {
-    public static byte[] downloadAsBytesArray (String url) throws IOException {
+    public static byte[] downloadAsBytesArray(String url) throws IOException {
         return IOUtils.toByteArray(new URL(url));
     }
 }