ImageReader对内存字节流进行图像解码
public static void checkBase64ImageIsNormal(String base64Code) throws Exception {
String path = String.format(SensefaceHistoryConstant.TEMP_FOLDER_PATH,
SensefaceHistoryConstant.SYSTEM_TEMP_PATH, UUIDUtil.getUUID() + ".jpg");
Base64Img.base64ToFile(base64Code, path);
File imageFile = new File(path);
try {
final long imageSize = imageFile.length();
if (imageSize > SenseFaceConstant.FILE_SIZE_MORE_THAN_MAX) {
throw ErrorCode.FILE_SIZE_MORE_THAN_MAX;
}
FileInputStream fileInputStream = new FileInputStream(imageFile);
final ImageInputStream iis = ImageIO.createImageInputStream(fileInputStream);
final Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
final ImageReader reader = iter.next();
final String suffix = reader.getFormatName();
final String[] imageTypes = { "jpeg", "jpg", "png", "bmp" };
final List<String> imageTypeList = Arrays.asList(imageTypes);
if (!imageTypeList.contains(suffix.toLowerCase())) {
throw ErrorCode.FILE_FORMAT_NOT_SUPPORTED;
}
} catch (Exception e) {
// TODO: handle exception
throw e;
} finally {
imageFile.delete();
}
}
public static void checkImageIsNormal(final String imageUrl) throws Exception {
final URL url = new URL(imageUrl);
final URLConnection uc = url.openConnection();
if (uc == null) {
throw ErrorCode.NETWORK_PICTURE_NOT_EXIST;
}
final long imageSize = uc.getContentLength();
if (imageSize > SenseFaceConstant.FILE_SIZE_MORE_THAN_MAX) {
throw ErrorCode.FILE_SIZE_MORE_THAN_MAX;
}
final ImageInputStream iis = ImageIO.createImageInputStream(uc.getInputStream());
final Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
final ImageReader reader = iter.next();
final String suffix = reader.getFormatName();
final String[] imageTypes = { "jpeg", "jpg", "png", "bmp" };
final List<String> imageTypeList = Arrays.asList(imageTypes);
if (!imageTypeList.contains(suffix.toLowerCase())) {
throw ErrorCode.FILE_FORMAT_NOT_SUPPORTED;
}
}