thumbnailator压缩图片并存至excel单元格代码 -ag凯发k8国际
ag凯发k8国际
收集整理的这篇文章主要介绍了
thumbnailator压缩图片并存至excel单元格代码
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
文章目录
- 依赖
- 压缩图片工具类:
- 存至excel的转化器
- excel导入导出的数据类
- 将数据库实体转化为excel实体
@description: 调用thumbnails压缩图片
public class imgutil {private static logger logger = loggerfactory.getlogger(imgutil.class);/*** 根据指定大小压缩图片** @param imagebytes 源图片字节数组* @param desfilesize 指定图片大小,单位kb* @param imageid 影像编号* @return 压缩质量后的图片字节数组*/public static byte[] compresspicforscale(byte[] imagebytes, long desfilesize, string imageid) {if (imagebytes == null || imagebytes.length <= 0 || imagebytes.length < desfilesize * 1024) {return imagebytes;}long srcsize = imagebytes.length;//double accuracy = getaccuracy(srcsize / 1024);double accuracy=0.4;try {while (imagebytes.length > desfilesize * 1024) {bytearrayinputstream inputstream = new bytearrayinputstream(imagebytes);bytearrayoutputstream outputstream = new bytearrayoutputstream(imagebytes.length);//thumbnails用来压缩图片的工具thumbnails.of(inputstream).scale(accuracy).outputquality(accuracy).tooutputstream(outputstream);imagebytes = outputstream.tobytearray();}logger.info("【图片压缩】imageid={} | 图片原大小={}kb | 压缩后大小={}kb",imageid, srcsize / 1024, imagebytes.length / 1024);} catch (exception e) {logger.error("【图片压缩】msg=图片压缩失败!", e);}return imagebytes;}/*** 自动调节精度(经验数值)** @param size 源图片大小* @return 图片压缩质量比*/private static double getaccuracy(long size) {double accuracy;if (size < 900) {accuracy = 0.5;} else if (size < 2047) {accuracy = 0.6;} else if (size < 3275) {accuracy = 0.44;} else {accuracy = 0.4;}return accuracy;}} /*** @author hai* @program: subtlechat* @description: 将url图片的格式转化器* @create 2020/10/8 - 12:45**/ public class myurlimageconverter implements converter<url> {@overridepublic class supportjavatypekey() {return url.class;}@overridepublic celldatatypeenum supportexceltypekey() {return celldatatypeenum.image;}@overridepublic url converttojavadata(celldata celldata, excelcontentproperty contentproperty,globalconfiguration globalconfiguration) {throw new unsupportedoperationexception("cannot convert images to url.");}@overridepublic celldata converttoexceldata(url value, excelcontentproperty contentproperty,globalconfiguration globalconfiguration) throws ioexception {inputstream inputstream = null;try {//开启连接urlconnection uc = value.openconnection();//openconnection:返回一个urlconnection实例,该实例表示到url引用的远程对象的连接。url url = null;//获取响应状态int statuscode = ((httpurlconnection) uc).getresponsecode();switch (statuscode){case 200:inputstream = value.openstream();break;case 404://默认给一个图片url = new url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598096095144&di=9a72ad26e83effb9341c711c9818b85f&imgtype=0&src=http://pic.616pic.com/ys_bnew_img/00/11/69/j2ajnhspwt.jpg");inputstream = url.openstream();break;default :url = new url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598096095144&di=9a72ad26e83effb9341c711c9818b85f&imgtype=0&src=http://pic.616pic.com/ys_bnew_img/00/11/69/j2ajnhspwt.jpg");inputstream = url.openstream();break;}byte[] bytes = ioutils.tobytearray(inputstream);byte[] compressbytes = imgutil.compresspicforscale(bytes,200, uuid.randomuuid().tostring());return new celldata(compressbytes);}catch (connectexception exception){//捕获下链接异常url url = new url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598096095144&di=9a72ad26e83effb9341c711c9818b85f&imgtype=0&src=http://pic.616pic.com/ys_bnew_img/00/11/69/j2ajnhspwt.jpg");inputstream = url.openstream();byte[] bytes = ioutils.tobytearray(inputstream);return new celldata(bytes);}catch (filenotfoundexception filenotfoundexception){url url = new url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598096095144&di=9a72ad26e83effb9341c711c9818b85f&imgtype=0&src=http://pic.616pic.com/ys_bnew_img/00/11/69/j2ajnhspwt.jpg");inputstream = url.openstream();byte[] bytes = ioutils.tobytearray(inputstream);return new celldata(bytes);}finally {if (inputstream != null) {inputstream.close();}}} } @data @columnwidth(25) @contentrowheight(30) public class groupmsgcontentdata {@excelproperty("消息内容编号")private integer id;@excelproperty("发送者的编号")private integer fromid;@excelproperty("昵称")private string fromname;//@excelproperty(value = "头像",converter = urlimageconverter.class)@excelignoreprivate url fromprofile;@excelproperty("发送时间")@jsonformat(pattern = "yyyy-mm-dd hh:mm:ss",timezone = "gmt 8")private date createtime;@excelproperty(value = {"内容","文本"})@columnwidth(50)private string textcontent;@excelproperty(value = {"内容","图片"},converter = myurlimageconverter.class)@columnwidth(50)private url imagecontent;@excelignoreprivate integer messagetypeid;@overridepublic string tostring() {return "groupmsgcontentdata{" "id=" id ", fromid=" fromid ", fromname='" fromname '\'' ", fromprofile=" fromprofile ", createtime=" createtime ", textcontent='" textcontent '\'' ", imagecontent=" imagecontent ", messagetypeid=" messagetypeid '}';}注意保存图片的这一数据是:
@excelproperty(value = {“内容”,“图片”},converter = myurlimageconverter.class)
@columnwidth(50)
private url imagecontent;
总结
以上是ag凯发k8国际为你收集整理的thumbnailator压缩图片并存至excel单元格代码的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: