apicloud直接上傳圖片


function getPicture() {
    api.confirm({
      title : "提示",
      msg : "選擇圖片",
      buttons : ["現在照", "相冊選", "取消"]
    }, function(ret, err) {
      //定義圖片來源類型
      var sourceType;
      if (1 == ret.buttonIndex) {/* 打開相機*/
        sourceType = "camera";
        openPicture(sourceType);
      } else if (2 == ret.buttonIndex) {
        sourceType = "album";
        openPicture(sourceType);
      } else {
        return;
      }
      });
 }
  //  選取圖片
function openPicture(sourceType) {
  var q = 100;
  //獲取一張圖片
  api.getPicture({
    sourceType : sourceType,
    encodingType : 'png',
    mediaValue : 'pic',
    //返回數據類型,指定返回圖片地址或圖片經過base64編碼后的字符串
    //base64:指定返回數據為base64編碼后內容,url:指定返回數據為選取的圖片地址
    destinationType : 'base64',
    //是否可以選擇圖片后進行編輯,支持iOS及部分安卓手機
    allowEdit : false,
    //圖片質量,只針對jpg格式圖片(0-100整數),默認值:50
    quality : q,
    //                targetWidth : 100,
    //                targetHeight : 1280,
    saveToPhotoAlbum : true
  }, function(ret, err) {
    if (ret) {
      //  data:"fs://",                //圖片路徑
      //  base64Data:"",          //base64數據,destinationType為base64時返回
      var img_url = ret.data;
      if (img_url != "") {
          function getBase64Image(img1) {
            var cvs = document.createElement("canvas");
            var width = img1.width;
            height = img1.height;
            var scale = width / height;
            cvs.width = width;
             cvs.height = height;
            var ctx = cvs.getContext("2d");
            ctx.drawImage(img1, 0, 0,width,height,0,0,width,height);
            var src = cvs.toDataURL("image/jpeg",0.7);
            return src;
          }
        var image=new Image();
        $('#imgUp').show();
        $('.imgtext').hide();
        $('#imgUp').attr('src',img_url);
        image.src = $('#imgUp').attr('src');
        image.onload=function(){//轉base64圖片
          var base64 = getBase64Image(image);
          localStorage.base64=base64;
          var imgmsg = {UpType:'HandheldPhoto',Vid:'',Uid:Uid,Position:'',Base64:localStorage.base64,Uid:Uid,Client_Id:Client_Id};
          Get_request(120102, imgmsg, 'post', 1);//1,編號  2,要傳送的值  3,請求方式  4,請求地址
        };
      }
    }
  });
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM