java 判断图片类型
说明:如果根据后缀名来判断文件的类型的话是不靠谱的,因为后缀名一旦被手动重命名之后,是无法判断的,所以需要根据文件的编码方式或者文件头信息来判断,因为这些是无法手动改变的。
方法一 根据编码方式获取图片的类型:
1 package com.test01;2
3 import javax.imageio.ImageIO;
4 import javax.imageio.ImageReader;
5 import javax.imageio.stream.ImageInputStream;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.Iterator;
11 import java.util.List;
12
13 /**
14 * @Description: Test02
15 * @Author: luzhiming
16 * @CreateDate: 2021/9/13 10:28
17 * @Version: 1.0
18 */
19 public class GetImageTypeTest {
20
21 public static void main(String[] args) throws IOException {
22 String rootPath = System.getProperty("user.dir");
23 List<String> list = new ArrayList<>();
24 list.add(rootPath + "/image-demo/file/input/1.jpg");
25 // 修改过后缀名 jiang webp 修改为 jpg
26 list.add(rootPath + "/image-demo/file/input/2.jpg");
27 list.add(rootPath + "/image-demo/file/input/1111.png");
28 list.add(rootPath + "/image-demo/file/input/100.webp");
29 list.forEach(filename -> {
30 try {
31 getImageType(filename);
32 } catch (IOException e) {
33 e.printStackTrace();
34 }
35 });
36 }
37
38 public static void getImageType(String filename) throws IOException {
39 File file = new File(filename);
40 ImageInputStream image = ImageIO.createImageInputStream(new FileInputStream(file));
41 Iterator<ImageReader> readers = ImageIO.getImageReaders(image);
42 String formatName = readers.next().getFormatName();
43 System.out.println(formatName);
44 }
45 }
控制台输出:
JPEGWebP
JPEG
WebP
方法二 将图片的信息读成流转16进制,根据文件流判断文件类型(推荐使用)
1 package com.test01;2
3 import java.io.*;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 public class ImageTypeUtils {
8 /**
9 * 常用文件的文件头如下:(以前六位为准)
10 * JPEG (jpg),文件头:FFD8FF
11 * PNG (png),文件头:89504E47
12 * GIF (gif),文件头:47494638
13 * TIFF (tif),文件头:49492A00
14 * Windows Bitmap (bmp),文件头:424D
15 * CAD (dwg),文件头:41433130
16 * Adobe Photoshop (psd),文件头:38425053
17 * Rich Text Format (rtf),文件头:7B5C727466
18 * XML (xml),文件头:3C3F786D6C
19 * HTML (html),文件头:68746D6C3E
20 * Email [thorough only] (eml),文件头:44656C69766572792D646174653A
21 * Outlook Express (dbx),文件头:CFAD12FEC5FD746F
22 * Outlook (pst),文件头:2142444E
23 * MS Word/Excel (xls.or.doc),文件头:D0CF11E0
24 * MS Access (mdb),文件头:5374616E64617264204A
25 * WordPerfect (wpd),文件头:FF575043
26 * Postscript (eps.or.ps),文件头:252150532D41646F6265
27 * Adobe Acrobat (pdf),文件头:255044462D312E
28 * Quicken (qdf),文件头:AC9EBD8F
29 * Windows Password (pwl),文件头:E3828596
30 * ZIP Archive (zip),文件头:504B0304
31 * RAR Archive (rar),文件头:52617221
32 * Wave (wav),文件头:57415645
33 * AVI (avi),文件头:41564920
34 * Real Audio (ram),文件头:2E7261FD
35 * Real Media (rm),文件头:2E524D46
36 * MPEG (mpg),文件头:000001BA
37 * MPEG (mpg),文件头:000001B3
38 * Quicktime (mov),文件头:6D6F6F76
39 * Windows Media (asf),文件头:3026B2758E66CF11
40 * MIDI (mid),文件头:4D546864
41 */
42
43
44 public static final String TYPE_JPG = ".jpg";
45 public static final String TYPE_GIF = ".gif";
46 public static final String TYPE_PNG = ".png";
47 public static final String TYPE_BMP = ".bmp";
48 public static final String TYPE_WEBP = ".webp";
49 public static final String TYPE_TIF = ".tif";
50 public static final String TYPE_UNKNOWN = "unknown";
51
52
53 public static void main(String[] args) throws FileNotFoundException {
54 String rootPath = System.getProperty("user.dir");
55 List<String> list = new ArrayList<>();
56 list.add(rootPath + "/image-demo/file/input/1.jpg");
57 // 修改过后缀名 jiang webp 修改为 jpg
58 list.add(rootPath + "/image-demo/file/input/2.jpg");
59 list.add(rootPath + "/image-demo/file/input/1111.png");
60 list.add(rootPath + "/image-demo/file/input/100.webp");
61 list.forEach(filename -> {
62 try {
63 File pdfFile = new File(filename);
64 // test code
65 System.out.println("图片格式:" + getPicType(new FileInputStream(pdfFile)));
66 } catch (IOException e) {
67 e.printStackTrace();
68 }
69 });
70 }
71
72 /**
73 * byte数组转换成16进制字符串
74 *
75 * @param src
76 * @return
77 */
78 public static String bytesToHexString(byte[] src) {
79 StringBuilder stringBuilder = new StringBuilder();
80 if (src == null || src.length <= 0) {
81 return null;
82 }
83 for (int i = 0; i < src.length; i++) {
84 int v = src[i] & 0xFF;
85 String hv = Integer.toHexString(v);
86 if (hv.length() < 2) {
87 stringBuilder.append(0);
88 }
89 stringBuilder.append(hv);
90 }
91 return stringBuilder.toString();
92 }
93
94 /**
95 * 根据文件流判断图片类型
96 *
97 * @param fis
98 * @return jpg/png/gif/bmp
99 */
100 public static String getPicType(FileInputStream fis) {
101 //读取文件的前几个字节来判断图片格式
102 byte[] b = new byte[4];
103 try {
104 fis.read(b, 0, b.length);
105 String type = bytesToHexString(b).toUpperCase();
106 if (type.contains("FFD8FF")) {
107 return TYPE_JPG;
108 } else if (type.contains("89504E47")) {
109 return TYPE_PNG;
110 } else if (type.contains("47494638")) {
111 return TYPE_GIF;
112 } else if (type.contains("424D")) {
113 return TYPE_BMP;
114 } else if (type.contains("52494646")) {
115 return TYPE_WEBP;
116 } else if (type.contains("49492A00")) {
117 return TYPE_TIF;
118 } else {
119 return TYPE_JPG;
120 }
121 } catch (IOException e) {
122 e.printStackTrace();
123 } finally {
124 if (fis != null) {
125 try {
126 fis.close();
127 } catch (IOException e) {
128 e.printStackTrace();
129 }
130 }
131 }
132 return null;
133 }
134
135 /**
136 * 根据文件流判断图片类型
137 *
138 * @param fis
139 * @return jpg/png/gif/bmp
140 */
141 public static String getPicType2(InputStream fis) {
142 //读取文件的前几个字节来判断图片格式
143 byte[] b = new byte[4];
144 try {
145 fis.read(b, 0, b.length);
146 String type = bytesToHexString(b).toUpperCase();
147 if (type.contains("FFD8FF")) {
148 return TYPE_JPG;
149 } else if (type.contains("89504E47")) {
150 return TYPE_PNG;
151 } else if (type.contains("47494638")) {
152 return TYPE_GIF;
153 } else if (type.contains("424D")) {
154 return TYPE_BMP;
155 } else if (type.contains("52494646")) {
156 return TYPE_WEBP;
157 } else if (type.contains("49492A00")) {
158 return TYPE_TIF;
159 } else {
160 return null;
161 }
162 } catch (IOException e) {
163 e.printStackTrace();
164 } finally {
165 if (fis != null) {
166 try {
167 fis.close();
168 } catch (IOException e) {
169 e.printStackTrace();
170 }
171 }
172 }
173 return TYPE_UNKNOWN;
174 }
175 }
控制台输出:
1 图片格式:.jpg2 图片格式:.webp
3 图片格式:.jpg
4 图片格式:.webp
以上是 java 判断图片类型 的全部内容, 来源链接: utcz.com/z/394711.html