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

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");}

相关文章:

  • java中的Filter使用详解
  • Leetcode 01 java
  • 预先学习:构建智能系统的 “未雨绸缪” 之道
  • 杰发科技AC7840——如何把结构体数据写到Dflash中
  • 【NLP 76、Faiss 向量数据库】
  • 珈和科技贺李德仁院士荣膺国际数字地球学会会士:以时空智能赋能可持续发展目标 绘就数字地球未来蓝图
  • 【神经网络与深度学习】扩散模型之通俗易懂的解释
  • HarmonyOS5云服务技术分享--ArkTS调用函数
  • 4-5月份,思科,华为,微软,个别考试战报分享
  • 鸿蒙PC新物种发布!华为MateBook Pro/ Fold深度解析:折叠屏革命与生态破局
  • 一文掌握vue3基础,适合自学入门案例丰富
  • A1-A2 英语学习系列 第四集 中国版
  • MYSQL备份恢复知识:第四章:备份锁
  • 关于汇编语言与接口技术——单片机串行口的学习心得
  • 【综述】视频目标分割VOS
  • 视频监控联网系统GB28181协议中互联结构详解
  • 零基础入门Selenium自动化测试:自动登录edu邮箱
  • python宠物用品商城系统
  • web开发全过程总结
  • php、laravel框架下如何将一个png图片转化为jpg格式
  • 上海合作组织减贫和可持续发展论坛开幕,沈跃跃宣读习近平主席贺信
  • 澎湃读报丨多家央媒刊文关注拧紧纪律的螺丝:强化监督推动过紧日子要求落到实处
  • 黄仁勋的新逻辑:从“卖铲人”到“全球AI基建运营商”
  • 特朗普与普京开始进行电话会谈,稍后还将致电泽连斯基
  • 美俄亥俄州北部发生火车撞人事故,致2人死亡、至少1人失踪
  • 坐标大零号湾科创策源区,上海瑞金医院闵行院区正式启动建设