h5+ 上傳圖片(選擇圖片、拍照)


$(document).on('click','.upimgbtn', function() {
    var pos = $(this).data('pos');
    var posTxt = {'photo1':'照片1','photo2':'照片2','photo3':'照片3'};
    var that = this;
    if(mui.os.plus) {
        var a = [{
            title: "拍照"
        }, {
            title: "從手機相冊選擇"
        }];
        plus.nativeUI.actionSheet({
            title: posTxt[pos]+"上傳",
            cancel: "取消",
            buttons: a
        }, function(b) { /*actionSheet 按鈕點擊事件*/
            switch(b.index) {
                case 0:
                    break;
                case 1:
                    getImage(that,pos); /*拍照*/
                    break;
                case 2:
                    galleryImg(that,pos); /*打開相冊*/
                    break;
                default:
                    break;
            }
        })
    }
});


//拍照
function getImage(that,pos) {
    var cmr = plus.camera.getCamera();
    cmr.captureImage(function(p) {
        //alert(p);//_doc/camera/1467602809090.jpg
        plus.io.resolveLocalFileSystemURL(p, function(entry) {
            //alert(entry.toLocalURL());//file:///storage/emulated/0/Android/data/io.dcloud...../doc/camera/1467602809090.jpg
            //alert(entry.name);//1467602809090.jpg
            var path = (entry.toLocalURL());
            $(that).html("<img src='" + path + "' style='display:block;' />")

            zoomImage(path,pos)
            // upload(path,pos)
        }, function(e) {
            plus.nativeUI.toast("讀取拍照文件錯誤:" + e.message);
        });
    }, function(e) {}, {
        filename: "_doc/camera/",
        index: 1
    });
}
//本地相冊選擇
function galleryImg(that,pos) {
    plus.gallery.pick(function(path) {
        // alert("get image success: " + path);
        $(that).html("<img src='" + path + "' style='display:block;' />")

        zoomImage(path,pos)
        // upload(path,pos)

    }, function(e) {
        console.log("取消選擇圖片");
    }, {
        filter: "image"
    });
};


function upload(path,pos){
    var wt=plus.nativeUI.showWaiting();
    var task=plus.uploader.createUpload(baseUrl+'/upload',
        {method:"POST"},
        function(t,status){ //上傳完成
            if(status==200){
    //             var data=JSON.parse(t.responseText);
                // if(data.code == 1){
                    switch(pos){
                        case 'photo1':
                                                            // 上傳結果(服務器圖片地址)處理
                                                           break;

                // }
                // alert("上傳成功:"+t.responseText);
                wt.close(); //關閉等待提示按鈕
            }else{
                // alert("上傳失敗:"+status);
                wt.close();//關閉等待提示按鈕
            }
        }
    );
    //添加其他參數
    // task.addData("name","test");
    task.addFile(path,{key:"file"});
    task.start();
}

//縮放圖片
function zoomImage(path,pos){
    plus.zip.compressImage({
            src:path,
            dst:path,
            width:'800px',        // 縮小
            overwrite: true
        },
        function() {
            upload(path,pos)
            // alert("Compress success!");
        },function(error) {
            // alert("Compress error!");
    });
}

 


免責聲明!

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



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