Blob、DataURL、canvas、image的相互转换

最近开发过程中碰到的问题:

接收到的是Blob对象,需要转换成img可以使用的src,用到的是如下方法,但是window.URL.createObjectURL不兼容IE9,在解决这个问题的时候,想着看是否可以换种方式考虑:

var url = window.URL.createObjectURL(blob);
document.getElementById("validCode").src = url;
Blob/DataURL/canvas/image的相互转换
/*-----------------------------------------------------------------------*/
// canvas转dataURL:canvas对象、转换格式、图像品质
function canvasToDataURL(canvas, format, quality){
	return canvas.toDataURL(format||'image/jpeg', quality||1.0);
}
// DataURL转canvas
function dataURLToCanvas(dataurl, cb){
	var canvas = document.createElement('CANVAS');
	var ctx = canvas.getContext('2d');
	var img = new Image();
	img.onload = function(){
		canvas.width = img.width;
		canvas.height = img.height;
		ctx.drawImage(img, 0, 0);
		cb(canvas);
	};
	img.src = dataurl;
}
/*-----------------------------------------------------------------------*/
// image转canvas:图片地址
function imageToCanvas(src, cb){
	var canvas = document.createElement('CANVAS');
	var ctx = canvas.getContext('2d');
	var img = new Image();
	img.src = src;
	img.onload = function (){
		canvas.width = img.width;
		canvas.height = img.height;
		ctx.drawImage(img, 0, 0);
		cb(canvas);
	};
}
// canvas转image
function canvasToImage(canvas){
	var img = new Image();
	img.src = canvas.toDataURL('image/jpeg', 1.0);
	return img;
}
/*-----------------------------------------------------------------------*/
// File/Blob对象转DataURL
function fileOrBlobToDataURL(obj, cb){
	var a = new FileReader();
	a.readAsDataURL(obj);
	a.onload = function (e){
		cb(e.target.result);
	};
}
// DataURL转Blob对象
function dataURLToBlob(dataurl){
	var arr = dataurl.split(',');
	var mime = arr[0].match(/:(.*?);/)[1];
	var bstr = atob(arr[1]);
	var n = bstr.length;
	var u8arr = new Uint8Array(n);
	while(n--){
		u8arr[n] = bstr.charCodeAt(n);
	}
	return new Blob([u8arr], {type:mime});
}
/*-----------------------------------------------------------------------*/
// Blob转image
function blobToImage(blob, cb){
	fileOrBlobToDataURL(blob, function (dataurl){
		var img = new Image();
		img.src = dataurl;
		cb(img);
	});
}
// image转Blob
function imageToBlob(src, cb){
	imageToCanvas(src, function (canvas){
		cb(dataURLToBlob(canvasToDataURL(canvas)));
	});
}
/*-----------------------------------------------------------------------*/
// Blob转canvas
function BlobToCanvas(blob, cb){
	fileOrBlobToDataURL(blob, function (dataurl){
		dataURLToCanvas(dataurl, cb);
	});
}
// canvas转Blob
function canvasToBlob(canvas, cb){
	cb(dataURLToBlob(canvasToDataURL(canvas)));
}
/*-----------------------------------------------------------------------*/
// image转dataURL
function imageToDataURL(src, cb){
	imageToCanvas(src, function (canvas){
		cb(canvasToDataURL(canvas));
	});
}
// dataURL转image,直接赋值给img的src
function dataURLToImage(dataurl){
	var img = new Image();
	img.src = dataurl;
	return img;
}
/*-----------------------------------------------------------------------*/

你可能感兴趣的:(js)