java - 检查是否有旋转角度导致ImageIO获取宽高相反
导入依赖
<dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extractor</artifactId> <version>2.15.0</version> </dependency>
获取图片宽高
public static ImgWH getImgWH(File file, String url) { if (null == file) { if (StringUtils.isEmpty(url)) return null; file = new File(url); } if (!file.exists()) return null; ImgWH wh = new ImgWH(); try { Image img = ImageIO.read(file); wh.setWidth(img.getWidth(null)); wh.setHeight(img.getHeight(null)); //检查是否有旋转角度导致ImageIO获取宽高相反 Metadata metadata = ImageMetadataReader.readMetadata(file); StringBuilder description = new StringBuilder(); metadata.getDirectories().forEach(directory -> { directory.getTags().forEach(tag -> { if (tag.getTagType() == ExifDirectoryBase.TAG_ORIENTATION) { description.append(tag.getDescription().replaceAll(" ", "")); } }); }); if (description.length() > 0) { int rotateIndex = description.indexOf("Rotate"); int cwIndex = description.indexOf("CW"); if (rotateIndex >= 0 && cwIndex > 0 && rotateIndex < cwIndex) { int angel = Integer.parseInt(description.substring(rotateIndex + 6, cwIndex)); if (angel == 90 || angel == 180) { //宽高互换 int t = wh.getHeight(); wh.setHeight(wh.getWidth()); wh.setWidth(t); } } } } catch (Exception e) { e.printStackTrace(); return null; } return wh; }
参考博文地址
https://www.bbsmax.com/A/E35p0je85v/
本文来自博客园,作者:岑惜,转载请注明原文链接:https://www.cnblogs.com/c2g5201314/p/17336425.html
响应开源精神相互学习,内容良币驱除劣币
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-04-20 目前可用svip账户下载