Explorar o código

测试vips切图

dengsixing hai 6 días
pai
achega
b8a0ac4e89

+ 7 - 0
pom.xml

@@ -143,6 +143,13 @@
             <version>3.0.0-SNAPSHOT</version>
             <version>3.0.0-SNAPSHOT</version>
         </dependency>
         </dependency>
 
 
+        <dependency>
+            <groupId>com.drewnoakes</groupId>
+            <artifactId>metadata-extractor</artifactId>
+            <version>2.19.0</version>
+        </dependency>
+
+
     </dependencies>
     </dependencies>
 
 
     <dependencyManagement>
     <dependencyManagement>

+ 7 - 4
src/main/java/com/fdkankan/scene/service/impl/SceneProServiceImpl.java

@@ -186,7 +186,10 @@ public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro>
 //            String fragmentCmd = "docker run --rm --security-opt seccomp=unconfined --pids-limit=4096 -v /mnt:/mnt vips:8.15.1 vips copy " + origFilePath + "[autorot] " +workPath;
 //            String fragmentCmd = "docker run --rm --security-opt seccomp=unconfined --pids-limit=4096 -v /mnt:/mnt vips:8.15.1 vips copy " + origFilePath + "[autorot] " +workPath;
 //            String fragmentCmd = "docker run --rm --security-opt seccomp=unconfined --pids-limit=4096 -v /mnt:/mnt vips:8.15.1 vips dzsave --tile-size "  + tileSize + " " + origFilePath + "[autorot] " +  workPath;
 //            String fragmentCmd = "docker run --rm --security-opt seccomp=unconfined --pids-limit=4096 -v /mnt:/mnt vips:8.15.1 vips dzsave --tile-size "  + tileSize + " " + origFilePath + "[autorot] " +  workPath;
 //            String fragmentCmd = "vips dzsave " + origFilePath + "[autorot] " + workPath + " --tile-size " + tileSize + " --strip";
 //            String fragmentCmd = "vips dzsave " + origFilePath + "[autorot] " + workPath + " --tile-size " + tileSize + " --strip";
-            String fragmentCmd = "docker run --rm --security-opt seccomp=unconfined --pids-limit=4096 -v /mnt:/mnt vips:8.15.1 vips dzsave --tile-size "  + tileSize + " " + origFilePath + " " + workPath;
+            String tempFilePath = workPath + "/temp_rotated.v";
+            String autorotCmd = "docker run --rm --security-opt seccomp=unconfined --pids-limit=4096 -v /mnt:/mnt vips:8.15.1 vips autorot " + origFilePath + " " + tempFilePath;
+            CmdUtils.callLine(autorotCmd);
+            String fragmentCmd = "docker run --rm --security-opt seccomp=unconfined --pids-limit=4096 -v /mnt:/mnt vips:8.15.1 vips dzsave --tile-size "  + tileSize + " " + tempFilePath + " " + workPath;
             log.info("fragmentCmd:{}" + fragmentCmd);
             log.info("fragmentCmd:{}" + fragmentCmd);
             CmdUtils.callLine(fragmentCmd);
             CmdUtils.callLine(fragmentCmd);
             if(!ComputerUtil.checkComputeCompleted(dziPath, 5, 200)){
             if(!ComputerUtil.checkComputeCompleted(dziPath, 5, 200)){
@@ -226,9 +229,9 @@ public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro>
             fYunFileService.uploadFile(origFilePath, ossPath + sid + "." + extName);
             fYunFileService.uploadFile(origFilePath, ossPath + sid + "." + extName);
 
 
         }finally {
         }finally {
-            FileUtil.del(workPath);
-            FileUtil.del(dziPath);
-            FileUtil.del(outFilesPath);
+//            FileUtil.del(workPath);
+//            FileUtil.del(dziPath);
+//            FileUtil.del(outFilesPath);
         }
         }
 
 
         Map<String, Object> hw = new HashMap<>();
         Map<String, Object> hw = new HashMap<>();

+ 36 - 0
src/main/java/com/fdkankan/scene/util/ImageUtil.java

@@ -0,0 +1,36 @@
+package com.fdkankan.scene.util;
+
+import com.drew.imaging.ImageMetadataReader;
+import com.drew.metadata.Metadata;
+import com.drew.metadata.exif.ExifIFD0Directory;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.File;
+
+public class ImageUtil {
+
+    public static boolean isFakePortrait(File file) throws Exception {
+        // 1. 读像素尺寸
+        BufferedImage img = ImageIO.read(file);
+        int width = img.getWidth();
+        int height = img.getHeight();
+
+        // 2. 读 EXIF
+        Metadata metadata = ImageMetadataReader.readMetadata(file);
+        ExifIFD0Directory exif = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
+
+        boolean hasOrientation = false;
+        if (exif != null && exif.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
+            hasOrientation = true;
+        }
+
+        // 3. 判定伪竖图
+        return width > height && !hasOrientation;
+    }
+
+
+    public static void main(String[] args) throws Exception {
+        System.out.println(isFakePortrait(new File("D:\\test-vips\\1.jpg")));
+    }
+}