Commit ecf584923aa538890d5969ab6f8d2a3ac910a5d2
1 parent
cb720a7a
ImageUtil
Showing
1 changed file
with
76 additions
and
0 deletions
src/main/java/com/diligrp/xtrade/product/common/utils/ImageUtil.java
0 → 100644
1 | +package com.diligrp.xtrade.product.common.utils; | |
2 | + | |
3 | +import java.awt.image.BufferedImage; | |
4 | +import java.io.IOException; | |
5 | +import java.io.InputStream; | |
6 | +import java.security.MessageDigest; | |
7 | +import java.security.NoSuchAlgorithmException; | |
8 | + | |
9 | +import javax.imageio.ImageIO; | |
10 | + | |
11 | +import com.diligrp.xtrade.product.exception.ExceptionEnum; | |
12 | +import com.diligrp.xtrade.product.exception.ProductException; | |
13 | + | |
14 | + | |
15 | +/** | |
16 | + * | |
17 | + * <B>Description</B> 图片工具类 <br /> | |
18 | + * <B>Copyright</B> Copyright (c) 2015 www.diligrp.com All rights reserved. <br /> | |
19 | + * 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br /> | |
20 | + * <B>Company</B> 地利集团 | |
21 | + * @createTime 2015年4月24日 上午11:30:36 | |
22 | + * @author celine | |
23 | + */ | |
24 | +public class ImageUtil { | |
25 | + | |
26 | + /** | |
27 | + * 检测文件是否是图片 | |
28 | + * @param inputStream | |
29 | + * @return | |
30 | + * @createTime 2015年4月24日 上午11:35:29 | |
31 | + * @author celine | |
32 | + */ | |
33 | + public static Boolean isImage(InputStream inputStream) { | |
34 | + try { | |
35 | + BufferedImage bi = ImageIO.read(inputStream); | |
36 | + return null == bi ? false : true; | |
37 | + } catch (IOException e) { | |
38 | + throw new ProductException(ExceptionEnum.FILE_UP_ERROR); | |
39 | + } | |
40 | + } | |
41 | + | |
42 | + public static Boolean checkImageSize(InputStream inputStream, int maxWidth, int maxHeight, int minWidth, | |
43 | + int minHeight) { | |
44 | + try { | |
45 | + BufferedImage bi = ImageIO.read(inputStream); | |
46 | + if (bi != null) { | |
47 | + if (bi.getWidth() <= maxWidth && bi.getHeight() <= maxHeight && bi.getWidth() >= minWidth | |
48 | + && bi.getHeight() >= minHeight) { | |
49 | + return true; | |
50 | + } | |
51 | + } | |
52 | + return false; | |
53 | + } catch (IOException e) { | |
54 | + throw new ProductException(ExceptionEnum.FILE_UP_ERROR); | |
55 | + } | |
56 | + } | |
57 | + | |
58 | + public static String getMd5(byte[] imgBytes) { | |
59 | + if (imgBytes != null && imgBytes.length > 0) { | |
60 | + StringBuilder sb = new StringBuilder(); | |
61 | + MessageDigest md5; | |
62 | + try { | |
63 | + md5 = MessageDigest.getInstance("MD5"); | |
64 | + } catch (NoSuchAlgorithmException e) { | |
65 | + throw new ProductException(ExceptionEnum.FILE_UP_ERROR); | |
66 | + } | |
67 | + md5.update(imgBytes); | |
68 | + for (byte by : md5.digest()) { | |
69 | + sb.append(String.format("%02X", by)); | |
70 | + } | |
71 | + return sb.toString(); | |
72 | + } | |
73 | + return ""; | |
74 | + } | |
75 | + | |
76 | +} | ... | ... |