private afterFront(file){
console.log(2, file)
let file_mb = file.content.length / (1024.0 * 1024);
console.log(2, file_mb);
let img = new Image();
img.src = file.content;
this.compress(file_mb, img, (d) => {
let scanImg = d;
// if (d) {
// scanImg = d;
// } else {
// scanImg = file.content;
// }
console.log(2, scanImg.length / (1024.0 * 1024))
this.uploadBase(scanImg,1);
})
}
compress(file_mb, img, callBack) {
if (file_mb <= 0.5) {
callBack(img.src);
return;
}
img.onload = () => {
const canvas = document.createElement('canvas');
if (!canvas) {
callBack(null);
return;
}
const ctx = canvas.getContext('2d');
if (!ctx) {
callBack(null);
return;
}
const width = img.width;
const height = img.height;
canvas.width = width;
canvas.height = height;
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, width, height);
ctx.drawImage(img, 0, 0, width, height);
setTimeout(()=>{
callBack(canvas.toDataURL('image/jpeg', 0.5 / file_mb));
},0);
}
}
private uploadBase(base,face){
// loading.openLoading();
api.postJson('/oss/aaa',{content:base}).then((res:any)=>{
loading.clearLoading();
this.canSelect = false;
if (res.errno=='0') {
if (face == 1) {
this.data2.img = this.imgTransfer(res.data.url);
}
} else {
this.$toast(res.error);
}
}).catch((error)=>{
loading.clearLoading();
this.$toast('网络波动,请稍后再试');
})
}