程式包com.sun.image.codec.jpeg不存在
阿新 • • 發佈:2018-12-29
出現原因:影象處理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 :程式碼更改
可能出現問題的程式碼:
- import java.awt.image.BufferedImage;
- //import com.sun.image.codec.jpeg.JPEGCodec;
- //import com.sun.image.codec.jpeg.JPEGImageEncoder;
- import javax.imageio.ImageIO;
修改後:
- static void saveImage(BufferedImage dstImage, String dstName) throws IOException {
- String formatName = dstName.substring(dstName.lastIndexOf(".") + 1);
- //FileOutputStream out = new FileOutputStream(dstName);
- //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- //encoder.encode(dstImage);
- ImageIO.write(dstImage, /*"GIF"*/ formatName /* format desired */ , new File(dstName) /* target */ );
- }