js 图片转base64码后 再对base64码进行压缩 然后传给后台 无限压缩 直到符合输出码率

kejiang 在这里插入图片描述在这里插入图片描述

1 html

					<div>
                        <img
                            :src="url"
                            alt
                            class="el-upload-list__item-thumbnail"
                        />
                        <input
                            type="file"
                            @change="change($event)"
                            id="input"
                            accept="image/jpeg, image/png, image/gif"
                            ga-data="imoyao-enterprise-132"
                            class="inputfile"
                            capture="camera"
                        />
                    </div>
                     <div class="f2-sub_btn">
            <van-button
                type="primary"
                :disabled="disabled"
                block
                @click="gosign"
                >提交认证</van-button>
        	</div>
 //上传图片信息保存在数组中并且回显
        change(event) {
            let file = event.target.files[0];
            console.log(file.size);
            console.log("------22222---------");
            let windowURL = window.URL || window.webkitURL;
            //图片格式为PNG或JPG且小于5M
            if (/\.(jpg|jpeg|png|JPG|JPEG|PNG)$/.test(file.name)) {
                if (file) {
                    let dataURl = windowURL.createObjectURL(file);
                    file.url = dataURl;
                    this.url = dataURl;
                    this.getImgBase64(dataURl, "url");
                }
            } else {
                Toast({
                    message: "身份证格式为PNG或JPG",
                    position: "middle",
                    duration: 1500,
                });
            }
        },
        image2Base64(img,size = 0.9) {
            var canvas = document.createElement("canvas");
            canvas.width = img.width;
            canvas.height = img.height;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, img.width, img.height);
            let dataURL = canvas.toDataURL("image/jpeg", size);
            if (dataURL.length > 1000000) {//如果输出大于1m 就递归直到输出小于1M才行
                return this.image2Base64(img, (size / 2).toFixed(1) * 1);
            } else {
                console.log(dataURL);
                return dataURL;
            }
        },
        getImgBase64(src, flag) {
            var base64 = "";
            var img = new Image();
            img.src = src;
            let that = this;
            img.onload = function () {
                base64 = that.image2Base64(img);
                console.log(base64.length);
                console.log("==============");
                if (flag == "url") {
                    that.url = base64;
                } else {
                    that.urlfan = base64;
                }

                if (that.url.length > 500 && that.urlfan.length > 500) {
                    Toast.loading({
                        message: "提交中...",
                        forbidClick: true,
                        duration: 0,
                    });
                    that.post("/personal/realNameAuth", {
                        idCardFront: that.url,
                        idCardBack: that.urlfan,
                    }).then((res) => {
                        that.expiryDate = res.expiryDate;
                        that.licenceAuthority = res.licenceAuthority;
                        that.name = res.name;
                        that.idNo = res.idNo;
                        that.disabled = false;

                        that.post("/personal/realNamesAuth", {
                            name: that.name,
                            idNo: that.idNo,
                            sfzmocrplatsrl: res.sfzmocrplatsrl,
                        }).then((data) => {
                            Toast.clear();
                        });
                    });
                }
            };
        },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值