当前位置: 首页 > ops >正文

Java中的ImageIo支持webp解析

Java 的 ImageIO 默认不支持 WebP 格式。要读取 WebP 格式的图片,我们需要添加额外的依赖和配置

<dependency><groupId>org.sejda.webp-imageio</groupId><artifactId>webp-imageio-sejda</artifactId><version>0.1.0</version>
</dependency>

我们下面做一个 webp信息获取

 @Data@Builderpublic static class ImageInfo{private Integer width;private Integer height;/*** 字节*/private Long size;/*** type  例如 "png"*/private String type;/*** 处理的视频*/private File file;private byte[] imageByte;public static ImageInfo withWidthHeight(Integer width, Integer height){return ImageInfo.builder().width(width).height(height).build();}public static ImageInfo withSize(Long size){return ImageInfo.builder().size(size).build();}public static ImageInfo withType(String type){return ImageInfo.builder().type(type).build();}public static ImageInfo withWidthHeightTypeSize(Integer width, Integer height,String type,Long size){return ImageInfo.builder().width(width).height(height).size(size).type(type).build();}public static ImageInfo withAll(Integer width,Integer height,String type,Long size,byte[] content){return ImageInfo.builder().width(width).height(height).type(type).imageByte(content).size(size).build();}public ImageInfo file(File file){this.file = file;return this;}public ImageInfo imageByte(byte[] imageByte){this.imageByte = imageByte;return this;}}
public static ImageInfo getImageDetail(String strUrl) {int retryCount = 0;int maxRetries = 2;while (retryCount < maxRetries) {try {URL url = new URL(strUrl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(10 * 1000);try (InputStream inStream = conn.getInputStream()) {// 获取图片字节数组byte[] imageBytes = StreamUtils.copyToByteArray(inStream);// 获取图片尺寸信息BufferedImage sourceImg = ImageIO.read(new ByteArrayInputStream(imageBytes));if (sourceImg == null) {throw new IOException("Failed to read image");}// 获取图片格式String format = getImageFormat(imageBytes);return ImageInfo.withAll(sourceImg.getWidth(),sourceImg.getHeight(),format,(long)imageBytes.length,imageBytes);}} catch (Exception e) {retryCount++;if (retryCount >= maxRetries) {throw new RuntimeException("Failed to get image detail after " + maxRetries + " retries", e);}log.warn("Failed to get image from url: {}, retrying... (attempt {}/{})",strUrl, retryCount, maxRetries);}}throw new RuntimeException("Failed to get image detail");}
http://www.xdnf.cn/news/7671.html

相关文章:

  • 小白成长之路-Linux磁盘管理(一)
  • 如何管理和优化内核参数
  • [IMX] 07.LCD 显示
  • 【高斯函数】
  • 驱动相关基础
  • leetcode刷题日记——从前序与中序遍历序列构造二叉树
  • MES管理系统电子看板驱动企业智能制造
  • python Numpy-数组
  • 探索nsupdate:动态DNS更新的终极指南
  • 码钉枪行业2025数据分析报告
  • Java程序员从0学AI(二)
  • 使用F5-tts复刻音色
  • ArrayList源码分析
  • 实现商品列表
  • 建站系统哪个好?
  • 基于CATIA参数化圆锥建模的自动化插件开发实践——NX建模之圆锥体命令的参考与移植(二)
  • 笔记:显示实现接口如何实现,作用是什么
  • ollama部署模型
  • 工单派单应用:5 大核心功能提升协作效率
  • Ai学习之LangChain框架
  • STM32外设应用详解——从基础到高级应用的全面指南
  • 差分数组:原理与应用
  • 文献分享-临床预测模型-基于围手术期时间数据肝切除术后肝衰竭早期检测
  • CSS 背景全解析:从基础属性到视觉魔法
  • MinIO集群故障,其中一块driver-4异常
  • 网络安全之带正常数字签名的后门样本分析
  • 软件测试之环境搭建及测试流程
  • 见多识广10:大模型的一些基础概念
  • Python训练营打卡——DAY31(2025.5.20)
  • 类和对象------2