工做總結02(海報上傳模塊)

1.海報顯示的速度比較慢。

緣由:圖片過大。java

解決:在數據庫中增長字段,存壓縮後的圖片。在上傳圖片的時候,對圖片進行壓縮處理,存兩份,一份原圖,一份壓縮圖。linux

顯示圖片列表的時候查詢壓縮圖,查看詳情的時候,去查原圖。算法

壓縮圖片的工具類:數據庫

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;
    }
}

核心代碼:windows

//海報上傳
  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.在服務器上安裝微軟雅黑字體app

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.將字體文件放到項目中post

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.核心代碼。

相關文章
相關標籤/搜索