H5拍照

<input
type="file"
accept="image/*"
@change="select">
iOS下會(huì)喚起選擇照片或者拍照面板
Android直接進(jìn)入選擇照片,添加capture="camera"后直接喚起拍照
所以Android需要自己實(shí)現(xiàn)一個(gè)拍照或者選擇照片面板來(lái)適應(yīng)iOS逗鸣,所以讓APP實(shí)現(xiàn)去吧成榜,整個(gè)統(tǒng)一的UI

    select(event) {
      // eslint-disable-next-line no-console
      console.log('select', event, event.target);
      const file = event.target.files[0];
      // eslint-disable-next-line no-console
      console.log('start', file);
      const fileReader = new FileReader();
      fileReader.onload = async (e) => {
        // eslint-disable-next-line no-console
        console.log('filereader.onload', file, file.type, e.target);
        const base64Str = e.target.result;
        if (!base64Str) {
          Toast.show('選擇文件有誤,請(qǐng)重試');
          return;
        }

        const img = new Image();
        img.onload = async () => {
          try {
            // eslint-disable-next-line no-console
            console.log('img.onload');
            // 獲取橫屏豎屏
            const Orientation = await this.getOrientation(img);
            let n = 0;
            if (Orientation === 6) {
              n = 1;
            }
            // iphone拍照上傳圖片橫屏的問(wèn)題
            const canvas = this.rotateAndCompressImg(img, n, 700);
            // eslint-disable-next-line no-console
            console.log('canvas壓縮旋轉(zhuǎn)后的canvas');
            // 導(dǎo)出base64文件
            const imgData = canvas.toDataURL('image/jpeg');
            // eslint-disable-next-line no-console
            console.log('canvas壓縮旋轉(zhuǎn)后的base64', imgData.length);
            // 獲取被canvas壓縮后的圖片對(duì)象留拾,從而獲取base64字符串字節(jié)大小
            const blob = await Tools.base64ToBlob(imgData.split(',')[1], 'image/png');
            // eslint-disable-next-line no-console
            console.log('壓縮后的img', blob, blob.size);
            // 限制 15M 以內(nèi)
            if (blob.size > 1048576 * 15) {
              Toast.show('選擇文件太大,請(qǐng)重選');
              return;
            }
            this.uploadImg({
              file: imgData.slice(imgData.indexOf(',') + 1),
              type: 'image/jpeg'
            });
            // change事件監(jiān)聽(tīng)的是input的value的改變褐耳,不清空胁孙,就不能選擇同一個(gè)文件
            // eslint-disable-next-line no-console
            console.log(event.target);
            if (event.target && event.target.value) {
              event.target.value = '';
            }
          } catch (err) {
            // eslint-disable-next-line no-console
            console.log('外層', err);
          }
        };
        img.src = base64Str;
      };
      fileReader.readAsDataURL(file);
    },
    getOrientation(img) {
      return new Promise(((resolve) => {
        EXIF.getData(img, function _cb() {
          try {
            EXIF.getAllTags(this);
            const Orientation = EXIF.getTag(this, 'Orientation');
            resolve(Orientation);
          } catch (e) {
            // eslint-disable-next-line no-console
            console.log('getOrientation', e);
            // reject(e);
            // 即便獲取圖片寬高失敗也不阻止后續(xù)圖片上傳顯示,只是不能旋轉(zhuǎn)
            resolve(0);
          }
        });
      }));
    },
    rotateAndCompressImg(img, n, maxWidth = 700) {
      const canvas = document.createElement('canvas');
      const anw = document.createAttribute('width');
      const anh = document.createAttribute('height');
      let w;
      let h;
      const isLandscape = n % 2 !== 0;
      // eslint-disable-next-line no-console
      console.log('img原始寬高', img.width, img.height);
      if (isLandscape) {
        w = img.height;
        h = img.width;
      } else {
        w = img.width;
        h = img.height;
      }

      if (w > maxWidth) {
        h *= (maxWidth / w);
        w = maxWidth;
      }
      anw.nodeValue = w;
      anh.nodeValue = h;
      canvas.setAttributeNode(anw);
      canvas.setAttributeNode(anh);
      const ctx = canvas.getContext('2d');
      ctx.translate(w / 2, h / 2);
      ctx.rotate(n * 0.5 * Math.PI);
      ctx.drawImage(img, -w / 2, -h / 2, w, h);
      return canvas;
    },
    async uploadImg({ file, type }) {
      // eslint-disable-next-line no-console
      console.log('uploadImg');
      Indicator.open({
        text: '圖片上傳中...',
      });
      try {
        const { url: src } = await recruitApi.uploadImage({
          file: file || '',
          image: 40,
          type: this.getPlatform()
        });
        Indicator.close();
        this.urls.push({
          type,
          src
        });
      } catch (error) {
        if (error.name === 'CustomError') {
          Toast.show(error.response && error.response.data && error.response.data.msg);
          return;
        }
        Toast.show(error.message);
      } finally {
        Indicator.close();
      }
    },
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末菜枷,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子叁丧,更是在濱河造成了極大的恐慌啤誊,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拥娄,死亡現(xiàn)場(chǎng)離奇詭異蚊锹,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)稚瘾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門牡昆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人摊欠,你說(shuō)我怎么就攤上這事丢烘。” “怎么了些椒?”我有些...
    開(kāi)封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵播瞳,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我免糕,道長(zhǎng)赢乓,這世上最難降的妖魔是什么忧侧? 我笑而不...
    開(kāi)封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮牌芋,結(jié)果婚禮上蚓炬,老公的妹妹穿的比我還像新娘。我一直安慰自己躺屁,他們只是感情好试吁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著楼咳,像睡著了一般熄捍。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上母怜,一...
    開(kāi)封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天余耽,我揣著相機(jī)與錄音,去河邊找鬼苹熏。 笑死碟贾,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的轨域。 我是一名探鬼主播袱耽,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼干发!你這毒婦竟也來(lái)了朱巨?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤枉长,失蹤者是張志新(化名)和其女友劉穎冀续,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體必峰,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡洪唐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吼蚁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片凭需。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖肝匆,靈堂內(nèi)的尸體忽然破棺而出粒蜈,到底是詐尸還是另有隱情,我是刑警寧澤术唬,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布薪伏,位于F島的核電站,受9級(jí)特大地震影響粗仓,放射性物質(zhì)發(fā)生泄漏嫁怀。R本人自食惡果不足惜设捐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望塘淑。 院中可真熱鬧萝招,春花似錦、人聲如沸存捺。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)捌治。三九已至岗钩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肖油,已是汗流浹背兼吓。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留森枪,地道東北人视搏。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像县袱,于是被迫代替她去往敵國(guó)和親浑娜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容