1. 程式人生 > >程式包com.sun.image.codec.jpeg不存在

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

出現原因:影象處理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. }