2018-12-10

tesCamera(){

? ? let that =this;

? ? //調(diào)用原生系統(tǒng)彈出按鈕選擇框let page =null;

? ? page={

? ? ? ? imgUp:function(){

? ? ? ? ? ? plus.nativeUI.actionSheet(

? ? ? ? ? ? ? ? {cancel:"取消",buttons:[

? ? ? ? ? ? ? ? {title:"拍照"},

? ? ? ? ? ? ? ? {title:"從相冊(cè)中選擇"}

? ? ? ? ? ? ]}, function(e){

? ? ? ? ? ? ? ? //1 是拍照? 2 從相冊(cè)中選擇 switch(e.index){

? ? ? ? ? ? ? ? ? ? case1:

? ? ? ? ? ? ? ? ? ? getImage();

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? case2:

? ? ? ? ? ? ? ? ? ? appendByGallery();

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? default:

? ? ? ? ? ? ? ? ? ? break;? ?

? ? ? ? ? ? ? ? }

? ? ? ? ? ? });

? ? ? ? }

? ? }

? ? // 拍照函數(shù)function getImage(){

? ? ? ? let cmr = plus.camera.getCamera();

? ? ? ? cmr.captureImage(function(p){

? ? ? ? ? ? plus.io.resolveLocalFileSystemURL(p, function(entry){

? ? ? ? ? ? ? ? varpath = entry.toLocalURL();

? ? ? ? ? ? ? ? //文件傳轉(zhuǎn)base64方法? ? ? ? ? ? ? ? that.imgPreviewnew(path, _typedata);

? ? ? ? ? ? }, function(e){

? ? ? ? ? ? ? ? console.log("讀取拍照文件錯(cuò)誤:"+e.message);

? ? ? ? ? ? });

? ? ? ? }, function(e){

? ? ? ? ? ? console.log("讀取拍照文件錯(cuò)誤:"+e.message);

? ? ? ? }, {filename:'_doc/camera/',index:1});

? ? }

? ? //選擇相片文件function appendByGallery(){

? ? ? ? plus.gallery.pick(function(path){

? ? ? ? ? ? //文件傳轉(zhuǎn)base64方法? ? ? ? ? ? that.imgPreviewnew(path, _typedata);

? ? ? ? });

? ? }

? ? // 彈出系統(tǒng)選擇按鈕框? ? ? page.imgUp();

}

圖片轉(zhuǎn)base64函數(shù);

imgPreviewnew(file, type){

? ? let that =this;

? ? let Orientation;

? ? let img =new Image();

? ? img.src = file;

? ? img.onload =function () {

? ? ? ? //壓縮圖片函數(shù)-輸出base64let data = that.compress(img,Orientation);

? ? }

}

圖片壓縮函數(shù)

compress(img,Orientation) {

? let canvas = document.createElement("canvas");

? let ctx = canvas.getContext('2d');

? ? //瓦片canvaslet tCanvas = document.createElement("canvas");

? let tctx = tCanvas.getContext("2d");

? let initSize = img.src.length;

? let width = img.width;

? let height = img.height;

? //如果圖片大于四百萬像素,計(jì)算壓縮比并將大小壓至400萬以下? let ratio;

? if((ratio = width * height / 4000000) > 1) {

? ? console.log("大于400萬像素")

? ? ratio = Math.sqrt(ratio);

? ? width /= ratio;? ? height /= ratio;? }else {

? ? ratio = 1;

? }

? canvas.width = width;

? canvas.height = height;//? ? ? ? 鋪底色ctx.fillStyle = "#fff";

? ctx.fillRect(0, 0, canvas.width, canvas.height);

? //如果圖片像素大于100萬則使用瓦片繪制? let count;

? if((count = width * height / 1000000) > 1) {

? ? console.log("超過100W像素");

? ? count = ~~(Math.sqrt(count) + 1);//計(jì)算要分成多少塊瓦片//? ? ? ? ? ? 計(jì)算每塊瓦片的寬和高let nw = ~~(width / count);

? ? let nh = ~~(height / count);

? ? tCanvas.width = nw;

? ? tCanvas.height = nh;

? ? for(let i = 0; i < count; i++) {

? ? ? for(let j = 0; j < count; j++) {

? ? ? ? tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);

? ? ? ? ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);

? ? ? }

? ? }

? } else {

? ? ctx.drawImage(img, 0, 0, width, height);

? }

? //修復(fù)ios上傳圖片的時(shí)候 被旋轉(zhuǎn)的問題if(Orientation != "" && Orientation != 1){

? ? switch(Orientation){

? ? ? case6://需要順時(shí)針(向左)90度旋轉(zhuǎn)this.rotateImg(img,'left',canvas);

? ? ? ? ? break;

? ? ? case8://需要逆時(shí)針(向右)90度旋轉(zhuǎn)this.rotateImg(img,'right',canvas);

? ? ? ? ? break;

? ? ? case3://需要180度旋轉(zhuǎn)this.rotateImg(img,'right',canvas);//轉(zhuǎn)兩次this.rotateImg(img,'right',canvas);

? ? ? ? ? break;

? ? }

? }

? //進(jìn)行最小壓縮let ndata = canvas.toDataURL('image/jpeg', 0.1);

? console.log('壓縮前:' + initSize);

? console.log('壓縮后:' + ndata.length);

? console.log('壓縮率:' + ~~(100 * (initSize - ndata.length) / initSize) + "%");

? tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;

? return ndata;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容