亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue項(xiàng)目base64字符串轉(zhuǎn)圖片的實(shí)現(xiàn)代碼

 更新時(shí)間:2018年07月13日 08:42:53   作者:?jiǎn)坛? 
這篇文章主要介紹了vue項(xiàng)目base64字符串轉(zhuǎn)圖片的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
<img :onerror="errpic" class="customerHead" :src="param.customerHead" alt="">
data() {

  return {
    param:{ 
       id:"",
         customerHead: "",
    }
  }
}
let _this = this
let files = e.target.files[0]
if (files.size/(1024*1024) > 2) {
this.open('上傳的圖片不可大于2M!')
return false;
}
var reader = new FileReader();
reader.onload = function (e) {
var base64 = e.target.result;
_this.param.customerHead = base64
//console.log(base64)
}
if(files) {
reader.readAsDataURL(files);
}

如果修改頭像,向后臺(tái)傳base64字符串,否則會(huì)傳原圖片路徑,后臺(tái)判斷是否是base64字符串.

如果是base64字符串,則對(duì)base64字符串進(jìn)行處理,在后臺(tái)服務(wù)器生成圖片.此處需要對(duì)base64字符串進(jìn)行處理,如圖所示,刪除藍(lán)框部分,留逗號(hào)之后的內(nèi)容.

若為圖片路徑,則不需要進(jìn)行處理,直接返回圖片路徑即可.

@Value("${upload.image.path}")
private String filePath;
//base64字符串轉(zhuǎn)化成圖片 headerImgPath:http://+ip+:端口號(hào) 
  public String generateImage(String imgStr,String headerImgPath,String cusID)
  { //對(duì)字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片
    if (imgStr == null) //圖像數(shù)據(jù)為空
      return "../picclife/static/custom.png";
    BASE64Decoder decoder = new BASE64Decoder();
    try
    {
      //判斷是base64字符串還是圖片路徑
      if(imgStr.substring(0,5).equals("data:")){
        //Base64解碼
        byte[] b = decoder.decodeBuffer(imgStr.substring(imgStr.indexOf(",") + 1));
        for(int i=0;i<b.length;++i)
        {
          if(b[i]<0)
          {//調(diào)整異常數(shù)據(jù)
            b[i]+=256;
          }
        }
        //生成圖片
        String imgFilePath = filePath+"/headerImg/"+cusID+".jpg";//新生成的圖片
        OutputStream out = new FileOutputStream(imgFilePath);
        out.write(b);
        out.flush();
        out.close();
        return headerImgPath+"headerImg/"+cusID+".jpg";
      }else{
        return imgStr;
      }
    }
    catch (Exception e)
    {
      return "../picclife/static/custom.png";
    }
  }

總結(jié)

以上所述是小編給大家介紹的vue項(xiàng)目base64字符串轉(zhuǎn)圖片的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論