前言
最近在加载图片时,由于需要对不同图片使用不同的加载方式,因此需要通过判断图片的类型进行条用不同的接口。
因此整理如下内容。
常见的图片格式头文件代码
PS:如下是十六进制
- JPEG (jpg) 文件头:FFD8FF
- PNG (png) 文件头:
89504E47(打印png的值是:89506082) - GIF (gif) 文件头:47494638
- Windows Bitmap (bmp) 文件头:424D
- TIFF (tif) 文件头:49492A00
总结
- gif,jpg,bmp测试通过
- tiff由于没有图片,没有测试
- png的判断存在问题,上面也备注了,打印值跟89 50 4E 47 不一样
正文
下面是测试的代码片段。
PS:代码中判断的是十进制,需要转换一下
gif 格式
/** * gif (十六进制 : 47 49 46 38) * * @param file * @return */ public static boolean isGifFile(File file) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); int[] buffer = new int[5]; buffer[0] = fileInputStream.read(); buffer[1] = fileInputStream.read(); buffer[2] = fileInputStream.read(); buffer[3] = fileInputStream.read(); fileInputStream.skip(fileInputStream.available() - 1); buffer[4] = fileInputStream.read(); return buffer[0] == 71 && buffer[1] == 73 && buffer[2] == 70 && buffer[3] == 56; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != fileInputStream) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
jpg 格式
/** * jpg (十六进制 : FF D8 FF) * * @param file * @return */ public static boolean isJpgFile(File file) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); int buffer[] = new int[4]; buffer[0] = fileInputStream.read(); buffer[1] = fileInputStream.read(); fileInputStream.skip(fileInputStream.available() - 2); buffer[2] = fileInputStream.read(); buffer[3] = fileInputStream.read(); return buffer[0] == 255 && buffer[1] == 216 && buffer[2] == 255; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally { if (null != fileInputStream) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
bmp格式
/** * bmp (十六进制 : 42 4D) * * @param file * @return */ public static boolean isBmpFile(File file) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); int buffer[] = new int[4]; buffer[0] = fileInputStream.read(); buffer[1] = fileInputStream.read(); fileInputStream.skip(fileInputStream.available() - 2); buffer[2] = fileInputStream.read(); buffer[3] = fileInputStream.read(); return buffer[0] == 66 && buffer[1] == 77; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally { if (null != fileInputStream) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
tiff格式
/** * tiff (十六进制 : 49 49 2A 00) * * @param file * @return */ public static boolean isTiffFile(File file) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); int buffer[] = new int[4]; buffer[0] = fileInputStream.read(); buffer[1] = fileInputStream.read(); fileInputStream.skip(fileInputStream.available() - 2); buffer[2] = fileInputStream.read(); buffer[3] = fileInputStream.read(); return buffer[0] == 73 && buffer[1] == 73 && buffer[2] == 42 && buffer[2] == 00; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally { if (null != fileInputStream) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
png格式
参考网上的值,这个png测试不通过,打印对应的值是 89506082 (十六进制)
/** * png (十六进制 : 89 50 4E 47) * * @param file * @return */ public static boolean isPngFile(File file) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); int buffer[] = new int[4]; buffer[0] = fileInputStream.read(); buffer[1] = fileInputStream.read(); fileInputStream.skip(fileInputStream.available() - 2); buffer[2] = fileInputStream.read(); buffer[3] = fileInputStream.read(); return buffer[0] == 137 && buffer[1] == 80 && buffer[2] == 78 && buffer[3] == 71; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } if (null != fileInputStream) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return false; }
参考文章
© 版权声明