Maven packaging of springboot project: package com.sun.image.codec.jpeg does not exist

Maven packaging of springboot project: package com.sun.image.codec.jpeg does not exist


Error message:

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

Insert picture description here
The reason is (JPEGCodec class) Removed after JDK1.7, an error will be reported when packaging with JDK1.8

Code:

	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;

Solution: Replace 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] ------------------------------------------------------------------------

Image compression complete code
image rotation

Guess you like

Origin blog.csdn.net/qq_42476834/article/details/107998256