傅行帆 преди 6 години
родител
ревизия
6c7561ab67

+ 81
- 0
CODE/smart-community/app-api/src/main/java/com/community/huiju/common/PicUtils.java Целия файл

@@ -0,0 +1,81 @@
1
+package com.community.huiju.common;
2
+
3
+import net.coobird.thumbnailator.Thumbnails;
4
+import org.slf4j.Logger;
5
+import org.slf4j.LoggerFactory;
6
+
7
+import java.io.ByteArrayInputStream;
8
+import java.io.ByteArrayOutputStream;
9
+
10
+/**
11
+ * 图片压缩Utils
12
+ *
13
+ * @author worstEzreal
14
+ * @version V1.1.0
15
+ * @date 2018/3/12
16
+ */
17
+public class PicUtils {
18
+
19
+    private static Logger logger = LoggerFactory.getLogger(PicUtils.class);
20
+
21
+   /*public static void main(String[] args) throws IOException {
22
+        byte[] bytes = FileUtils.readFileToByteArray(new File("C:\\Users\\DELL\\Pictures\\timg.jpg"));
23
+      long l = System.currentTimeMillis();
24
+       bytes = PicUtils.compressPicForScale(bytes, 20, "x");// 图片小于300kb
25
+        System.out.println(System.currentTimeMillis() - l);
26
+        FileUtils.writeByteArrayToFile(new File("C:\\Users\\DELL\\Pictures\\timg.jpg"), bytes);
27
+   }*/
28
+
29
+    /**
30
+     * 根据指定大小压缩图片
31
+     *
32
+     * @param imageBytes  源图片字节数组
33
+     * @param desFileSize 指定图片大小,单位kb
34
+     * @param imageId     影像编号
35
+     * @return 压缩质量后的图片字节数组
36
+     */
37
+    public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize, String imageId) {
38
+        if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
39
+            return imageBytes;
40
+        }
41
+        long srcSize = imageBytes.length;
42
+        double accuracy = getAccuracy(srcSize / 1024);
43
+        try {
44
+            while (imageBytes.length > desFileSize * 1024) {
45
+                ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
46
+                ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
47
+                Thumbnails.of(inputStream)
48
+                        .scale(accuracy)
49
+                        .outputQuality(accuracy)
50
+                        .toOutputStream(outputStream);
51
+                imageBytes = outputStream.toByteArray();
52
+            }
53
+            logger.info("【图片压缩】imageId={} | 图片原大小={}kb | 压缩后大小={}kb",
54
+                    imageId, srcSize / 1024, imageBytes.length / 1024);
55
+        } catch (Exception e) {
56
+            logger.error("【图片压缩】msg=图片压缩失败!", e);
57
+        }
58
+        return imageBytes;
59
+    }
60
+
61
+    /**
62
+     * 自动调节精度(经验数值)
63
+     *
64
+     * @param size 源图片大小
65
+     * @return 图片压缩质量比
66
+     */
67
+    private static double getAccuracy(long size) {
68
+        double accuracy;
69
+        if (size < 900) {
70
+            accuracy = 0.85;
71
+        } else if (size < 2047) {
72
+            accuracy = 0.6;
73
+        } else if (size < 3275) {
74
+            accuracy = 0.44;
75
+        } else {
76
+            accuracy = 0.4;
77
+        }
78
+        return accuracy;
79
+    }
80
+
81
+}

+ 4
- 4
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/impl/FaceServicelimpl.java Целия файл

@@ -5,8 +5,9 @@ import com.alibaba.fastjson.JSONObject;
5 5
 import com.community.commom.ailiyun.AESDecode;
6 6
 import com.community.commom.constant.Constant;
7 7
 import com.community.commom.mode.ResponseBean;
8
+import com.community.huiju.common.PicUtils;
8 9
 import com.community.huiju.common.hk.HKConstant;
9
-import com.community.huiju.common.hk.test.PicUtils;
10
+import com.community.huiju.common.hk.HKOpenApi;
10 11
 import com.community.huiju.dao.TaFaceMapper;
11 12
 import com.community.huiju.dao.TaSysRoleMapper;
12 13
 import com.community.huiju.dao.TaUserMapper;
@@ -172,7 +173,7 @@ public class FaceServicelimpl implements FaceServicel {
172 173
             JSONObject jsonObject = JSONObject.parseObject(responseBean.getMessage());
173 174
             Integer type = (Integer) jsonObject.get("errno");
174 175
             Integer faceNum= (Integer) jsonObject.get("face_num");
175
-            if (0==type && null!=faceNum && 1==faceNum && isA==true) {
176
+            if (0==type && null!=faceNum && 1==faceNum && isA) {
176 177
                 //校验通过后
177 178
                 String errorCode= (String) imgPush(name, uploadFile);
178 179
                 JSONObject Code = JSONObject.parseObject(errorCode);
@@ -199,7 +200,6 @@ public class FaceServicelimpl implements FaceServicel {
199 200
     }
200 201
     //在图片验证通过的情况下,推送到海康
201 202
     public Object imgPush(String name,MultipartFile uploadFile){
202
-        HKOpenApi hkOpenApi=new HKOpenApi();
203 203
         //把图片压缩成100K以下
204 204
         // 压缩后的图片字节数组
205 205
         byte [] picByte = null;
@@ -212,7 +212,7 @@ public class FaceServicelimpl implements FaceServicel {
212 212
             e.printStackTrace();
213 213
         }
214 214
         // String imgs = hkOpenApi.getImgStr(uploadFile);
215
-        String imgs = hkOpenApi.getImgStr(picByte);
215
+        String imgs = HKOpenApi.getImgStr(picByte);
216 216
         String pgId= (String) HKpersonGroupId();
217 217
         JSONObject jsonObject = JSONObject.parseObject(pgId);
218 218
         Integer personGroupId = (Integer) jsonObject.get("personGroupId");