分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

工作总结02(海报上传模块)

发布时间:2023-09-06 02:16责任编辑:傅花花关键词:暂无标签

1.海报显示的速度比较慢。

原因:图片过大。

解决:在数据库中增加字段,存压缩后的图片。在上传图片的时候,对图片进行压缩处理,存两份,一份原图,一份压缩图。

显示图片列表的时候查询压缩图,查看详情的时候,去查原图。

压缩图片的工具类:

import java.awt.Image;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import javax.imageio.IIOImage;import javax.imageio.ImageIO;import javax.imageio.ImageWriteParam;import javax.imageio.ImageWriter;/** * ?* @ClassName: ImageBiz * @Description:压缩图片方法 */public class ImageBizUtil { ???/** ????* ?????* @Title: imageZip ????* @Description: 根据分辨率压缩图片 ????* @param oldByte 源图片字节 ????* @param width 压缩图片的宽 ????* @param height 压缩图片的高 ????* @return ????* @throws IOException ????* @return: byte[] ????* @throws ????*/ ???public static byte[] imageZip(byte[] oldByte, int width, int height) throws IOException { ???????ByteArrayOutputStream os = null; ???????InputStream inputStream = null; ???????try { ???????????inputStream = new ByteArrayInputStream(oldByte); ???????????// 用系统缓存 ???????????ImageIO.setUseCache(false); ???????????// 或者设定一个缓存路径 ???????????// ImageIO.setCacheDirectory(File cacheDirectory); ???????????BufferedImage image = ImageIO.read(inputStream); ???????????// 为等比缩放计算输出的图片宽度及高度 ???????????int imageWidth = image.getWidth(null); ???????????int imageHeight = image.getHeight(null); ???????????float ratio = getRatio(imageWidth, imageHeight, width, height); ???????????int newWidth = (int) (ratio * imageWidth); ???????????int newHeight = (int) (ratio * imageHeight); ???????????BufferedImage newBufferedImage = ???????????????????new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB); ???????????newBufferedImage.getGraphics().drawImage( ???????????????????image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null); ???????????Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ???????????ImageWriter imageWriter = iter.next(); ???????????ImageWriteParam iwp = imageWriter.getDefaultWriteParam(); ???????????iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); ???????????os = new ByteArrayOutputStream(oldByte.length); ???????????imageWriter.setOutput(ImageIO.createImageOutputStream(os)); ???????????IIOImage iio_image = new IIOImage(newBufferedImage, null, null); ???????????imageWriter.write(null, iio_image, iwp); ???????????os.flush(); ???????????byte[] newBytes = os.toByteArray(); ???????????return newBytes; ???????} catch (IOException e) { ???????????throw e; ???????} finally { ???????????if (os != null) { ???????????????os.close(); ???????????} ???????????if (inputStream != null) { ???????????????inputStream.close(); ???????????} ???????} ???} ???/** ????* ?????* @Title: getRatio ????* @Description: 压缩比例算法 ????* @param imageWidth 原图片宽 ????* @param imageHeight 原图片高 ????* @param width 压缩后的图片宽 ????* @param height 压缩后的图片高 ????* @return ????* @return: float ????*/ ???public static float getRatio(int imageWidth, int imageHeight, int width, int height) { ???????float Ratio = 1.0f; ???????float widthRatio = (float) width / imageWidth; ???????float heightRatio = (float) height / imageHeight; ???????if (widthRatio < 1.0 || heightRatio < 1.0) { ???????????Ratio = widthRatio <= heightRatio ? widthRatio : heightRatio; ???????} ???????return Ratio; ???}}

核心代码:

//海报上传 ?public BaseResult uploadPosterInfomation(List<GoPhosterInfomationPo> list){ ???????//创建返参对象 ???BaseResult baseResult = new BaseResult(); ???????????for (int i = 0; i < list.size(); i++) { ???????????if (StringUtils.isBlank(list.get(i).getTypeCode())) { ???????baseResult.setRet(BaseResult.BIZ_ERROR); ???????baseResult.setMsg("请录入海报类型"); ???????return baseResult; ?????} ???????????if (StringUtils.isBlank(list.get(i).getPhosterImage())) { ???????baseResult.setRet(BaseResult.BIZ_ERROR); ???????baseResult.setMsg("请选择要保存的海报模板"); ???????return baseResult; ?????} ???????????//设置海报唯一标识 ?????list.get(i).setPhosterId(UUIDUtil.genUUID2()); ???????????//设置海报序号 ?????Integer maxNum = posterMakeFunctionMapper.queryMaxNum(); ?????if (maxNum == null || maxNum == 0) { ???????list.get(i).setOrderNumber(1); ?????}else{ ???????list.get(i).setOrderNumber(maxNum+1); ?????} ???????//将base64图片转为二进制 ?????byte[] imgByt = ImageUtil.getImageByte(list.get(i).getPhosterImage()); ?????byte[] imgZip = null;//压缩之后的图片 ?????try { ?????????imgZip = ImageBizUtil.imageZip(imgByt, 200, 200); ?????} catch (IOException e) { ?????????e.printStackTrace(); ?????} ?????String imgStr = ImageUtil.getImageStr(imgZip); ??????????list.get(i).setPhosterImageZip("data:image/png;base64,"+imgStr); ???????????//保存海报 ?????posterMakeFunctionMapper.uploadPosterInfomation(list.get(i)); ?????????}

2.在制作海报的时候,设置微软雅黑字体,在本地测试,生成的海报上的字体是微软雅黑,但是在服务器上却显示宋体。

原因:服务器没有微软雅黑字体。

解决:

01.在服务器上安装微软雅黑字体

1.到windows环境下找到微软雅黑字体库,C:\Windows\Fonts。msyf.ttf(微软雅黑))2.到linux环境下创建目录mkdir -pv /usr/share/fonts/chinese/TrueType使用rz命令,将字体放入目录下rz cd /usr/share/fonts/chinese/TrueTypechmod 755 * 为字体赋予可执行权限3.建立字体缓存# mkfontscale (如果提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale )# mkfontdir# fc-cache -fv (如果提示 fc-cache: command not found,则需要安装# yum install fontconfig )4.reboot重启系统

02.将微软雅黑字体文件放在项目中

工作总结02(海报上传模块)

原文地址:https://www.cnblogs.com/lyb0103/p/9732586.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved