1. 程式人生 > >工作總結02(海報上傳模塊)

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

ebo 創建 als NPU 進行 share map 工具 eth

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.將微軟雅黑字體文件放在項目中

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