Przeglądaj źródła

调整人脸方向问题

dzx 5 miesięcy temu
rodzic
commit
04a62dab43

+ 24 - 0
src/main/java/com/xjrsoft/common/utils/ImageUtil.java

@@ -1,5 +1,6 @@
 package com.xjrsoft.common.utils;
 
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.xjrsoft.common.exception.MyException;
 import org.apache.commons.lang3.StringUtils;
@@ -23,6 +24,8 @@ import java.util.Base64;
 import java.util.HashMap;
 import java.util.Map;
 
+import static org.apache.poi.util.LittleEndianConsts.BYTE_SIZE;
+
 public class ImageUtil {
     public static byte[] getByteByPic(String imageUrl) throws IOException {
         File imageFile = new File(imageUrl);
@@ -138,4 +141,25 @@ public class ImageUtil {
         return map.get(suffix) + ImageUtil.bytesEncode2Base64(resultImg);
     }
 
+    public static byte[] dealPicture(MultipartFile file, Integer anInt) {
+        byte[] fileByte;
+        try {
+            byte[] imageBytes = file.getBytes();
+            fileByte = ImageUtil.compressUnderSize(imageBytes, BYTE_SIZE);
+            InputStream input = new ByteArrayInputStream(fileByte);
+            BufferedImage image = ImageIO.read(input);
+            BufferedImage rotatedImage = PictureUtil.rotatePhonePhoto(image, anInt);
+
+            fileByte = PictureUtil.convertBufferedImageToByteArray(rotatedImage);
+        } catch (Exception e) {
+            throw new MyException("图片格式不符");
+        }
+
+        if (ObjectUtils.isEmpty(fileByte) || fileByte.length == 0) {
+            throw new MyException("图片格式不符");
+        }
+
+        return fileByte;
+    }
+
 }

+ 70 - 0
src/main/java/com/xjrsoft/common/utils/PictureUtil.java

@@ -0,0 +1,70 @@
+package com.xjrsoft.common.utils;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+public class PictureUtil {
+
+    public static byte[] convertBufferedImageToByteArray(BufferedImage image) {
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            ImageIO.write(image, "JPEG", baos);
+            return baos.toByteArray();
+        } catch (IOException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    /**
+     * 旋转照片
+     *
+     * @return
+     */
+    public static BufferedImage rotatePhonePhoto(BufferedImage image, int orientation) {
+        int angle = 0;
+        // 原图片的方向信息
+        if (6 == orientation) {
+            //6旋转90
+            angle = 90;
+        } else if (3 == orientation) {
+            //3旋转180
+            angle = 180;
+        } else if (8 == orientation) {
+            //8旋转90
+            angle = 270;
+        } else {
+            // 不旋转,直接返回
+            return image;
+        }
+
+        BufferedImage src;
+        src = image;
+        int src_width = src.getWidth(null);
+        int src_height = src.getHeight(null);
+
+        int swidth = src_width;
+        int sheight = src_height;
+
+        if (angle == 90 || angle == 270) {
+            swidth = src_height;
+            sheight = src_width;
+        }
+
+        Rectangle rect_des = new Rectangle(new Dimension(swidth, sheight));
+
+        BufferedImage res = new BufferedImage(rect_des.width, rect_des.height, BufferedImage.TYPE_INT_RGB);
+        Graphics2D g2 = res.createGraphics();
+
+        g2.translate((rect_des.width - src_width) / 2,
+                (rect_des.height - src_height) / 2);
+        g2.rotate(Math.toRadians(angle), src_width / 2, src_height / 2);
+
+        g2.drawImage(src, null, null);
+
+        return res;
+
+    }
+}

+ 10 - 1
src/main/java/com/xjrsoft/module/hikvision/util/FaceImportUtil.java

@@ -5,7 +5,11 @@ import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.xjrsoft.common.utils.ImageUtil;
+import com.xjrsoft.common.utils.PictureUtil;
 
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -88,7 +92,6 @@ public class FaceImportUtil {
                 if(personPhoto.size() > 0){
                     faceId = personPhoto.get(0).getAsJsonObject().get("personPhotoIndexCode").getAsString();
                 }
-
             }
         }
         String response = null;
@@ -128,6 +131,12 @@ public class FaceImportUtil {
             //压缩到200k
             imageBytes = ImageUtil.compressUnderSize(imageBytes, 204800);
 
+            InputStream input = new ByteArrayInputStream(imageBytes);
+            BufferedImage image = ImageIO.read(input);
+            BufferedImage rotatedImage = PictureUtil.rotatePhonePhoto(image, 1);
+
+            imageBytes = PictureUtil.convertBufferedImageToByteArray(rotatedImage);
+
             //压缩之后,存入本地
             String[] split = imageUrl.split("\\.");
             String suffix = split[split.length - 1];