程序包com.sun.image.codec.jpeg不存在

本文介绍了解决JDK1.7之后由于JPEGCodec类被移除导致的问题。提供了两种解决方案:一是通过配置maven-compiler-plugin插件;二是更改代码使用ImageIO替代JPEGCodec。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出现原因:图像处理JPEGCodec类已经从Jdk1.7以后移除。

解决方法1:通过配置maven-compiler-plugin插件可以解决此问题。

插件中添加这个配置

    <plugin> 

               <artifactId>maven-compiler-plugin</artifactId> 

               <version>2.5.1</version> 

               <configuration> 

                    <source>1.7</source> 

                   <target>1.7</target> 

                   <encoding>UTF-8</encoding>

                   <compilerArguments> 

                        <verbose/> 

                       <bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath> 

                   </compilerArguments> 

               </configuration> 

            </plugin> 

解决方法2 :代码更改

可能出现问题的代码:

  1. import java.awt.image.BufferedImage;  
  2. //import com.sun.image.codec.jpeg.JPEGCodec;  
  3. //import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  4. import javax.imageio.ImageIO; 

 

修改后:

  1. static void saveImage(BufferedImage dstImage, String dstName) throws IOException {  
  2.     String formatName = dstName.substring(dstName.lastIndexOf(".") + 1);  
  3.     //FileOutputStream out = new FileOutputStream(dstName);  
  4.     //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  5.     //encoder.encode(dstImage);  
  6.     ImageIO.write(dstImage, /*"GIF"*/ formatName /* format desired */ , new File(dstName) /* target */ );  
  7. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值