java图片处理-twelvemonkeys

java图片处理 - TwelveMonkeys ImageIO

javax.imageio原生支持的格式很少,特殊格式使用 ImageIO.read 获取的为null

可以用getWriterFileSuffixes()查询支持格式

String[] writerFileSuffixes = ImageIO.getWriterFileSuffixes();

原生支持格式

jpg,bmp,gif,png,jpeg,wbmp

TwelveMonkeys ImageIO 是对于 javax.imageio.*包 扩展的插件,这个插件配置后可以添加JRE本身没有覆盖的格式

中文翻译地址

文件格式支持列表

表格来自 TwelveMonkeys 文档

插件格式简介元数据文档
BatikSVGScalable Vector Graphics--Requires Batik
WMFMS Windows Metafile--Requires Batik
BMPBMPMS Windows and IBM OS/2 Device Independent BitmapNative, Standard
CURMS Windows Cursor Format--
ICOMS Windows Icon Format-
HDRHDR辐射高动态范围RGBE格式-Standard
ICNSICNS苹果图标图像-
IFFIFFCommodore Amiga/Electronic Arts Interchange File FormatStandard
JPEGJPEGJoint Photographers Expert GroupNative, Standard
JPEG Lossless-Native, Standard
PCXPCXZSoft Paintbrush Format-Standard
DCXMulti-page PCX fax document-Standard
PICTPICTApple QuickTime Picture FormatStandard
PNTGApple MacPaint Picture Format-Standard
PNMPAMNetPBM Portable Any MapStandard
PBMNetPBM Portable Bit Map-Standard
PGMNetPBM Portable Grey Map-Standard
PPMNetPBM Portable Pix MapStandard
PFMPortable Float Map-Standard
PSDPSDAdobe Photoshop Document(✔)Native, Standard
PSBAdobe Photoshop Large Document-Native, Standard
SGISGISilicon Graphics Image Format-Standard
TGATGATruevision TGA图像格式Standard
ThumbsDBThumbs.dbMS Windows Thumbs DB--OLE2 Compound Document based format only
TIFFTIFFAldus/Adobe Tagged Image File FormatNative, Standard
BigTIFFNative, Standard
WebPWebPGoogle WebP Format-Standard
XWDXWDX11 Window Dump Format-Standard

引入该插件后支持的格式

jpg,tpic,tiff,bmp,gif,tf8,png,btiff,tga,tif,btf,jpeg,wbmp

maven依赖

 <dependency>
            <groupId>com.twelvemonkeys.imageio</groupId>
            <artifactId>imageio-jpeg</artifactId>
            <version>3.9.4</version>
        </dependency>
        <dependency>
            <groupId>com.twelvemonkeys.imageio</groupId>
            <artifactId>imageio-tiff</artifactId>
            <version>3.9.4</version>
        </dependency>
        <dependency>
            <groupId>com.twelvemonkeys.servlet</groupId>
            <artifactId>servlet</artifactId>
            <version>3.9.4</version>
        </dependency>
        <dependency>
            <groupId>com.twelvemonkeys.servlet</groupId>
            <artifactId>servlet</artifactId>
            <version>3.9.4</version>
            <classifier>jakarta</classifier>
        </dependency>

tga图片等特殊格式需要额外添加本地依赖

<dependency>
            <groupId>javax.imageio</groupId>
            <artifactId>tga</artifactId>
            <version>3.9.4</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jar/imageio-tga-3.9.4.jar</systemPath>
        </dependency>

tga图片转jpg

try {
            BufferedImage bufferegImage = ImageIO.read(new File("E:\\input.tga"));
            ImageIO.write(bufferegImage, "jpg", new File("E:output.jpg"));
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

ImageIO插件

可以通过添加插件,增加imageio支持的文件格式

<think>好的,我现在需要帮用户解决如何用Java处理MultipartFile类型的图片压缩的问题。首先,我得回想一下Java中处理图片压缩的常用方法。记得Java自带的ImageIO类可以处理图片的读写,但可能功能有限。对于更高效的压缩,可能需要用到第三方库,比如Thumbnailator,这个库简化了图片处理的流程,应该适合用户的需求。 用户提到的是MultipartFile类型,这是Spring框架中处理文件上传的类,所以需要先将MultipartFile转换为Java可以处理的图像格式。首先,可能需要通过ImageIO读取MultipartFile的输入流,转换成BufferedImage对象。但要注意,ImageIO支持的格式有限,比如不支持WebP,这时候可能需要用其他库,比如TwelveMonkeysImageIO插件来扩展支持。 然后,压缩图片通常涉及调整尺寸和质量。Thumbnailator库可以很方便地调整尺寸和质量参数,比如使用scale()方法按比例缩放,或者width()和height()指定具体尺寸。同时,输出时设置输出质量,使用JPEG格式并指定压缩质量参数,这样可以有效减少文件大小。 另外,用户可能需要将压缩后的图片保存回MultipartFile或者字节数组,以便后续处理或存储。这里可能需要使用ByteArrayOutputStream将压缩后的图片写入字节数组,然后再转换为MultipartFile,但需要注意Spring的MockMultipartFile类通常用于测试,实际应用中可能需要其他方式处理文件保存。 还要考虑异常处理,比如图片格式不支持,或者读取输入流时的IO异常。此外,性能方面,压缩参数的选择会影响处理时间和图片质量,需要根据实际需求平衡。如果用户需要更高的压缩率或更好的质量保持,可能需要更复杂的算法,比如使用双三次插值法调整尺寸,或者选择不同的压缩格式。 最后,记得提供示例代码,让用户能够直观地看到如何实现。同时,根据用户的需求生成相关问题,比如性能优化、不同格式的处理或者如何保持宽高比,这样可以帮助用户深入理解相关技术点。</think>### Java处理MultipartFile图片压缩的实践方法 #### 1. 核心实现步骤 **1.1 读取图片数据** 通过`ImageIO.read()`将`MultipartFile`转换为`BufferedImage`对象: ```java BufferedImage originalImage = ImageIO.read(multipartFile.getInputStream()); ``` **1.2 压缩策略选择** - **尺寸压缩**:通过`java.awt.Graphics2D`调整图片分辨率 ```java BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = resizedImage.createGraphics(); graphics.drawImage(originalImage, 0, 0, newWidth, newHeight, null); ``` - **质量压缩**:使用JPEG压缩参数(推荐Thumbnailator库简化操作) ```java Thumbnails.of(originalImage) .scale(0.5) // 缩放比例 .outputQuality(0.7) // 质量系数(0.0-1.0) .toOutputStream(outputStream); ``` **1.3 格式转换处理** 针对WebP等新格式,需添加依赖项: ```xml <dependency> <groupId>com.twelvemonkeys.imageio</groupId> <artifactId>imageio-webp</artifactId> <version>3.8.0</version> </dependency> ``` #### 2. 完整代码示例 ```java public MultipartFile compressImage(MultipartFile file, float quality) throws IOException { // 读取原始图片 BufferedImage image = ImageIO.read(file.getInputStream()); // 创建压缩后的字节流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); Thumbnails.of(image) .scale(1) .outputFormat("jpg") .outputQuality(quality) .toOutputStream(baos); // 转换为MultipartFile return new MockMultipartFile( file.getName(), file.getOriginalFilename(), "image/jpeg", baos.toByteArray() ); } ``` #### 3. 关键注意事项 - **格式兼容性**:`ImageIO`默认支持JPEG/PNG,需扩展库处理WebP[^1] - **质量平衡**:建议质量系数保持在0.6-0.8之间(文件大小与清晰度的最佳平衡点) - **尺寸计算**:推荐保持原始宽高比,避免图像变形 - **内存管理**:大文件处理时应使用`ImageInputStream`进行流式处理[^3] #### 4. 性能优化建议 - 使用`java.util.concurrent`实现并行压缩(针对批量处理场景)[^3] - 采用缓存机制存储常用尺寸模板 - 结合`BufferedImage.getSubimage()`实现局部区域压缩
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值