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

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/TrueType
chmod 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.将微软雅黑字体文件放在项目中

 001.将字体文件放到项目中

002.根据项目中的字体文件获取字体Font的工具类。

package cn.picclife.mwx.salesupport.phoster.service.impl;

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;

import cn.picclife.mwx.salesupport.marketactivity.util.ParsePDFUtil;

/**
 * 
* @ClassName: FontUtil 
* @Description: TODO(根据项目中的字体库,获取Font的工具类) 
* @author yabo.liu 
* @date 2018年9月25日 下午3:54:32 
*
 */
public class FontUtil {
  /**
   * 
  * @Title: getFont 
  * @Description: TODO(根据项目字体库,获取Font) 
  * @param @param size
  * @param @return    设定文件 
  * @return Font    返回类型 
  * @date 2018年9月25日 下午3:55:59
  * @throws
   */
  public static Font getFont(float size,String path) {
    String caPath = ParsePDFUtil.class.getClassLoader().getResource("ca").getPath().toString();
    String fontPath = caPath + path;
    try {
        Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath));
        dynamicFont = dynamicFont.deriveFont(size);
        return dynamicFont;
    }catch (FontFormatException e) {
      e.printStackTrace();
    }catch(IOException e){
      e.printStackTrace();
    }
    
    return null;
  }
  
  
}

003.核心代码。

 

posted @ 2018-09-30 16:58  迷茫王子  阅读(344)  评论(0编辑  收藏  举报