手机端多图上传,调取摄像头和相册选择


html
    <button onclick="showActionSheet()">点击上传</button>

script
    var imgs = "";

    function showActionSheet() {
        var actionbuttons = [{
            title: "拍照"
        }, {
            title: "相册选取"
        }];
        var actionstyle = {
            title: "选择照片",
            cancel: "取消",
            buttons: actionbuttons
        };
        plus.nativeUI.actionSheet(actionstyle, function(e) {
            if(e.index == 1) {
                getImage();
            } else if(e.index == 2) {
                appendByGallery();
            }
        });
    }

    function getImage() {
        var cmr = plus.camera.getCamera();
        cmr.captureImage(function(p) {
            plus.io.resolveLocalFileSystemURL(p, function(entry) {
                var localurl = entry.toLocalURL(); //把拍照的目录路径,变成本地url路径,例如file:///........之类的。
                appendFile(localurl);
                console.log("返回路径:" + localurl)
                //                      }
            });
        }, function(error) {
            alert("Capture image failed: " + error.message);
        });
    }

    // 从相册添加文件 
    function appendByGallery() {
        plus.gallery.pick(function(path) {
            appendFile(path); //处理图片的地方
        });
    }

    // 添加文件
    function appendFile(path) {
        var img = new Image();
        img.src = path; // 传过来的图片路径在这里用。
        img.onload = function() {
            var that = this;
            //生成比例 
            var w = that.width,
                h = that.height,
                scale = w / h;
            w = 480 || w; //480  你想压缩到多大,改这里
            h = w / scale;

            //生成canvas
            var canvas = document.createElement('canvas');
            var ctx = canvas.getContext('2d');
            $(canvas).attr({
                width: w,
                height: h
            });
            ctx.drawImage(that, 0, 0, w, h);
            var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8); //1最清晰,越低越模糊。    


            var imgPath = TransferString(base64);
            console.log(imgPath)
            upload(imgPath)
        }
    }

    function TransferString(content) {
        var string = content;
        try {
            string = string.replace(/\r\n/g, "<br>")
            string = string.replace(/\n/g, "<br>");
        } catch(e) {
            alert(e.message);
        }
        return string;
    }

    function upload(imgPath) {
        $.ajax({
            type: "post",
            url: imgServerUrl+imgUpload,
            data: {
                base64Data: imgPath,
                dataType: ".jpg"
            },
            success: function(data) {
                console.log("返回路径:" + JSON.stringify(data));
                imgs = '<img src="' + data + '" alt=""/>';
                $('#imgFile').append(imgs);
            },
            error: function(err) {
                console.log("错误:" + JSON.stringify(err))
            }
        });
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值