springboot项目maven打包:程序包com.sun.image.codec.jpeg不存在

springboot项目maven打包:程序包com.sun.image.codec.jpeg不存在


报错信息:

Error:(3,32) java: 程序包com.sun.image.codec.jpeg不存在

在这里插入图片描述
该原因是(JPEGCodec类)在JDK1.7之后移除,使用 JDK1.8 打包时会报错

代码:

	import com.sun.image.codec.jpeg.JPEGCodec;
	import com.sun.image.codec.jpeg.JPEGImageEncoder;
------------------------------------------------------------
/* bf为处理后的image,jpeg为图片编码格式,out为输出流 */
	BufferedImage bf = new BufferedImage(new_wi, new_he, BufferedImage.TYPE_INT_RGB);
    bf.getGraphics().drawImage(bufImg, 0, 0, new_wi, new_he, null);
    
 	ByteArrayOutputStream out = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    /*近JPEG编码*/
    encoder.encode(bf);
    byte[] re = out.toByteArray();
    logger.info("【图片剪切】| 图片原大小={}kb | 压缩后大小={}kb", (data.length / 1024), (re.length / 1024));
    return re;

解决办法: 代替掉 JPEGCodec

/* bf为处理后的image,jpeg为图片编码格式,out为输出流 */
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bf,"jpeg",out); //以这个代替掉 JPEGCodec
byte[] re = out.toByteArray();
********************************略*********
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.035 s
[INFO] Finished at: 2020-08-14T10:29:21+08:00
[INFO] ------------------------------------------------------------------------

图片压缩完整代码类
图片旋转

猜你喜欢

转载自blog.csdn.net/qq_42476834/article/details/107998256